I am currently writing some code to implement spinners into table cells. So far I have managed to do that however there is still one thing I could not fix.
If I select a spinner cell and enter a new value using my keyboard then click at another cell, the first spinner cell's value does not update. If I do the same thing but instead of clicking another cell I press Enter, the value of the cell gets updated.
I originally asked this question in Sun's forum but it's been a while no response so I decided to come to this forum. This is the link to the original post: Swing - Spinner Table Cells
And here's the code:
Thank you very much for your help,
LD
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
/**
* Implement this class to create a column containing spinner cells in a JTable
*/
public class SpinnerColumn extends AbstractCellEditor
implements TableCellEditor, TableCellRenderer
{
private JSpinner editSpinner, renderSpinner;
private JTable table;
private String[] list;
private Border originalBorder;
private Object oldValue, newValue;
private int edittingRow, edittingCol;
public SpinnerColumn(JTable table, int column)
{
edittingRow = -1;
edittingCol = -1;
editSpinner = new JSpinner();
renderSpinner = new JSpinner();
originalBorder = editSpinner.getBorder();
editSpinner.setBorder(new LineBorder(Color.BLUE));
this.table = table;
table.getColumnModel().getColumn(column).setCellEditor(this);
table.getColumnModel().getColumn(column).setCellRenderer(this);
}
public SpinnerColumn(String[] list, JTable table, int column)
{
editSpinner = new JSpinner();
editSpinner.setModel(new SpinnerListModel(list));
renderSpinner = new JSpinner();
renderSpinner.setModel(new SpinnerListModel(list));
originalBorder = editSpinner.getBorder();
editSpinner.setBorder(new LineBorder(Color.BLUE));
this.list = list;
this.table = table;
table.getColumnModel().getColumn(column).setCellEditor(this);
table.getColumnModel().getColumn(column).setCellRenderer(this);
}
public Object getCellEditorValue()
{
return editSpinner.getValue();
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column)
{
// Show the first value when a spinner cell is chosen,
// either first element of list or 0
if (list != null) {
editSpinner.setValue(list[0]);
} else {
editSpinner.setValue(0);
}
if (value != null) {
editSpinner.setValue(value);
}
return editSpinner;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
if (hasFocus) {
renderSpinner.setBorder(new LineBorder(Color.BLUE));
} else {
renderSpinner.setBorder(originalBorder);
}
// *** here's where we set the spinner's value
if (value == null) {
if (list != null) {
renderSpinner.setValue(list[0]);
} else {
renderSpinner.setValue(0);
}
} else {
renderSpinner.setValue(value);
}
return renderSpinner;
}
public boolean isCellEditable(EventObject evt)
{
return true;
}
// main method for testing purposes only
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new JFrame("SpinnerColumn");
JPanel panel = new JPanel(new GridLayout(1, 1));
JTable table = new JTable(5, 1);
String[] items = {"dude", "gal", "rock"};
SpinnerColumn spinnerColumn = new SpinnerColumn(table, 0);
// SpinnerColumn spinnerColumn = new SpinnerColumn(items, table, 0);
panel.add(table);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}


Sign In
Create Account


Back to top









