For those curious or had trouble like I did, here is some code for formatting JTables base on what is in their cell. Might find this useful if you have never done this like I hadnt. Learned alot on this 4 day project. Any thoughts would be appreciated.
Variables and Setup:
//Column Names
Object[] columnNames = new Object[]{"Range","Color 1","Color 2","Type","Single"};
//JTable Data. For this table, the types are String,Color,Color,JComboBox,JCheckBox
Object[][] patternData = new Object[1][5]{"E3:AS4",Color.BLUE,Color.RED,new JComboBox(new String[]{"Line","Bar"}),false};
//Creating the JTable model
DefaultTableModel model = new DefaultTableModel(patternData,columnNames);
//Creating the JTable using the model
JTable patterns = new JTable(model);
Cell With String Value That User Can Edit
DefaultCellEditor cellEditor = new DefaultCellEditor(new JTextField());
cellEditor.setClickCountToStart(1);
cellEditor.addCellEditorListener(new CellEditorListener(){
public void editingCanceled(ChangeEvent e)
{
System.out.println("Editing Canceled: "+e);
}
public void editingStopped(ChangeEvent e)
{
patterns.setValueAt(((DefaultCellEditor)e.getSource()).getCellEditorValue(),patterns.getSelectedRow(),0);
patternData[patterns.getSelectedRow()][0] = patterns.getValueAt(patterns.getSelectedRow(),0);
}
});
patterns.getColumnModel().getColumn(0).setCellEditor(cellEditor);
Cell That Contains It's Background Color
patterns.getColumnModel().getColumn(1).setCellRenderer(new TableCellRenderer()
{
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col)
{
Color colour = (Color) value;
JButton rendererComponent = new JButton();
rendererComponent.setBackground(colour);
return rendererComponent;
}
});
Cell That Contains JComboBox
patterns.getColumnModel().getColumn(3).setCellRenderer(new TableCellRenderer()
{
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col)
{
JComboBox rendererComponent = new JComboBox(new String[]{"Line","Bar"});
rendererComponent.setSelectedItem(value.toString());
return rendererComponent;
}
});
DefaultCellEditor cellEditor2 = new DefaultCellEditor(new JComboBox(new String[]{"Line","Bar"}));
cellEditor2.setClickCountToStart(1);
cellEditor2.addCellEditorListener(new CellEditorListener(){
public void editingCanceled(ChangeEvent e)
{
System.out.println("Editing Canceled: "+e);
}
public void editingStopped(ChangeEvent e)
{
System.out.println("Editing Stopped: "+((DefaultCellEditor)e.getSource()).getComponent());
patternData[patterns.getSelectedRow()][3] = patterns.getValueAt(patterns.getSelectedRow(),3);
System.out.println(patternData[patterns.getSelectedRow()][3]);
}
});
patterns.getColumnModel().getColumn(3).setCellEditor(cellEditor2);
Cell With CheckBox