How to make only one checkbox selectable in JTable Column - java

I am using DefaultTableModel as follows:
DefaultTableModel model = new DefaultTableModel (COLUMNS, 0 )
{
#Override
public boolean isCellEditable(int row, int column)
{
return (getColumnName(column).equals("Selected"));
}
public Class getColumnClass(int columnIndex)
{
if(getColumnName(columnIndex).equals("Selected"))
return Boolean.class;
return super.getColumnClass(columnIndex);
}
};
Now I want to make only one checkbox selectable in the column "Selected". How can this be done. I have tried following method also but its not working.
public void fireTableCellUpdated(int row,int column)
{
if(getColumnName(column).equals("Selected"))
{
for(int i = 0; i<getRowCount() && i!=row;i++)
setValueAt(Boolean.FALSE, row, column);
}
}

#eatSleepCode wrote #mKorbel can you please give example code for implementing setValueAt method.
code for (OP used) DefaultTableModel,
for code based on AbstractTableModel is required to hold code ordering for notifier fireTableCellUpdated(rowIndex, columnIndex);, because/otherwise nothing will be repainted in JTables view,
there are a few important differencies betweens those two models and its notifiers, and (my view) there isn't reason to bothering with and to use AbstractTableModel for basic stuff (99pct of table models)
. . . . . . . .
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class TableRolloverDemo {
private JFrame frame = new JFrame("TableRolloverDemo");
private JTable table = new JTable();
private String[] columnNames = new String[]{"Column"};
private Object[][] data = new Object[][]{{false}, {false}, {true}, {true},
{false}, {false}, {true}, {true}, {false}, {false}, {true}, {true}};
public TableRolloverDemo() {
final DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private boolean ImInLoop = false;
#Override
public Class<?> getColumnClass(int columnIndex) {
return Boolean.class;
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == 0) {
if (!ImInLoop) {
ImInLoop = true;
Boolean bol = (Boolean) aValue;
super.setValueAt(aValue, rowIndex, columnIndex);
for (int i = 0; i < this.getRowCount(); i++) {
if (i != rowIndex) {
super.setValueAt(!bol, i, columnIndex);
}
}
ImInLoop = false;
}
} else {
super.setValueAt(aValue, rowIndex, columnIndex);
}
}
};
table.setModel(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TableRolloverDemo tableRolloverDemo = new TableRolloverDemo();
}
});
}
}

You get an stack overflow exception because setValueAt() method triggers fireTableCellUpdated() method once again and again.
Instead, try using a table listener which would listen to check box's value change and would set all other check boxes' value to false.

You can create your own custom cell editor that joins all check boxes in a column in a ButtonGroup. here's how:
public class VeryComplicatedCellEditor extends DefaultCellEditor {
private ArrayList<ButtonGroup> groups;
public getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
JCheckBox checkBox = new JCheckBox();
growToSize(column);
groups.get(column).add(checkBox);
return checkBox;
}
private growToSize(int size) {
groups.ensureCapacity(size);
while (groups.size() < size)
groups.add(new ButtonGroup());
}
}
There are some complications that come from the fact that we don't know how big the table is, which are mostly taken care of in the growToSize method.
The way this works is by maintaining a list of ButtonGroups, one for each column. The editor component for each cell is added to the button group for its column.

Related

How to make JTable's header be highlighted when column selection? [duplicate]

