I need a listener on a CombobBox which is a cellEditor on a JTable.
This listener must give me the new selected value and the row id.
Problem with my below solution is that the listner is linked to all rows, so when I change one ComboBox value in one row, then move to another row (with a different combo value) an event is raised, but the selected row has not yet changed. How can I get rid of this case ?
Thanks
column = jTableCheck.getColumnModel().getColumn(9);
JComboBox comboBox = new JComboBox(comboGenre);
comboBox.addItemListener(new ItemListener(){
#Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
int row = jTableCheck.getSelectedRow();
Popup.info(e.getItem() + " SELECTED, row="+row);
}
}
});
column.setCellEditor(new DefaultCellEditor(comboBox));
Don't use an ItemListener on the combo box.
Instead you should be using a TableModelListener. An event will be fired whenever the data in the TableModel is changed. So you add the TableModelListener to the TableModel of your JTable.
The TableModelEvent will give your row/column of the cell that changed. You can get the changed value from the TableModel.
Or maybe you would want to use a Table Cell Listener which is similar to the TableModelListener except the code is only invoked when the value is actually changed and you use an Action to do the processing.
In fact, I already used a TableCellListener on another table, but forgot about that!
I found out a usefull class here: http://tips4java.wordpress.com/2009/06/07/table-cell-listener/
Related
Here am using mouseClicked event to get data on the field while clicking on the table for that i used my code as below
scrollPane.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int rowIndex= table.getSelectedRow();
DefaultTableModel model=(DefaultTableModel) table.getModel();
txt_Product_ID.setText(model.getValueAt(rowIndex,0).toString());
txt_Product_Code.setText(model.getValueAt(rowIndex,1).toString());
txt_Product_Name.setText(model.getValueAt(rowIndex,2).toString());
}
});
Here the problem is when i click on the row or column the data is not appearing on the corresponding fields but appearing when clicking on row or column and clicking on the remaining space available on the table.so double time clicking only producing the result.please help me to solve my problem
scrollPane.addMouseListener(new MouseAdapter() {
Here the problem is when i click on the row or column the data is not appearing on the corresponding fields
Don't add the MouseListener to the scrollPane. The MouseListener should be added to the table, since that is the component you are clicking on.
I have a JTable and in one column I have a JComboBox for each of the rows. I am dynamically adding rows when I press a button. The selection made in the combobox will determine what calculation is carried out for that particular row. For arguments sake lets say that the options for the combobox are: option 1, option 2, option 3 and option 4.
The issue I am having is as follows:
Say I have added 2 rows and select any option from the combobox for row 1, when I go to make a selection in the combobox for row 2 the same selection is ticked as was made for row 1. There seems to be some kind of memory. How can I disable this, so that the default selection is always -1 (i.e. non of the options selected)? I would like to have complete control over this.
Here is an example snippet of code just considering option 1:
String labels[] = {"Option 1", "Option 2", "Option 3", "Option4"};
JComboBox comboBox = new JComboBox(labels);
comboBox.setSelectedIndex(-1);
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
int state = itemEvent.getStateChange();
ItemSelectable is = itemEvent.getItemSelectable();
if (selectedString(is) == "Option 1" & state == ItemEvent.SELECTED){
System.out.println("A");
}
}
};
comboBox.addItemListener(itemListener);
Thanks very much for your time and help :)
First of all don't use "==" when comparing strings. Instead you should be using the equals(...) method:
if (someString.equals(anotherString))
// do something
However, that is not the cause of the problem.
You are using the JComboBox incorrectly for a JTable. You should NOT be using a ItemListener (or any listener).
The combo box is just used as an editor for the table. That means when you select a value from the combo box, the TableModel of the table is updated. So if you have custom logic based on the selected value you need to override the setValueAt(...) method of your TableModel.
#Override
public void setValueAt(Object value, int row, int column)
{
super.setValueAt(value, row, column);
// add your custom logic here
}
How can I disable this, so that the default selection is always -1
The value displayed in the combo box is taken from the TableModel. So if you set the default value to be null the combo box will not have a selection when you start editing.
Read the section from the Swing tutorial on How to Use Tables for more information and working examples. Keep the tutorial link handy for future reference on Swing basics.
Is there any way to listener for cell selection even on already selected cells with a JTable, other than using a MouseListener?
I have a JTable with a row and column listener. Neither listener fires when selecting an already selected cell:
JTable table() {
JTable table = new JTable(10, 10);
table.getSelectionModel().addListSelectionListener(rowListener);
table.getColumnModel().getSelectionModel().addListSelectionListener(colListener);
return table;
}
ListSelectionListener rowListener = event -> {
if(event.getValueIsAdjusting())
return;
System.out.println("Row: "+((ListSelectionModel) event.getSource()).getMinSelectionIndex());
};
ListSelectionListener colListener = event -> {
if(event.getValueIsAdjusting())
return;
System.out.println("Col: "+((ListSelectionModel) event.getSource()).getMinSelectionIndex());
};
My goal was to switch cells on/off. It works, other than the fact that the listeners do not fire when selecting an already selected cell, which is represented through the SSCCE above.
There doesn't seem to be any listener I can attach to the JTable (or it's models/selection models) to handle this, unless I were to use a MouseListener and manually manage the cooridnates. Using a TableModelListener, I can listen for changes, but this event is targeted at a previous cell (that has been deselected), and clicking an area that doesn't select a cell would cause that listener to fire.
My goal was to switch cells on/off.
Store Boolean data in the TableModel. Then whenever you click on the cell the value will toggle between true/false.
The default renderer for a Boolean value is a check box. You can always use a custom renderer if you don't want to see the check box.
I have a JTable with several columns. I override the getColumnClass method of the table model in order to specify which columns hold Integer values. So basically when a user tries to enter a String into an Integer column, he/she is not allowed to do so. The problem is that the user can still click on a button on my form which then uses the improper value in that cell.
How can I not allow the user to click on any buttons as long as one of the cells in the table is still being edited?
Add a PropertyChangeListener to the JTable:
#Override
public void propertyChange(PropertyChangeEvent e)
{
// A cell has started/stopped editing
if ("tableCellEditor".equals(e.getPropertyName()))
{
if (table.isEditing())
// disable buttons;
else
// enable buttons;
}
}
Or, if you don't want to disable the buttons, you can just add code to the ActionListener to check if the table.isEditing() and if so then just return.
You could use one of the 3 methods returning information on the editing process to enable your button.
JTable table = new JTable();
table.getEditingColumn();
table.getEditingRow();
table.getEditorComponent();
Check JTable documentation to see which could be best used for your case. You could make your button enabled only if
table.getEditorComponent();
returns null for example.
add a TableModelListener to your JTable
you should override its tableChanged method somewhat like this :
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
//Check the data!!!
//Check the data!!!
//Check the data!!!
//disable the button if needed right here
}
ref:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#modelchange
I want to add combobox into a cell of JTable.
model=new DefaultTableModel(data,col);
JTableHeader head=new JTableHeader();
head.setBackground(Color.BLUE);
table=new JTable(model);
table.add(head);
JComboBox combo = new JComboBox();
combo.addItem("Names");
combo.addItem("Antony");
combo.addItem("Victor");
combo.addItem("Ramkumar");
table.add(combo);
But i cant get the combobox in the cell. Is it possible to set combo box?
You need to set the TableCellEditor of the JTable. It's better to search the Java Tutorials, but here is a short explain.
JTable uses three main classes to work:
1) TableModel: it's function is to say how many rows and columns the table has and to serve the data of the Table, it's main methods are getValue(row,col) and setValue(value, row,col). And fire events to notify the JTable repaints.
2) TableCellRenderer: it's main purpose it's to draw components in the JTable's cells. This components are only painted: NOT WORK! if you draw a JComboBox it won't desplegate if you click on it or if you draw a JCheckbox it wont't select/unselect.
3) TableCellEditor: it's main purpose it's to draw a component within a JTableCell to edit the value of the cell. It receives events and decide when to start the editing, then it's getTableCellEditorComponent method is called to return the editor component. The component returned has to launch events so that the TableCellEditor knows when to stop the editing and get the value and use it to call the TableModel.setvalue... or cancel the editing.
So that to show JComboBox in a JTable you must create your own TableCellEditor, not an easy task if you haven't done it before.
Take alook at this
Java tutorial and search in this page for "Using a Combo Box as an Editor"
Try this: its working for me..click on the cell to see the combobox.
private void comboloader() {
try {
TableColumn gradeColumn = jTable1.getColumnModel().getColumn(0);
JComboBox comboBox = new JComboBox();
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));
} catch (Exception e) {
}
}