I have implemented a JTree and populated some data. The table contains three columns and based on some values on a specific cell it should have either labels or combo boxes. All the values in the 3rd column are editable. I too have a JTree from which a node is selected and based on it the table values change accordingly. The issue exists when I edit a cell in the table and move to another node in the tree (which populates a new set of data in the table), the previously edited cell values exist in the table on top of the new cell values. Below is how I implemented TableCellRenderer and TableCellEditor. I might have used some concepts wrong since I am a beginner for swing. Please help me figure out what I have done incorrectly.
public void populateTableData(List<Field> list,JTree jTree){
fieldList = null;
tcBuilderTree = jTree;
fieldList = list;
md=new PropertiesTableModel(fieldList);
getPropertieseTable().setModel(md);
final TableCellRenderer cellRenderer = new TableCellRenderer() {
#Override
public Component getTableCellRendererComponent(JTable arg0,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int col) {
if(value instanceof List<?>) {
List<Value> valueList=(ArrayList)value;
return createComboBox(valueList);
}
else{
JLabel lbl=new JLabel();
lbl.setText((String)value);
return lbl;
}
}
};
propertiesTable.setDefaultRenderer(Object.class, cellRenderer);
final TableCellEditor cellEditor = new TableCellEditor() {
private DefaultCellEditor textEditor;
private DefaultCellEditor currentEditor;
#Override
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column) {
textEditor = new DefaultCellEditor(new JTextField());
PropertiesTableModel model = (PropertiesTableModel) table.getModel();
List<Value> values = model.getPossibleValues(row, column);
if (values != null) {
List<Value> valueList=(ArrayList)value;
currentEditor = new DefaultCellEditor(createComboBox(valueList));
} else {
currentEditor = textEditor;
}
return currentEditor.getTableCellEditorComponent(table, value,
isSelected, row, column);
}
#Override
public Object getCellEditorValue() {
return currentEditor.getCellEditorValue();
}
#Override
public boolean isCellEditable(EventObject anEvent) {
JTable tbl = (JTable) anEvent.getSource();
int row, col;
if (anEvent instanceof MouseEvent) {
MouseEvent evt = (MouseEvent) anEvent;
row = tbl.rowAtPoint(evt.getPoint());
col = tbl.columnAtPoint(evt.getPoint());
} else {
row = tbl.getSelectedRow();
col = tbl.getSelectedColumn();
}
if(col<2){
return false;
}
else
{
return true;
}
}
#Override
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
#Override
public boolean stopCellEditing() {
Object obj = currentEditor.getCellEditorValue();
fieldList.get(propertiesTable.getEditingRow())
.setDefaultValue(obj);
return currentEditor.stopCellEditing();
}
#Override
public void cancelCellEditing() {
currentEditor.cancelCellEditing();
}
#Override
public void addCellEditorListener(CellEditorListener l) {
}
#Override
public void removeCellEditorListener(CellEditorListener l) {
}
};
propertiesTable.setDefaultEditor(Object.class,cellEditor);
}
The issue exists when I edit a cell in the table and move to another node in the tree (which populates a new set of data in the table), the previously edited cell values exist in the table on top of the new cell values.
I would guess you are not stopping editing of the table cell before you reload the table with new data.
You probably should add the following to your code when you create the JTable:
JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
See Table Stop Editing for more information.
The table contains three columns and based on some values on a specific cell it should have either labels or combo boxes
You may not need to create custom editors. Just have the table choose the appropriate default editor based on the cell being edited. You can do this by overriding the getCellEditor(...) method of the JTable. Check out: How to add unique JComboBoxes to a column in a JTable (Java) for an example of this approach.
Related
I have a table with a checkbox column, I am able to show the table like i want and I make editable just the column where the checkbox is placed. The problem is when I select the checkbox the render paint in the right way the checkbox but the value in the tablemodel isn't changed, this value just change until i give click in another checkbox, always the last checkbox that I choose doesn't reflects his value in the TableModel. The classes that I am using are:
Checkcell
class CheckCell extends DefaultCellEditor implements TableCellRenderer{
private JComponent component = new JCheckBox();
private boolean value = false;
public CheckCell() {
super(new JCheckBox());
}
#Override
public Object getCellEditorValue() {
return ((JCheckBox)component).isSelected();
}
#Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
( (JCheckBox) component).setBackground( new Color(200,200,0) );
boolean b = ((Boolean) value).booleanValue();
( (JCheckBox) component).setSelected( b );
( (JCheckBox) component).setHorizontalAlignment(SwingConstants.CENTER);
return ( (JCheckBox) component);
}
#Override
public boolean stopCellEditing() {
value = ((Boolean)getCellEditorValue()).booleanValue() ;
((JCheckBox)component).setSelected( value );
return super.stopCellEditing();
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null)
return null;
return ( (JCheckBox) component );
}
}
CheckRender
class CheckRender extends JCheckBox implements TableCellRenderer {
private JComponent component = new JCheckBox();
public CheckRender() {
setOpaque(true);
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
((JCheckBox) component).setBackground( new Color(0,200,0));
boolean b = ((Boolean) value).booleanValue();
((JCheckBox) component).setSelected(b);
((JCheckBox) component).setHorizontalAlignment(SwingConstants.CENTER);
return ((JCheckBox) component);
}
}
My Model
class DynaTableModel extends DefaultTableModel {
public DynaTableModel() {
super();
}
public DynaTableModel(final DynaTableBean dynaBean) {
super(dynaBean.getContent(), dynaBean.getHeaders());
}
#Override
public boolean isCellEditable(int row, int col) {
if (col == 0)
{
return true;
} else {
return false;
}
}
#Override
public void setValueAt(Object value, int row, int column) {
super.setValueAt(value, row, column);
}
Could you help me with this? what is missing me to change the value in the jtable when this change in the checkbox.
I am new in java and I think that there are something that is missing me.
Thanks in advanced.
Being a little more explicity, when I ticked the checkbox and I tried to recover the value from the TableModel the value is wrong by example if I ticked the first checkbox and I tried to recover the value using:
valor = (Boolean) tablemodel.getValueAt(i, 0);
I am getting false when this should be true, but if I check the first checkbox and later the second one the value of the first checkbox now is fine(true) but the second is still false is a kind of bug but I don't find the way to avoid this.
Someone knows how to avoid this.
I have a table with a checkbox column, I am able to show the table
like i want and I make editable just the column where the checkbox is
placed. The problem is when I select the checkbox the render paint in
the right way the checkbox but the value in the tablemodel isn't
changed, this value just change until i give click in another
checkbox, always the last checkbox that I choose doesn't reflects his
value in the TableModel
JTable has built in support for JCheckBox as TableCellRenderer and Editor
Boolean value represens JCheckBox in XxxTableModel, you would need to put there true or false (1st column)
override public Class getColumnClass(int c) {,
I'm trying to build a table which includes JComboBoxes as both the renderer and editor components. This mostly works fine, however there are two things I can't seem to solve.
Tabbing between cells should make the JComboBox active
Clicking the drop-down arrow should immediately open the option list
Regarding 1, the editable combo should place focus within the embedded text field, the fixed combo should allow the down arrow to open the list of options.
Regarding 2, I find that this sometimes works depending on what other cell is currently active, but other times I have to double click. I cannot make this behaviour consistent.
For convenience I have included a clear example which (I believe) uses the recommended approach for embedding JComboBoxes within Jtables.
Thank you for constructive advice.
import java.awt.Component;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableCombos
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Frame");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
AbstractTableModel model = new DefaultTableModel()
{
String[] columnHeaders = new String[]{"label", "combo-edit", "combo-fixed"};
Class<?>[] columnClasses = new Class<?>[]{String.class, Double.class, Double.class};
List<Object[]> data = new ArrayList<>();
{
data.add(new Object[]{"row 1", 1.0d, 2.0d});
data.add(new Object[]{"row 2", 2.0d, 3.0d});
}
#Override
public int getColumnCount()
{
return columnHeaders.length;
}
#Override
public boolean isCellEditable(int row, int column)
{
return column != 0;
}
#Override
public int getRowCount()
{
if (data == null) // race condition
return 0;
return data.size();
}
#Override
public Object getValueAt(int row, int column)
{
return data.get(row)[column];
}
#Override
public void setValueAt(Object aValue, int row, int column)
{
data.get(row)[column] = aValue;
}
#Override
public Class<?> getColumnClass(int column)
{
return columnClasses[column];
}
#Override
public String getColumnName(int column)
{
return columnHeaders[column];
}
};
JTable table = new JTable(model);
table.setSurrendersFocusOnKeystroke(true);
TableColumn c1 = table.getColumnModel().getColumn(1);
TableColumn c2 = table.getColumnModel().getColumn(2);
JComboBox<Double> editorComboEditable = new JComboBox<>(new Double[]{1.0d, 2.0d, 3.0d});
editorComboEditable.setEditable(true);
c1.setCellEditor(new DefaultCellEditor(editorComboEditable));
c2.setCellEditor(new DefaultCellEditor(new JComboBox<>(new Double[]{1.0d, 2.0d, 3.0d})));
final JComboBox<Double> rendererComboEditable = new JComboBox<>(new Double[]{1.0d, 2.0d, 3.0d});
rendererComboEditable.setEditable(true);
final JComboBox<Double> rendererComboFixed = new JComboBox<>(new Double[]{1.0d, 2.0d, 3.0d});
c1.setCellRenderer(new TableCellRenderer()
{
#Override
public Component getTableCellRendererComponent(JTable t, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
rendererComboEditable.setSelectedItem(value);
return rendererComboEditable;
}
});
c2.setCellRenderer(new TableCellRenderer()
{
#Override
public Component getTableCellRendererComponent(JTable t, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
rendererComboFixed.setSelectedItem(value);
return rendererComboFixed;
}
});
frame.getContentPane().add(new JScrollPane(table));
frame.pack();
frame.setVisible(true);
}
}
Both of your issues be resolved by overriding the DefaultCellEditor#isCellEditable method and returning true
You may need to set JTable#setSurrendersFocusOnKeystroke to true as well
Update
The short answer is "It's messy". The long answer is "it's very messy"
I did a "continuos editing" process some time back. Basically, I overrode the Enter and Tab key bindings.
What I'd did was basically stop any active cell editor, take note of the current cell and then tried to finding the next editable cell, looping back around to the start (cell 0x0) if required.
When I found a editable cell, I called JTable#esitCellAt to start editing the cell.
To get the popup to be visible when the cell starts editing, you will need to override the addNotify method of the JComboBox and, using SwingUtilities#invokeLater, set the popup visible
I am having a JTable with two columns. In the second column there are different editors (JTextField, JComboBox and CheckComboBox), in each row one. This works fine so far however I have implemented a reset option which changes the whole JTable back to the original state (resets all changes).
The problem I am facing now is that when I programmatical change the index of a ComboBox with setSelectedIndex I see no result in the GUI although the model fires its change with fireTableDataChanged and is also receivied by an TableModelListener of the Table. When I lookup the changed ComboBox I also get the correct index however it is not shown in the GUI. I also tried the methods revalidate, updateUI and repaint wihtout any change.
The problem might lay in the architecture of it (maybe the Renderer?). Here is my Editor/Renderer class.
class VEachRowEditor implements TableCellEditor, TableCellRenderer {
protected Hashtable<Integer, TableCellEditor> editors;
protected TableCellEditor editor, defaultEditor, renderer;
JTable table;
VEachRowEditorManager rowmanager;
public VEachRowEditor(JTable table, VEachRowEditorManager rowmanager) {
this.table = table;
editors = new Hashtable<Integer, TableCellEditor>();
defaultEditor = new DefaultCellEditor(new JTextField());
this.rowmanager = rowmanager;
}
public void setEditorAt(int row, TableCellEditor editor) {
if (editor instanceof DefaultCellEditor)
((DefaultCellEditor) editor).setClickCountToStart(1);
editors.put(new Integer(row), editor);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
editor = (TableCellEditor) editors.get(new Integer(row));
if (editor == null) {
editor = defaultEditor;
}
return editor.getTableCellEditorComponent(table, value, isSelected,
row, column);
}
public Object getCellEditorValue() {
return editor.getCellEditorValue();
}
public final boolean stopCellEditing() {
return editor.stopCellEditing();
}
public void cancelCellEditing() {
editor.cancelCellEditing();
}
public boolean isCellEditable(EventObject anEvent) {
selectEditor((MouseEvent) anEvent);
return editor.isCellEditable(anEvent);
}
public void addCellEditorListener(CellEditorListener l) {
editor.addCellEditorListener(l);
}
public void removeCellEditorListener(CellEditorListener l) {
editor.removeCellEditorListener(l);
}
public boolean shouldSelectCell(EventObject anEvent) {
selectEditor((MouseEvent) anEvent);
return editor.shouldSelectCell(anEvent);
}
protected void selectEditor(MouseEvent e) {
int row;
if (e == null) {
row = table.getSelectionModel().getAnchorSelectionIndex();
} else {
row = table.rowAtPoint(e.getPoint());
}
editor = (TableCellEditor) editors.get(new Integer(row));
if (editor == null) {
System.out.println(editor);
editor = defaultEditor;
}
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
renderer = (TableCellEditor) editors.get(new Integer(row));
if (renderer == null) {
renderer = defaultEditor;
}
return renderer.getTableCellEditorComponent(table, value, isSelected,
row, column);
}
}
Is the getTableCellEditorComponent wrong?
The rowmanager holds all the JComboBoxes and CheckComboBoxes with all the models.
when I programmatical change the index of a ComboBox with setSelectedIndex I see no result in the GUI
Renderer and editers just display the data in the model. Don't reset the editor component.
Reset the data in the model. ie>
table.setValueAt(...); // or
table.getModel().setValueAt(...);
I need to display an image in one of jTable cells.
I wrote this:
class ImageRenderer extends DefaultTableCellRenderer {
JLabel lbl = new JLabel();
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
lbl.setText((String) value);
lbl.setIcon(new ImageIcon("/home/ariyan/Desktop/71290452.jpg"));
return lbl;
}
}
and then used it as this:
jTable1.getColumn(0).setCellRenderer(new ImageRenderer());
But this didn't work
How I can do that?
Thanks
JTable already provides a default renderer for images. You just need to tell the table what type of data is contained in each column and it will choose the best renderer:
a) override the getColumnClass() method of the JTable or the TableModel to return the class of data in the column. In this case you should return an Icon.class.
b) add an ImageIcon to the table model.
Now the JTable will use the default Icon renderer for that column.
Hmm: jTable1.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer()); perhaps?
Here's the relevant extract of some quick test code I put together to quickly verify my guess. It displays icons from a folder (it assumes all files in a folder are icons, so you should test it with something like an XDG icon theme sub directory). Install table model first then add the cell renderer on the columns:
class Renderer extends DefaultTableCellRenderer {
#Override
public Component getTableCellRendererComponent (JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row, int column) {
if(isSelected) {
this.setBackground(table.getSelectionBackground());
this.setForeground(table.getSelectionForeground());
}
else {
this.setBackground(table.getBackground());
this.setForeground(table.getForeground());
}
if(column == 0) {
this.setText(list[row]);
}
else {
// edit as appropriate for your icon theme
this.setIcon(new ImageIcon("/usr/share/icons/default.kde4/16x16/apps/"+list[row]));
}
return this;
}
}
class Model extends DefaultTableModel {
#Override
public boolean isCellEditable (int row, int column) {
return false;
}
#Override
public Object getValueAt (int row, int column) {
return list[row];
}
#Override
public int getRowCount () {
return list.length;
}
#Override
public int getColumnCount () {
return 2;
}
#Override
public String getColumnName (int column) {
return column == 0? "Name" : "Preview";
}
#Override
public Class<?> getColumnClass (int columnIndex) {
return String.class;
}
}
// edit base directory as appropriate for your icon theme of choice
static String[] list=new File("/usr/share/icons/default.kde4/16x16/apps/").list();
I want to put individual JComboBoxes into each cells of a JTable. ie. The JComboBox content is not identical for each cell.
I basically would like to be able to just call the following code to add a row of JComboBox into the JTable. Anyone has any idea? Thanks
JComboBox cb1 = new JComboBox(...);
JComboBox cb2 = new JComboBox(...);
model.addRow(new Object[] {"Row name", cb1, cb2} );
JComboBox cb3 = new JComboBox(...);
JComboBox cb4 = new JComboBox(...);
model.addRow(new Object[] {"Row name 2", cb3, cb4} );
The closest example code I can find is as follows. But it is for where JComboBox content is identical for the individual column. Not the solution I need.
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellEditor(new MyComboBoxEditor(values));
where
public class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}
Extend JTable with this code:
#Override
public TableCellEditor getCellEditor(int row, int column) {
Object value = super.getValueAt(row, column);
if(value != null) {
if(value instanceof JComboBox) {
return new DefaultCellEditor((JComboBox)value);
}
return getDefaultEditor(value.getClass());
}
return super.getCellEditor(row, column);
}
This will create a unique JComboBox cell editor for each combo box you get the a value for.
I am sure this will solve your problem. Mention in which column you need to set the combo box in .getColumn(int column)
private void addComboToTable(JComboBox combo) {
TableColumn gradeColumn = YourTable.getColumnModel().getColumn(0);
JComboBox comboBox = combo;
comboBox.removeAllItems();
try {
comboBox.addItem("Item 1");
comboBox.addItem("Item 2");
comboBox.addItem("Item 3");
} catch (NullPointerException e) {
} catch (Exception e) {
e.printStackTrace();
}
gradeColumn.setCellEditor(new DefaultCellEditor(comboBox));
}
You need to override:
Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
...in TableCellEditor. The value passed in to this method is what you can put in your JComboBox. That means that the 'value' for that particular cell needs to be something that can be translated into a collection. It could potentially just be a List of objects or it could be a POJO with fields that could be made into a JComboBox.
So just edit MyComboBoxEditor to override that method and change your model to allow for an Object that actually represents several other objects.
The JComboBox content is render identical for each row selection because
the JTable does not offer the capability to have more than one editor per column.
You have to extend the JTable class to support an additional selection for rows.
This article explains it very well:
http://www.javaworld.com/javaworld/javatips/jw-javatip102.html
In addition to cellEditor it is necessary to do the cellRenderer to paint the combobox in the cell, look at this:
public void example(){
TableColumn tmpColum =table.getColumnModel().getColumn(1);
String[] DATA = { "Data 1", "Data 2", "Data 3", "Data 4" };
JComboBox comboBox = new JComboBox(DATA);
DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox);
tmpColum.setCellEditor(defaultCellEditor);
tmpColum.setCellRenderer(new CheckBoxCellRenderer(comboBox));
table.repaint();
}
/**
Custom class for adding elements in the JComboBox.
*/
class CheckBoxCellRenderer implements TableCellRenderer {
JComboBox combo;
public CheckBoxCellRenderer(JComboBox comboBox) {
this.combo = new JComboBox();
for (int i=0; i<comboBox.getItemCount(); i++){
combo.addItem(comboBox.getItemAt(i));
}
}
public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
combo.setSelectedItem(value);
return combo;
}
}
#Override
public TableCellEditor getCellEditor(int row, int column) {
Object value = super.getValueAt(row, column);
if(value != null) {
if(value instanceof JComboBox) {
return new DefaultCellEditor((JComboBox)value);
}
return getDefaultEditor(value.getClass());
}
return super.getCellEditor(row, column);
}
And then, override the toString method from JComboBox.
This page might help you, although it seems you are restricted to having the same combobox in all the cells in a column.
You need to create a subclass of JTable to override the method TableCellEditor getCellEditor(int row, int column).
This enables you to set arbitrary cell editors for any row and column combination. The default way is to set the cell editor for an entire column.
(You can also set individual cell renderers by overriding getCellRenderer.)
The easiest way is to implement your own TableModel
public class MyModel extends AbstractTableModel {
List rows;
public int getRowCount() {
return rows.size();
}
public int getColumnCount() {
return 4;
}
public Object getValueAt(int row, int column) {
return rows.get(row).getCol(col); //assuming your row "Object" has a getCol()
}
public Class<?> getColumnClass(int col) {
return Boolean.class;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
rows.get(rowIndex).getCol(columnIndex).setValue(aValue);
}
}
Load this into you JTable. If you haven't replaced the default cell renderer for Boolean's, all you cells will be rendered as check boxes thanks to you implementation of getColumnClass(). All user input to these check boxes is collected with our setValueAt().