i'm currently building a little JTable, and want to highlight the column header (and row headers - the row-header part is actually working) when a cell is selected to make it easier to find the associated names with this cell. Here is a picture:
I already tried switching out the renderer for the header with this:
table.getTableHeader().setDefaultRenderer(new ColumnHeaderRenderer());
But it's only called when i click on the header and always says isSelected is false.
This is the code i use for the row-names, including the highlight inside the renderer - code is not by me, i just modified it a little:
/*
* Use a JTable as a renderer for row numbers of a given main table.
* This table must be added to the row header of the scrollpane that
* contains the main table.
*/
public class RowNameTable extends JTable
implements ChangeListener, PropertyChangeListener {
private JTable main;
public RowNameTable(JTable table) {
main = table;
main.addPropertyChangeListener(this);
setFocusable(false);
setAutoCreateColumnsFromModel(false);
setModel(main.getModel());
setSelectionModel(main.getSelectionModel());
TableColumn column = new TableColumn();
column.setHeaderValue(" ");
addColumn(column);
column.setCellRenderer(new RowNameRenderer(main));
getColumnModel().getColumn(0).setPreferredWidth(table.getColumnModel().getColumn(0).getPreferredWidth());
setPreferredScrollableViewportSize(getPreferredSize());
}
#Override
public void addNotify() {
super.addNotify();
Component c = getParent();
// Keep scrolling of the row table in sync with the main table.
if (c instanceof JViewport) {
JViewport viewport = (JViewport) c;
viewport.addChangeListener(this);
}
}
/*
* Delegate method to main table
*/
#Override
public int getRowCount() {
return main.getRowCount();
}
#Override
public int getRowHeight(int row) {
return main.getRowHeight(row);
}
/*
* This table does not use any data from the main TableModel,
* so just return a value based on the row parameter.
*/
#Override
public Object getValueAt(int row, int column) {
return Integer.toString(row + 1);
}
/*
* Don't edit data in the main TableModel by mistake
*/
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
//
// Implement the ChangeListener
//
public void stateChanged(ChangeEvent e) {
// Keep the scrolling of the row table in sync with main table
JViewport viewport = (JViewport) e.getSource();
JScrollPane scrollPane = (JScrollPane) viewport.getParent();
scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
}
//
// Implement the PropertyChangeListener
//
public void propertyChange(PropertyChangeEvent e) {
// Keep the row table in sync with the main table
if ("selectionModel".equals(e.getPropertyName())) {
setSelectionModel(main.getSelectionModel());
}
if ("model".equals(e.getPropertyName())) {
setModel(main.getModel());
}
}
/*
* Borrow the renderer from JDK1.4.2 table header
*/
private static class RowNameRenderer extends DefaultTableCellRenderer {
private JTable main;
public RowNameRenderer(JTable main) {
this.main = main;
setHorizontalAlignment(JLabel.CENTER);
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (table != null) {
JTableHeader header = table.getTableHeader();
if (header != null) {
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}
}
if (isSelected) {
setFont(getFont().deriveFont(Font.BOLD));
}
setText((value == null) ? "" : main.getColumnName(row));
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return this;
}
}
}
And here we have the relevant part to create the table:
costTableModel = new CostTableModel(costCalc);
table = new JTable(costTableModel);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setCellSelectionEnabled(true);
scrollPane = new JScrollPane(table);
RowNameTable nameTable = new RowNameTable(table);
scrollPane.setRowHeaderView(nameTable);
And the class costTableModel, just for completeness sake:
public class CostTableModel extends AbstractTableModel {
private CostCalculator costCalc;
public CostTableModel(CostCalculator costCalc) {
this.costCalc = costCalc;
}
#Override
public int getRowCount() {
return costCalc.getPersonsList().size();
}
#Override
public int getColumnCount() {
return costCalc.getPersonsList().size();
}
#Override
public String getColumnName(int col) {
return costCalc.getPersonsList().get(col).getName();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Person debtor = costCalc.getPersonsList().get(rowIndex);
Person debtee = costCalc.getPersonsList().get(columnIndex);
return costCalc.getAmountOwed(debtor, debtee);
}
#Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
Thank you for your help in advance!
A slight variant: as I read the question, the main problem is the header not updating on column selection change. Having a custom header listen to row selection changes doesn't help much for that scenario.
In fact, a JTableHeader already is listening to the ColumnModel and the model's change notification includes selection changes. Only the columnSelectionChange method is intentionally implemented to do nothing:
// --Redrawing the header is slow in cell selection mode.
// --Since header selection is ugly and it is always clear from the
// --view which columns are selected, don't redraw the header.
A custom header can simply implement to repaint (here lazy me does it in the table's factory method just to spare me the wiring to the table, you can easily make it a stand-alone class :-).
final JTable table = new JTable(new AncientSwingTeam()) {
#Override
protected JTableHeader createDefaultTableHeader() {
// subclassing to take advantage of super's auto-wiring
// as ColumnModelListener
JTableHeader header = new JTableHeader(getColumnModel()) {
#Override
public void columnSelectionChanged(ListSelectionEvent e) {
repaint();
}
};
return header;
}
};
table.setCellSelectionEnabled(true);
table.getTableHeader().setDefaultRenderer(new ColumnHeaderRenderer());
Also tweaked Mad's renderer a bit, using table api:
/**
* Slightly adjusted compared to #Mad
* - use table's selectionBackground
* - use table's isColumnSelected to decide on highlight
*/
public static class ColumnHeaderRenderer extends DefaultTableCellHeaderRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean selected,
boolean focused, int row, int column) {
super.getTableCellRendererComponent(table, value, selected, focused, row, column);
if (table.isColumnSelected(column)) {
setBackground(table.getSelectionBackground());
}
return this;
}
}
As to the observation:
always says isSelected is false
The reason is a slight quirk in BasicTableHeaderUI:
ui.selected != columnModel.selected
uiSelected is the column which will be accessible to keybindings - if the laf supports it and the header is focusOwner. Doesn't really make sense to me, but fully defining the semantics of ui and columnModel selection fell into the excitement about the new babe fx, that is got forgotten ;-)
The basic issue I had was there was no connection between the table header and the selection change. In fact, the header is really clever with it's repaints...
I ended up providing my own header, which attached a listener to the table's selection model and repainted the header on the selection changed.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.List;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.RowSorter.SortKey;
import static javax.swing.SortOrder.ASCENDING;
import static javax.swing.SortOrder.DESCENDING;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class TestColumnHighlight {
public static void main(String[] args) {
new TestColumnHighlight();
}
public TestColumnHighlight() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel(
new Object[]{"abc", "def", "ghi", "jkl"},
0);
model.addRow(new Object[]{0, 0, 0, 0});
model.addRow(new Object[]{0, 0, 0, 0});
model.addRow(new Object[]{0, 0, 0, 0});
model.addRow(new Object[]{0, 0, 0, 0});
model.addRow(new Object[]{0, 0, 0, 0});
table.setModel(model);
table.setTableHeader(new CustomTableHeader(table));
table.getTableHeader().setDefaultRenderer(new ColumnHeaderRenderer());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class CustomTableHeader extends JTableHeader {
public CustomTableHeader(JTable table) {
super();
setColumnModel(table.getColumnModel());
table.getColumnModel().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
repaint();
}
});
}
#Override
public void columnSelectionChanged(ListSelectionEvent e) {
repaint();
}
}
public class ColumnHeaderRenderer extends DefaultTableHeaderCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean selected, boolean focused, int row, int column) {
super.getTableCellRendererComponent(table, value, selected, focused, row, column);
int selectedColumn = table.getSelectedColumn();
System.out.println("Selected " + selectedColumn + "-" + column);
if (selectedColumn == column) {
Color bg = table.getSelectionBackground();
setBackground(bg);
setOpaque(true);
} else {
setOpaque(false);
}
return this;
}
}
public class DefaultTableHeaderCellRenderer extends DefaultTableCellRenderer {
public DefaultTableHeaderCellRenderer() {
setHorizontalAlignment(CENTER);
setHorizontalTextPosition(LEFT);
setVerticalAlignment(BOTTOM);
setOpaque(false);
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
JTableHeader tableHeader = table.getTableHeader();
if (tableHeader != null) {
setForeground(tableHeader.getForeground());
}
setIcon(getIcon(table, column));
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
return this;
}
protected Icon getIcon(JTable table, int column) {
SortKey sortKey = getSortKey(table, column);
if (sortKey != null && table.convertColumnIndexToView(sortKey.getColumn()) == column) {
switch (sortKey.getSortOrder()) {
case ASCENDING:
return UIManager.getIcon("Table.ascendingSortIcon");
case DESCENDING:
return UIManager.getIcon("Table.descendingSortIcon");
}
}
return null;
}
protected SortKey getSortKey(JTable table, int column) {
RowSorter rowSorter = table.getRowSorter();
if (rowSorter == null) {
return null;
}
List sortedColumns = rowSorter.getSortKeys();
if (sortedColumns.size() > 0) {
return (SortKey) sortedColumns.get(0);
}
return null;
}
}
}

