If required I'll post an SSCCE.
I have a JTable with JComboBoxes # First and Second Columns.
Upon a selection in the First Column JComboBox of a row, I want to update this row's Second Column JComboBox. (Chained Selections - I know how to do this with plain JComboBoxes but things go wrong with JTable)
I've tried getValueAt(int row, int col) method to change the CellEditor and dummy set the value as an empty string (like no selection), but it doesn't work properly. Doesn't properly update JComboBox and doesn't allow for selection and some other weird things.
Also, tried ItemListener for the First Column JComboBox but I can't find a way to properly update the Second Column JComboBox. I tried changing the CellEditor of the selected row, but it seems to be messing up with other rows as well, it's like more it remembers a previously selected row or something similar.
What is the proper way of doing chained selects in a JTable? I've been messing with this for almost a week..
In your case you should work with cell editors (your JCombobox). From editor you can get value from JCombobox and set this value to another JCombobox.
In my opinion you can 1) get selected cell; 2) from cell you can get cell editor; 3) from editor (if editor is JCombobox you can get text field component and from this component you can get value, jCombobox must be editable in this case) you can get value.
Related
Using a JTable, my Table Model setValueAt() method moves the selection to the next row in certain cases, using setRowSelectionInterval() and setColumnSelectionInterval(). When it's called from the (default) cell editor (by user typing in the cell and hitting tab), the code works: the desired next cell is selected (the first one on the next row).
However, if the user uses Return rather than Tab to commit the edit, the selection doesn't happen; instead the cell below is selected. That's fine with me.
I also have a JButton to clear a row. The button's action function calls the model's setValueAt() function for the desired cells. Unfortunately, the setRowSelectionInterval() and setColumnSelectionInterval() methods have no apparent effect; instead, no cells are selected.
I've tried table.requestFocusInWindow() and table.getParent().requestFocusInWindow(), as well as table.changeSelection(row, 0, false, false), all to no apparent effect.
Is there anything basic I'm missing here, before I go to the trouble of building the SSCCE?
In case it matters, here's the container hierarchy:
parent JPanel
button rows JPanel
button row 1 JPanel
button row 2 JPanel
table JScrollpane
JTable
The button in question is in button row 1.
Thanks!
Maybe you can use the Table Cell Listener to listen for edits to the table. It listens for actual changes done by the JTable editor.
Then in the supplied Action you can select the appropriate row. You may need to wrap the Action code in a SwingUtilities.invokeLater(...) to make sure the code executes after the table is completely finished editing.
I'm working on a Java GUI project with SwingX where I inserted a JTable with a DefaultTableModel! The JTable has two columns (ID and path) and in principle two main features:
With two JButtons "+" and "-" I can add rows to the DefaultTableModel and remove selected ones!
With double-right click on the second column (path) a JFileChooser opens, where I can select a path, which will be set as value for the cell! For this action I implemented a CustomMouselistener and overwrote the "mousePressed" method!
My problem:
After implementing this stuff, I can't edit the cell with a normal mouse-click on a cell! The cells are editable, because I can edit them with "F2" - press, but it seems that I overwrote the default functionality of the mouse!
Summarized I'm using following constructs:
Table with DefaultTableModel (2 columns)
MouseListener for JFileChooser
ColumnModelListener to change width of columns!
Can someone help me with this issue?
I designed a JTable with Netbeans Builder and I created 5 columns, the 5th one is Boolean so I want to know how to highlight whole the row when the user selected.
See Table Row Rendering for an easy way to color an entire row based on a value in the row.
The problem now is that only the cell is automatically repainted when you click on the checkbox so you will also need to add a TableModelListener to the TableModel so you can invoke a repaint() on the table row whenever the state of the checkbox is changed.
I've got a JTable with a model that has about 20 columns. That's more than you can fit in a single screen, so scrollbars enable the users to scroll up/down and right/left.
Now, if a user scrolls all the way to the right and clicks on a row, then that row gets selected just fine. However, if the user then use the scrollbars to scroll all the way to the left, and then press the down arrow key, the JTable automatically scrolls all the way to the right again (and selects the next row). It is as if the JTable remembers the column the user first clicked in, and when using the down arrow key the JTable just takes that column and moves down one row and scrolls back to that column.
Is there a way to disable this behaviour, so that the user remains in the selected view without JTable doing all this "magic" scrolling?
Scrolling a JTable isent connected to the cell selection.
Clicking on a cell will make the Jtable put its curose onto that cell. this means all future navigating will be from the last clicked place. Nomatter how much you scroll that last location will be the starting point of the key navigation.
But in fact the behaviour you describe is just the standard in about any gui. Take Intellij, Excel, Word, Editplus,... if you use arrow keys to navigate you always scroll back to where last clicked.
but GUi discussions aside back to your problem
i think you can make it work with
setAutoscrolls(false);
on your jtable
You could try setColumnSelectionAllowed(false), so that the user cannot select the column in the first place.
I'm preventing this behavior with this JTable override.
Note that my table does not care about cell selection, and only paints row selection (no cell selection border).
#Override
public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)
{
// essentially disabling cell selection, selected column index will always be 0
columnIndex = 0;
super.changeSelection(rowIndex, columnIndex, toggle, extend);
}
I want to implement following functionality but I am confused if it's possible in Java. If yes, than how? Please help:
I want to create a JTable kind of table where 1st row of table contains column names and an icon in each column i.e. in each cell of 1st row. Clicking on that icon should lead to removal of that column from table (possible using MouseListener??).
I have found many solution where I can add button to a cell in JTable but none which describes adding both text and icon (with MouseListener) to a cell. Please see if you can help and thanks a lot for reading.
You can create a custom TableCellRenderer that extends JLabel. This JLabel can be created with an icon (JLabel can display icons, to the right or left of the text). You will want the getTableCellRendererComponent to test wether the row being rendered is the first or not, and if so, set the icon, otherwise do not.
For the removal action, you can add a MouseListener on the table, and when processing the mouseClicked method, you can find the cell that was clicked in by testing the rowAtPoint and columnAtPoint by creating a Point from the mouseEvent.getX() and mouseEvent.getY(). If you determine the first row with the icon was clicked, you can remove the column from the column model.
If by 1st row, you actually mean the table header, you can create the same renderer for the JTableHeader, and set the MouseListener on that component.
Well, I don't understand your question.
I want to create a JTable kind of
table where 1st row of table contains
column names and an icon
Do you mean the Table Header, like the way sorting works by displaying the column name and the sort direction?
If so then you use a custom renderer for the table header and add a MouseListener to the header to determine which column was clicked. You should be able to customize the Default Table Header Renderer to do what you want.
Or do you mean the first row of data in the table. If so then you still need to use a custom renderer but this time you add the MouseListener to the table not the table header.
In both cases you can use the TableColumnModel.removeColumn() method to remove the column from the view of the table.