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.
Related
I want, in a JTable, has a column that when it is clicked, a JPanel to appear with the names of all the columns, and it can select (with JCheckBox) which we want to continue in the JTable. It would be a column with "#".
Just create such a column in your data model implementation, with a custom artificial data object. Then register an editor (setDefaultEditor) in the jtable to show a checkbox or open a dialog with a checkbox. based on the user selection you may then alter your model (add or remove columns) and fire an according changed event.
Not exactly what you asked for but maybe you can use the Table Column Manager.
The Table Column Manager manages which columns are visible in the table. You invoke the Table Column Manager by right clicking on any column in the column header. You are presented with a popup menu that uses check boxes to hide/show columns.
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 have to develop a front end in Swing.I have a JFrame in Netbeans with 3 panes in it.
JScrollPane 1:it contains a JTable
JScrollPane 2: It should display the value of the fields of the selected row
ListPane : which contains the list of tables from which a user chooses a table to be displayed.
Now since the content of JTable varies(the no of rows and columns also vary) dynamically based on the table chosen by user I can't drag and drop the TextBoxes in the 3rd scroll pane to display the selected row's values.It would be helpful if anybody can suggest a way to do it or any pointers to problems that deal with similar issue
Add a ListSelectionListener to your JList. When a particular table is selected from the list, use setModel() to change the TableModel of your JTable to one that is correct for the chosen table. A related example using setModel() is shown here.
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.
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.