Make a JCheckBox in a JTable editable

I need some help with my JTable. I am writing a program, wich extracts data from a database into a JTable. The first column should be a editable JCheckBox so I am able to work with the checked (true or false) rows and the data.
I am using a AbstractTableModel(with class extends AbstractTableModel) and override these five methods:
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 0;
}
#Override
public Class<?> getColumnClass(int col) {
if (col == 0) {
return Boolean.class;
}
return super.getColumnClass(col);
}
#Override
public int getColumnCount() {
return header.length;
}
#Override
public int getRowCount() {
return data.length;
}
#Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
To display the JTable I use:
JTable table = new JTable();
JScrollPane scrollpane = new JScrollPane();
.
.
.
table = new JTable(data, header);
table.setModel(this);
scrollpane = new JScrollPane(table);
I read the data with a for loop into the data array. The header array I defined.
Basically I need the checked rows to send a mail with the right data in it.
EDIT:
package test;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class TestCode extends AbstractTableModel {
private static final long serialVersionUID = -7051817393770003705L;
String[] header = {"", "header", "header", "header"};
Object[][] data = {{new Boolean(false), "Text", "Text", "Text"}, {new Boolean(false), "Text", "Text", "Text"}, {new Boolean(false), "Text", "Text", "Text"}};
public TestCode() {
JFrame frame = new JFrame();
JTable table = new JTable(data, header);
table.setModel(this);
JScrollPane scrollpane = new JScrollPane(table);
frame.add(scrollpane);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
#Override
public boolean isCellEditable(int row, int col) {
return col == 0;
}
#Override
public Class<?> getColumnClass(int col) {
if (col == 0) {
return Boolean.class;
}
return super.getColumnClass(col);
}
#Override
public int getColumnCount() {
return header.length;
}
#Override
public int getRowCount() {
return data.length;
}
#Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
public static void main(String[] args) {
TestCode code = new TestCode();
}
}
This is a short snippet of my code to execute to make it easier for you. I want be able to check the JCheckBoxes at the firt column so I am able to read a true or false boolen from that column.
Thank you for help!
You have to override AbstractTableModel#setValueAt
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
// super.setValueAt(aValue, rowIndex, columnIndex); by default empty implementation is not necesary if direct parent is AbstractTableModel
data[rowIndex][columnIndex] = aValue;
fireTableCellUpdated(rowIndex, columnIndex);// notify listeners
}
Result.
BTW : Don't use new Boolean(false) instead use Boolean.FALSE
Okay leaving aside the structure of the code (I agree that the creation of the table should not be done in the model and things should be separated out better) the reason this is not working is that your table model does not implement setValueAt(Object value, int row, int column).
So when you click on a cell, that method is called, but your data array is not updated so the value for the cell is always false.
Adding
#Override
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
}
to your model means the table behaves as you would expect
Simply add implementation of value setter
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
data[rowIndex][columnIndex] = aValue;
}

