Select next cell JTable - java

I would like to make a jTable in which when user select an uneditable cell then it should change focus to the next editable cell automatically. Important: the user could select a cell by keyboard (tab or arrow) and by mouse clicking. Is it possible?? How to to it?

This link details Programmatically Making Selections in a JTable Component; you'd have to have mouselisteners/etc chained to work off this.

Table Tabbing shows how you can do it with the keyboard.
I've never tried it but you should be able to use a MouseListener to invoke the same Action when you click on a cell.
Just did a quick test for the MouseListener and it seems to work fine:
JTable table = new JTable(...);
final EditableCellFocusAction action =
new EditableCellFocusAction(table, KeyStroke.getKeyStroke("TAB"));
MouseListener ml = new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
JTable table = (JTable)e.getSource();
int row = table.rowAtPoint(e.getPoint());
int column = table.columnAtPoint(e.getPoint());
if (! table.isCellEditable(row, column))
{
ActionEvent event = new ActionEvent(
table,
ActionEvent.ACTION_PERFORMED,
"");
action.actionPerformed(event);
}
}
};
table.addMouseListener(ml);

Related

mouseclick action event performs in wrong way in java

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.

getSelectedRow on a combobox cell editor

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/

How to disable mouse activity when the value in a table is not in proper format?

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

can i add the combobox into particular cell of the JTable?

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) {
}
}

How to get some interactivity with a JTable

I Have a JTable where the data model contains information from a sql query. Want to get the added ability to take me to a new jpanel by double-clicking a row in the jtabel.
Thnx
You can add a MouseListener to a JTable and then handle the mouseClicked event.
The following code shows a mouseClicked implementation that finds out what row was double clicked. You can then navigate to a panel using this information.
public void mouseClicked(MouseEvent event)
{
if (event.getClickCount() == 2)
{
JTable source = (JTable)event.getSource();
int rowIndex = source.rowAtPoint(event.getPoint());
// get data from table model using row index
// navigate to panel
}
}

Categories

Resources