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.
Related
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) {
......
}
}
});
I am having trouble with calculating the sum of my table column whenever a row is added or deleted. I have a button which adds a row to the table, and I want the sum of the to be automatically calculated and displayed in a text field. I have made a method getsum for the calculation and the am calling this method on the actionperformed event of the button. I am getting an error when I run the program and nothing is displayed in the text field.
Add a TableModelListener to your table. That way, the text field will get updated every time the table's model changes.
model.addTableModelListener(new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
textField.setText(getSum());
}
});
you have to call .setText function of the particular textField to bind the value u obtain onto it.
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/
Below is the snippet of my code. I have a JTable. I have extended the DefaultCellEditor to create my own editor.I have key listeners and mouse listeners added for the cells.I have a JButton as well. When i click on the JButton, i want the first cell in the JTable to enter edit mode.. For this i have used requestFocus and editCellAt(0,0). I have put this code in the actionperformed.
public void actionPerformed(ActionEvent e)
{
System.out.println("action performed");
if(e.getSource().equals(btn))
{
oTable.requestFocus();
oTable.setRowSelectionInterval(0, 0);
oTable.editCellAt(0, 0);
}
}
This places the cursor in the first cell. But when i type anything, the key events are not fired! Note: if i use my mouse to click on the cell and then type, it does fire key events. But I don't want to do this extra click.
Seems to work for me. Maybe you can try the following:
table.editCellAt(0, 0);
table.getEditorComponent().requestFocus();
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
}
}