I'm trying to use a cell render, so when I display monetary data, it does formatting. However, when I apply rendering to a particular column, the focus in that column does not have the background of the rest of the row.
To solve this, I thought of doing the same thing I did in my AbstractTable, creating a method that would change the color of the selected line, so it ends up getting all over the line. However, being Default, I could not do the same thing. So if anyone knows a way to apply render that does not bug or know how to set the color.
In my abstract, I do this:
private JTable tabela = new JTable(jtmCompra)
{
@Override
public Component prepareRenderer(TableCellRenderer renderer, int linha, int coluna)
{
Component componente = super.prepareRenderer(renderer, linha, coluna);
if (isRowSelected(linha))//Focu na linha.
{
componente.setBackground(new Color(147, 200, 255, 255));
}
}
}
Dai how "paint line" is right.
Simpleexample:
SystemScreen:
packagetelas;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JDesktopPane;importjavax.swing.JFrame;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;publicclassTelaSistemaextendsJFrameimplementsActionListener{publicstaticJDesktopPanejdp=newJDesktopPane();publicJMenuBarjmb=newJMenuBar();publicJMenujmCadastros=newJMenu("Cadastros");
public JMenuItem jmiE = new JMenuItem("Uma tela qualquer");
public TelaSistema()
{
setExtendedState(MAXIMIZED_BOTH);
setTitle("Sistema");
getContentPane().add(jdp);
setJMenuBar(jmb);
jmb.add(jmCadastros);
adicionaJMenuItem(jmCadastros, jmiE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void adicionaJMenuItem(JMenu menu, JMenuItem item)
{
menu.add(item);
item.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == jmiE)
{
TelaCadastroA a = new TelaCadastroA();
jdp.add(a);
}
}
public static void main(String args[])
{
TelaSistema telaSistema = new TelaSistema();
}
}
Screen: Registration
package telas;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
public class TelaCadastroA extends JInternalFrame implements ActionListener
{
public JButton jbConsultar = new JButton("Consultar");
public JPanel jpBotoes = new JPanel();
public TelaCadastroA()
{
super("");
adicionaBotao(jbConsultar);
setSize(500, 500);
getContentPane().add("South", jpBotoes);
jpBotoes.setLayout(new GridLayout(1, 1));
Dimension tamanhoTela = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((tamanhoTela.width - getWidth()) / 2, ((tamanhoTela.height - getHeight())) / 2);
setVisible(true);
}
private void adicionaBotao(JButton botao) {
jpBotoes.add(botao);
botao.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == jbConsultar)
{
consultar();
}
}
public void consultar()
{
TelaConsultar.getTela("Consulta", new String[]
{
"Código", "A", "B", "C"
}, new DefaultTableCellRenderer[] {null, null, new CellRenderMonetario(), null}, this);
}
class CellRenderMonetario extends DefaultTableCellRenderer
{
private DecimalFormat df = new DecimalFormat(",##0.00");
@Override
public Component getTableCellRendererComponent(JTable tabela, Object valor, boolean isSelected, boolean temFocu, int linha, int coluna)
{
if (valor == null)
{
valor = 0;
}
if (valor instanceof String)
{
setText(df.format(Double.parseDouble((String) valor)) + " ");
}
else
{
setText(df.format(valor) + " ");
}
setHorizontalAlignment(SwingConstants.RIGHT);
return this;
}
}
}
Coonsulta Screen:
package telas;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
public class TelaConsultar extends JInternalFrame implements InternalFrameListener, MouseListener {
private static TelaConsultar telaConsultar = null;
private String sql;
private String[] titulos;
private TelaCadastroA tela;
private JTable tabela;
private JScrollPane jsp;
private DefaultTableModel dtm = new DefaultTableModel();
private String proc;
public TelaConsultar(String tituloJanela, String[] titulos, DefaultTableCellRenderer[] renderizadores, TelaCadastroA tela)
{
super(tituloJanela, true, true, false, false);
this.sql = sql;
this.titulos = titulos;
this.tela = tela;
tabela = new JTable()
{
@Override
public boolean isCellEditable(int linha, int coluna)
{
return false;//Esta tabela não pode ser editada pois é uma consulta
}
};
tabela.setModel(dtm); //Define o modelo da tabela
tabela.getTableHeader().setReorderingAllowed(false);
for (int i = 0; i < titulos.length; i++)
{
dtm.addColumn(titulos[i]);
}
jsp = new JScrollPane(tabela);
getContentPane().add(jsp);
preencher();
//pack();
setSize(650, 400);
setMinimumSize(new Dimension(getWidth(), getHeight()));
centralizaTelaConsulta();
setVisible(true);
tabela.addMouseListener(this);
addInternalFrameListener(this);
TableColumnModel tableModel = tabela.getColumnModel();
for (int i = 0; i < titulos.length; i++)
{
tableModel.getColumn(i).setMinWidth(70);
}
for (int i = 0; i < tableModel.getColumnCount(); i++)
{
if (renderizadores[i] != null)
{
tableModel.getColumn(i).setCellRenderer(renderizadores[i]);
/* if (isRowSelected(i))//Focu na linha.
{
}*/
}
}
}
public static void getTela(String tituloJanela, String[] titulos, DefaultTableCellRenderer[] renderizadores, TelaCadastroA tela)
{
if (telaConsultar == null)
{
telaConsultar = new TelaConsultar(tituloJanela, titulos, renderizadores, tela);
TelaSistema.jdp.add(telaConsultar);
}
TelaSistema.jdp.setSelectedFrame(telaConsultar);
TelaSistema.jdp.moveToFront(telaConsultar);
}
private void preencher()
{
for (int i = 0; i < 10; i++)
{
dtm.addRow(new Object[]{"" + i, "Brasil - " + i});
}
}
public void centralizaTelaConsulta()
{
Dimension tamanhoTela = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((tamanhoTela.width - getWidth()) / 2, ((tamanhoTela.height - getHeight())) / 2);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void internalFrameOpened(InternalFrameEvent e) {
}
@Override
public void internalFrameClosing(InternalFrameEvent e) {
}
@Override
public void internalFrameClosed(InternalFrameEvent e) {
TelaSistema.jdp.remove(telaConsultar);
telaConsultar = null;
}
@Override
public void internalFrameIconified(InternalFrameEvent e) {
}
@Override
public void internalFrameDeiconified(InternalFrameEvent e) {
}
@Override
public void internalFrameActivated(InternalFrameEvent e) {
}
@Override
public void internalFrameDeactivated(InternalFrameEvent e) {
}
@Override
public void mouseClicked(MouseEvent me) {
}
}