How to get some interactivity with a JTable - java

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

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.

JTable choose other rows and get their data when certain cell is clicked

I have a JTable. I want to create an event for one cell that if the user clicks on it, he is able to choose one or more rows from the table and the corresponding IDs are saved in that cell.
So in the example the user would click on "Click here to choose" in row 2 and then click on e.g. row 1 and row 3. The cell "click here to choose" should then be overwritten with something like 1 and 3 afterwards:
I'm thinking of somehow creating a MouseAdapter Event on click on the cell but I have no real idea how to do it. Any idea how I can approach this?
Use a ListSelectionListener with MULTIPLE_INTERVAL_SELECTION. In the handler, update the table's model using setValueAt() to reflect the change.
yes you definitely need to use MouseAdapter as below(you have the cell if "if condition become true"):
jt.addMouseListener(new java.awt.event.MouseAdapter() {
#Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
int r = jt.rowAtPoint(evt.getPoint());
int c = jt.columnAtPoint(evt.getPoint());
if (r >= 0 && c >= 0) {
......
}
}
});

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

Select next cell JTable

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);

Categories

Resources