Olá,estou usando JButtons dentro de uma JTable para controlar os Apartamentos,so que naum consigo manipular o icone que vai para cada JButton dentro da JTable
Aqui vai o Codigo que estou usando,pois cada apartamento usa um icone de “Ocupado”,“Reservado”,“Livre”.
public Recepcao_Grid() {
initComponents();
con.conecta();
try{ con.conecta();
con.executeSQL("Select * from Apartamento");
String[] columnNames = {"","","",""};
Object[][] data =
{
};
modelo = new DefaultTableModel(data,columnNames);
modelo.setNumRows(0);
int row=modelo.getRowCount()-1;
int colun=modelo.getColumnCount();
int linha = 0;
while(con.resultset.next())
{
dados.add(con.resultset.getString("APARTAMENTO"));
status.add(con.resultset.getString("STATUS"));
}
int coluna1=0;
int coluna2=1;
int coluna3=2;
int coluna4=3;
for(int c=0;c<dados.size()/4;c++){
modelo.insertRow(c,new Object[]{dados.get(coluna1),dados.get(coluna2),dados.get(coluna3),dados.get(coluna4)});
coluna1+=4;
coluna2+=4;
coluna3+=4;
coluna4+=4;
}
}catch(SQLException error){
}
jTable1 = new JTable( modelo)
{
// Returning the Class of each column will allow different
// renderers to be used based on Class
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};
int NumRows=modelo.getColumnCount()-1;
// Create button column
for(int e=0;e<=NumRows;e++){
ButtonColumn buttonColumn = new ButtonColumn(jTable1,e);
}
}
class ButtonColumn extends AbstractCellEditor
implements TableCellRenderer, TableCellEditor, ActionListener
{
JButton renderButton;
JButton editButton;
String text;
public ButtonColumn(JTable table, int column)
{
super();
Scroll= new JScrollPane();
jPanel5.add(jTable1);
jTable1.add(Scroll);
jTable1.setSize(700,600);
jTable1.setAlignmentX(5);
jTable1.setAlignmentY(0);
String url="";
renderButton = new JButton();
int row=modelo.getRowCount()-1;
for(int e=0;e<=row;e++){
try{
con.executeSQL("Select * from Apartamento where APARTAMENTO='"+table.getValueAt(e, column)+"'");
con.resultset.first();
String status=con.resultset.getString(("STATUS"));
if(status.equals("Livre")){
url="\\livre.png";
}if(status.equals("Reservado")){
url="\\reservado.png";
}if(status.equals("Ocupado")){
url="\\ocupado.png";
}
} catch(SQLException error){
}
}
Icon icone = new ImageIcon(getClass().getResource(url));
renderButton.setIcon(icone);
renderButton.setHorizontalTextPosition(SwingConstants.CENTER);
renderButton.setVerticalTextPosition(SwingConstants.BOTTOM);
table.setRowHeight(60);
editButton = new JButton();
editButton.setFocusPainted( false );
editButton.addActionListener( this );
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(column).setCellRenderer(this);
columnModel.getColumn(column).setCellEditor( this );
}
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
if (hasFocus)
{
renderButton.setForeground(Color.lightGray);
renderButton.setBackground(UIManager.getColor("Button.background"));
}
else if (isSelected)
{
renderButton.setForeground(Color.black);
}
else
{
renderButton.setForeground(table.getForeground());
renderButton.setBackground(UIManager.getColor("Button.background"));
}
renderButton.setText( (value == null) ? "" : value.toString() );
return renderButton;
}
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column)
{
text = (value == null) ? "" : value.toString();
editButton.setText( text );
return editButton;
}
public Object getCellEditorValue()
{
return text;
}
public void actionPerformed(ActionEvent e)
{
fireEditingStopped();
System.out.println( e.getActionCommand() + " : " + jTable1.getSelectedRow());
}
}
Obrigado desde já