JTable valuechanged then change cell color

I have here a JTable with two(2) columns. The right column is an editable one while the other is not.
So, what my problem is that whenever the user changed the value of a cell, that specific cell will changed its cell color.
I wanna do this because I want to let the user know that he/she made some changes in the table.
I found this somewhere and it somehow solved my problem but 1 thing that didn't come up with my expectation is that after changing the value and clicked another cell, the color changes back to its original color. I want to let it stay until it is saved.
#Override
public Component prepareEditor(TableCellEditor editor, int data, int columns) {
Component c = super.prepareEditor(editor, data, columns);
c.setBackground(Color.RED);
return c;
}
Is it possible? If yes, please show some example.
UPDATE:
String[] columnname = {"Student Name", "Grade"};
Object[][] data = {};
gradetable = new JTable(data, columnname){
private Object[][] rowData;
public boolean isCellEditable(int data, int columns){
return columns == 1;
}
public Component prepareRenderer(TableCellRenderer r, int data, int columns){
final Component c = super.prepareRenderer(r, data, columns);
if (data % 2 == 0){
c.setBackground(Color.LIGHT_GRAY);
}
else{
c.setBackground(Color.WHITE);
}
if (isCellSelected(data, columns)){
c.setBackground(Color.ORANGE);
}
return c;
}
#Override
public Component prepareEditor(TableCellEditor editor, int data, int columns) {
Component c = super.prepareEditor(editor, data, columns);
c.setBackground(Color.RED);
return c;
}
};
gradetable.setModel(new DefaultTableModel(data, columnname));
gradetable.setPreferredScrollableViewportSize(new Dimension (350, 130));
gradetable.setFillsViewportHeight(true);
gradetable.getTableHeader().setReorderingAllowed(false);
gradetable.setGridColor(new Color(128,128,128,128));
JScrollPane jsp = new JScrollPane(gradetable);
panel3.add(jsp);
Tables use a TableCellRenderer to paint values on the screen. The editors and renderers don't actually have anything to do with each other (from a painting point of view).
So once the editor has been dismissed (accepted or cancelled), the cell is repainted using the assigned TableCellRenderer
You need to supply, in your table model, some way to determine which rows have been updated and change the state of the renderer to match.
FYI- The DefaultTableCellRenderer uses a JLabel as it's base renderer, so it is transparent by default; you will need to make it opaque to make it render properly.
Check out Using custom renderers for more details
Update with example
This is nothing more then a proof of concept. It will not meet your absolute requirements and you should take a serious look at the tutorial linked above.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class TableEdit {
public static void main(String[] args) {
new TableEdit();
}
public TableEdit() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JTable table = new JTable(new MyTableModel());
table.setSurrendersFocusOnKeystroke(true);
TableColumnModel model = table.getColumnModel();
model.getColumn(1).setCellRenderer(new MyTableCellRenderer());
add(new JScrollPane(table));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class MyData {
private String key;
private String value;
private boolean changed;
public MyData(String key, String value) {
this.key = key;
this.value = value;
this.changed = false;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
public void setValue(String newValue) {
if (value == null ? newValue != null : !value.equals(newValue)) {
value = newValue;
changed = true;
}
}
public boolean hasChanged() {
return changed;
}
}
public class MyTableModel extends AbstractTableModel {
private List<MyData> data;
public MyTableModel() {
data = new ArrayList<>(25);
for (int index = 0; index < 5; index++) {
data.add(new MyData("A" + (index + 1), "B" + (index + 1)));
}
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public int getColumnCount() {
return 2;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
MyData myData = data.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = myData.getKey();
break;
case 1:
value = myData.getValue();
break;
}
return value;
}
#Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
public boolean hasChanged(int rowIndex) {
MyData myData = data.get(rowIndex);
return myData.hasChanged();
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
MyData myData = data.get(rowIndex);
myData.setValue(aValue == null ? null : aValue.toString());
}
}
public class MyTableCellRenderer extends DefaultTableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setOpaque(isSelected);
TableModel model = table.getModel();
if (model instanceof MyTableModel) {
MyTableModel myModel = (MyTableModel) model;
if (myModel.hasChanged(row)) {
if (!isSelected) {
setBackground(Color.RED);
setOpaque(true);
}
}
}
return this;
}
}
}

