Getting value of checkbox from jtable in java - java

I have a problem to get value of checkbox from jtable in java, that is when i get value by this code "table.getvalue(0,1)" then i can not get the right value.

returns value from JTable contains JCheckBox represents Boolean value,
toString returns "true" / "false"
more in the JTable tutorial

As a concrete example, I got the expected result when I added the following line to the loop in the actionPerformed() method of this example:
System.out.println((table.getValueAt(i, CHECK_COL)));

JTable get cehckbox value when check box is checked:-
table.getModel().addTableModelListener(new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
for(int i=0;i<table.getModel().getRowCount();i++)
{
if ((Boolean) table.getModel().getValueAt(i,0))
{
System.out.println(">\t"+table.getSelectedRow());
break;
}
}
}
});

Related

JList - `setListData()` causes `getSelectedValue()` to return null

When I create my JList I am able to use getSelectedValue() to print out the string I have selected in the list. As soon as I change what is inside the list everything that is returned is null.
When the list is created I have this:
matchList.setModel(new javax.swing.AbstractListModel<String>() {
String[] playerList = {"test"};
public int getSize() { return playerList.length; }
public String getElementAt(int i) { return playerList[i]; }
});
Later I change the JList to contain an array of strings:
matchList.setListData(Bracket.wr1);
Everything inside the array displays in the JList but if I try to use getSelectedValue() to get the String being displayed it just returns null.
What am I doing wrong?
It seems there is no selection anymore. According to the Oracle Documentation getSelectedValue()
[r]eturns null if there is no selection.
So if there is no default selection anymore, you should probably just set the first element as the default:
list.setSelectedIndex(0);

TableModelEvent.getcolumn() returns -1

I intent to use a TableModelListener to react on user entries of a JTable. I would like to know the column of the edited cell. Unfortunately, the method getColumn() returns -1 instead of the edited column number. Any idea why?
public class TableEventListener implements TableModelListener {
#Override
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE ) {
System.out.println(e.getColumn()); //prints -1
}
}
}
As noted in the TableModelEvent API for getColumn(), "If the return value is ALL_COLUMNS; it means every column in the specified rows changed." Note that ALL_COLUMNS has the value -1.

Quick way to see if all checkboxes are unselected

I have a java JPanel with 16 JCheckBoxes and I am wanting to ensure that the user selects at least one before submitting the form. The only way I know to do this is a huge if statement that looks at the Boolean value of the "isSelected()" method, but this seems inefficient.
So I was wondering if there was a faster to way to check if all of the boxes are unchecked.
You don't need an if statement. You can do it with a big logical expression using ||:
boolean somethingChecked = box1.isSelected()
|| box2.isSelected()
|| ...;
or, if the boxes are in an array (much preferred), a loop:
boolean somethingSelected = false;
for (JCheckBox box : boxes) {
if (box.isSelected()) {
somethingSelected = true;
break;
}
}
Alternatively, you can use an ItemListener attached to each JCheckBox to track the count of checked boxes:
int selectionCount;
ItemListener boxListener = new ItemListener() {
#Override public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
selectionCount++;
} else {
selectionCount--;
}
}
};
(Note that if this is all that the ItemListener is doing, a single instance can be attached to all the boxes.) The selectionCount should be initialized to the number of boxes initially checked. Then at the appropriate point(s) in your code, you can simply test whether selectionCount is greater than zero.
This will do the trick I believe:
public boolean validatePanel(JPanel panel) {
for (Component component : panel.getComponents()) {
if(component instanceof JCheckBox){
JCheckBox c = (JCheckBox) component;
if(c.isSelected()){
return true;
}
}
}
return false;
}
That method receives a JPanel and get all the components on it. Then check all of then, if it's a checkbox will cast the Component to CheckBox to have access to isSelected method. If any checkbox is selected it will return true, if it finish the foreach without returning any true it means that no checkbox were selected.

GWT Celltable How to make Non-Editable cell in Editable column

I added EditTextCell(stringTestEditTextCell) to Column(testColumn).
EditTextCell editTextCell = new EditTextCell();
Column stringColumn = new Column(
editTextCell) {
#Override
public String getValue(Record object) {
return object.getValue();
}
};
All cells in testColumn are editable.
I want 1st cell of column such way that 1st cell of column should be Non-Editable.
Following class is answer to my question. I Solved it and works fine. But getting error when user clicking on 1st cell of column.
class CustomEditTextCell extends EditTextCell{
#Override
public void render(com.google.gwt.cell.client.Cell.Context context,
String value, SafeHtmlBuilder sb) {
// context.getColumn()==2 indicate Record ID column and context.getIndex()==0 indicate non editable cell in 1st empty row
if(context.getColumn()==2 && ( context.getIndex()==0 || context.getIndex()%10 == 0)){
sb.appendHtmlConstant("<div contentEditable='false' unselectable='true'></div>");
}else{
super.render(context, value, sb);
}
}
}
You might extend EditTextCell and override the edit()-Method so that it only edits when you have not set a boolean flag that you that you need to set for the first cell.

Updating cell renderer after a DefaultCellEditor derived instance does its job

I use a JTable which has its own cell renderer and cell editor.
Say, this table contains 2 columns and x rows:
The first column contains a boolean value, its own cell rendering and cell editor (a radiobutton)
The second column contains a string value, its own cell renderer: it makes it bold when the first column of the current row is set to true (radiobutton checked)
All the values are correctly updated by the editor but the 2nd row does not become bold when the radio button is set to true...
I have to check a radio button from a different row to see the changes
Where can I fire thoses changes ?
Cheers and thanks for your help
RadiobuttonTableCellEditor.java
public class RadiobuttonTableCellEditor extends DefaultCellEditor
implements ItemListener {
JRadioButton rb = new JRadioButton();
public RadiobuttonTableCellEditor(JCheckBox pCheckBox) {
super(pCheckBox);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (value == null)
return null;
rb.addItemListener(this);
rb.setSelected((Boolean)value);
return rb;
}
public void itemStateChanged(ItemEvent e) {
super.fireEditingStopped();
}
public Object getCellEditorValue() {
rb.removeItemListener(this);
return rb.isSelected();
}
}
In your table model whenever your value changes you have to fire appropriate event. If your model is inherited from AbstractTableModel you can use several fireXXX methods. My guess is you should call them from setValueAt method.
If you know exact column and row - you can call fireTableCellUpdated, otherwise you can you probably have to use fireTableChanged since you have to update different column.
And of course you renderer should properly render new value.
It doesn't seem to make any sense to extend DeafultCellEditor there. Implementing a listener interface like that is also not a great idea.
Renderers work best as a thin layer. If another cell should change, then that needs to be reflected in the table model which should fire a relevant update event.
I guess it could help people with a similar problem, make a true radiobutton unique in a row, you'll have to extend the DefaultTableModel to modify its behaviour especially the setValueAt method
Cheers
/**
* When <code>column</code> is the column that contains the Boolean (in fact the radio button):
* If aValue == false and that it had a previous value set to true we don't do anything
* If aValue == true and that it had a previous value set to false, we set all the other booleans to false and this one to true
*/
#Override
public void setValueAt(Object aValue, int row, int column) {
if (column == colonneBoutonradio)
{
if (((Boolean)aValue && !(Boolean)super.getValueAt(row, column)))
for (int i = 0; i < this.getRowCount(); i++)
// i==row permet de vérifier si la ligne courante est celle à modifier (et donc celle à mettre à true)
super.setValueAt(i==row, i, colonneBoutonradio);
}
else
super.setValueAt(aValue, row, column);
}

Categories