JTable Updating String Values from User Input
I am having issues with my JTable saving values that the user inputs in the cells. At first glance, it would seem that the data has been saved, but when you attempt to retrieve the data or reload the table, the data goes back to its previous value.
Basically, the user would input data, press enter or move on to a different cell or something, and the data should get saved into the data array. It seems the data is just getting saved visually, but not in any real memory.
Can anyone give any tips?
Re: JTable Updating String Values from User Input
My guess is that you aren't actually saving the data (using the TableModel's function setValueAt()), but without a short example that demos the problem its just a guess.
Re: JTable Updating String Values from User Input
How do I use the setValueAt() function when I cant see what data the user enters?
Re: JTable Updating String Values from User Input
Quote:
Originally Posted by
aussiemcgr
How do I use the setValueAt() function when I cant see what data the user enters?
The parameters for the setValueAt function include the row, column and an Object. In most cases where the table displays string, this object is simply a String value. In more complex cases you need to cast this object to the type of class the column contains. Either way, this object represents the user entered value, and you can use it to set the data model.
Re: JTable Updating String Values from User Input
Ok, well here is something I didnt know.
The setValueAt function DOESNT change the value in the data array. Which means I had to manually do it myself. I did this:
Code java:
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);
And it works.
Re: JTable Updating String Values from User Input
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:
Code java:
//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
Code java:
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
Code java:
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
Code java:
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