How can I select a table row with a single click using a custom TableCellEditor?

I have a JTable with a custom TableCellRenderer and a custom TableCellEditor. By default, the first click on a table row switch from renderer to editor and the second click select the row.
Is there any way I can make the row selected on a single click (and swith to the editor)?
I have tried to use:
table.getSelectionModel().setSelectionInterval(row, row);
in my getTableCellEditorComponent but it doesn't work, and if I add it to my getTableCellRendererComponent it works, but only sometimes.
Here is a full example:
public class SelectRowDemo extends JFrame {
public SelectRowDemo() {
CellRendererAndEditor rendererAndEditor = new CellRendererAndEditor();
StringTableModel model = new StringTableModel();
JTable table = new JTable(model);
table.setDefaultEditor(String.class, rendererAndEditor);
table.setDefaultRenderer(String.class, rendererAndEditor);
model.addElement("");
model.addElement("");
model.addElement("");
add(new JScrollPane(table));
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
new SelectRowDemo();
}
});
}
class CellRendererAndEditor extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
private final JLabel renderer = new JLabel();
private final JLabel editor = new JLabel();
#Override
public Object getCellEditorValue() {
return editor.getText();
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
String str = "renderer ";
str += (isSelected) ? "selected" : "not selected";
renderer.setText(str);
return renderer;
}
#Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
table.getSelectionModel().setSelectionInterval(row, row);
String str = "editor ";
str += (isSelected) ? "selected" : "not selected";
editor.setText(str);
return editor;
}
}
class StringTableModel extends AbstractTableModel {
private final List<String> data = new ArrayList<String>();
#Override
public int getColumnCount() {
return 1;
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public Object getValueAt(int row, int column) {
return data.get(row);
}
#Override
public Class<?> getColumnClass(int column) {
return String.class;
}
#Override
public boolean isCellEditable(int row, int column) {
return true;
}
#Override
public void setValueAt(Object aValue, int row, int column) {
if(aValue instanceof String) {
data.set(row, (String)aValue);
fireTableRowsUpdated(row, column);
} else throw new IllegalStateException("aValue is not a String");
}
public void addElement(String s) {
data.add(s);
}
}
}
What's happening is that the table UI is starting to edit the cell before changing selection. If you put a println in getTableCellEditor() and shouldSelectCell(), the editor gets called first, with isSelected == false, then it calls shouldSelectCell and changes the selection.
You can see exactly where it happens in BasicTableUI.adjustSelection(MouseEvent).
boolean dragEnabled = table.getDragEnabled();
if (!dragEnabled && !isFileList && table.editCellAt(pressedRow, pressedCol, e)) {
setDispatchComponent(e);
repostEvent(e);
}
CellEditor editor = table.getCellEditor();
if (dragEnabled || editor == null || editor.shouldSelectCell(e)) {
table.changeSelection(pressedRow, pressedCol,
BasicGraphicsUtils.isMenuShortcutKeyDown(e),
e.isShiftDown());
}
As for rendering purposes, I'd just render it as if selected == true, since it will before that event is finished processing.

