Can I add JRadioButton into JTable - java

I tried to add JRadioButton into JTable by using CellEditor and CellRenderer, but I can't add JRadioButton into JTable. I am using NetBeans and backend MySQL. Please help me.
Edit: Thank you, but I have no idea about how to group JRadioButton. Can you help me?
I have 4 columns .First column cell containing item name , second column cell containing quantity,3rd and 4th column cells contiaining JRadio Buttons.Then I want to grouping 3rd and 4th column cells containing JRadio Buttons in each row
Edit:If I try to add radiobutton in the customize code of jTable by using this,
new JRadioButton("a") , then it'l come as, javax.swing.JRadioButton[,0,0,0x0,invalid,alignmentX=0.0,.....text=a], in the column instead of button

It's not clear how you want to use JRadioButton in a JTable; consider these alternatives:
Use SINGLE_SELECTION mode to select individual rows.
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Use a column of type Boolean.class, which will be rendered using a JCheckBox. This example limits selections to a single row.
Use a JComboBox as an editor for mutually exclusive choices within a row.
Addendum: If JRadioButton is required, they can be added to a JPanel, as shown in this example due to #mKorbel.
Addendum: If each JRadioButton has its own column, you can't use a ButtonGroup because a single button is used for all cells having the same renderer. You can update other button(s) in the same row from your TableModel, which should override setValueAt() to enforce the single-selection rule that is usually managed by the ButtonGroup. There's an example here.
Addendum: This example due to #Guillaume Polet illustrates a way to manage one radio button per row.

Related

In Java Swing how to capture radio group selection within a table cell editor?

I added a radio button group to a JTable column and defined TableCellRenderer and TableCellEditor. The JTable is populated dynamically. When I click on the column, I see that TableCellRenderer and TableCellEditor are called. However, if I change the selection within that radio button group, I have no way of knowing what the user selected.
Could someone possibly help?
Thanks

SWING - Multiple JCheckBox in a JTable cell

I've a JTable where each cell from a column to contain a list of JCheckBox (to select multiple skills from the list)
Any idea how to do that or an alternate way?
Thanks,
You can use a JPanel (and add desired amount of JCheckboxes to the panel) as the TableCellRenderer.
Or you can create a TableCellRenderer based on JTable (e.g. 1 column with checkboxes in rows)
Actually I'd need something like this combobox. The user should be able to select multiple values in a 16 items list.
[edit] I found what I was looking for here

Edit JTable in Netbeans Builder

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.

Display values of a selected row from a dynamically varying JTable

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.

Add text and icon (with MouseListener) to JTable column

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.

Categories