Making JTable cells uneditable

I am trying to make all the cells of a JTable uneditable when double clicked by the user. I have read a lot of forum posts and the general consensus is to create a new table model class, extend DefaultTableModel and then override method isCellEditable(int row, int column). I did all of this and now when I run my program (applet) Nothing shows up in the cells. NOTE I have a prof this semester that does not think applets are outdated...
Code for the table model:
public class MyTableModel extends DefaultTableModel
{
public boolean isCellEditable(int row, int column) //override isCellEditable
//PRE: row > 0, column > 0
//POST: FCTVAL == false always
{
return false;
}
}
Code in my class: **NOTE** this class extends JPanel
private JScrollPane storesPane;
private JTable storesTable;
Code in the Constructor:
storesTable = new JTable(tableData, COL_NAMES); //tableData and COL_NAMES are passed in
storesTable.setModel(new MyTableModel());
storesPane = new JScrollPane(storesTable);
storesTable.setFillsViewportHeight(true);
add(storesPane, BorderLayout.CENTER);
Hopefully some of you Java Gurus can find my error :)
This line creates a new JTable and implicitly creates a DefaultTableModel behind the scenes, one that holds all the correct data needed for the JTable:
storesTable = new JTable(tableData, COL_NAMES);
And this line effectively removes the table model created implicitly above, the one that holds all of the table's data and replaces it with a table model that holds no data whatsoever:
storesTable.setModel(new MyTableModel());
You need to give your MyTableModel class a constructor and in that constructor call the super constructor and pass in the data that you're currently passing to the table in its constructor.
e.g.,
public class MyTableModel extends DefaultTableModel {
public MyTableModel(Object[][] tableData, Object[] colNames) {
super(tableData, colNames);
}
public boolean isCellEditable(int row, int column) {
return false;
}
}
Then you can use it like so:
MyTableModel model = new MyTableModel(tableData, COL_NAMES);
storesTable = new JTable(model);
Earlier today I had the same problem. This solved it for me.
JTable table = new JTable( data, headers ){
public boolean isCellEditable(int row, int column){
return false;
}
};
works great!
Just override the isCellEditable method of the DefaultTableModel class. The quick way to do this:
JTable table = new JTable();
DefaultTableModel dtm = new DefaultTableModel(0, 0) {
public boolean isCellEditable(int row, int column) {
return false;
}
};
table.setModel(dtm);
Hello Friend am also working on table please try my code
import javax.swing.table.AbstractTableModel;
public class Table extends AbstractTableModel {
private boolean DEBUG = false;
private String[] columnNames = {" Date Time", " Parameter",
" Barometric Pressure (hPa)", " Temperature (°C)", " Battery Voltage (V)"};
public static Object[][] data = {};
public TableControllerViewdataTabTableModel() {
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public int getRowCount() {
return data.length;
}
#Override
public String getColumnName(int col) {
return columnNames[col];
}
#Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
#Override
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
#Override
public boolean isCellEditable(int row, int col) {
return false;
}
#Override
/**
* The setValueAt.
*/
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
printDebugData();
}
}
/**
* The printDebugData.
*/
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
}
}
}
}

Categories