I used a JButton as render and editor in JTable, with following problems:
The JTable row is not selected when JButton clicked.
When the JButton is clicked, how to get its location within JTable, in other words its columnIndex and rowIndex
Any help will be appreciated!
To display a button in a column you need to create:
a custom renderer to display the JButton
a custom editor to respond to the mouse click
Read the section from the Swing tutorial on How to Use Tables. The section on:
Using Custom Renders will explain the basics of using a renderer
Using Other Editors will explain the basics of using an editor
Working example are provided in the tutorial that you can download.
You can check out Table Button Column for one approach.
The code uses a single class to implement the custom renderer and editor that you will need for the column to display your text as a button.
Related
I have successfully displayed a JTable using an AbstractTableModel, but I want to add delete button for each row in the last column, in the getValueAt method that returns Object there is no way that I can return JButton, JLabel or any JComponent that is clickable. I tried that and I only get the object description toString.
Is there another solution to render JComponent in a JTable without using the TableModel approach?
Is there another solution to render JComponent in JTable without using the TableModel approach?
The TableModel is used to hold the data for the data for the model.
The JTable implements the view of the data for each column. The renderer is just a picture of the data. You can easily render the data to look like a button, however a renderer does not respond to any event.
The JTable does support editors and that is how you interact with real components. When you edit a normal cell a JTextField is placed in the cell location so you can type data into the cell and then the data is save to the model.
So if you want to click on a button then you need to use a button as an editor.
Check out Table Column Button for a class that uses a JButton as the renderer and editor. You then provide the class an Action to be invoked when the button is clicked.
Read the section from the Swing tutorial on Concepts: Renderers and Editor for more information. There is also a section on Using Other Editors.
One way : TableColumn.setCellEditor(jbutton_instance) on a hand added column.
I have a JTable which contains names returned from a search in database. I want to use the names as buttons, when we click on them the program redirect to another frame with more details of the nameholder. Can I do that ?
This is easy to do, add the mouse listener to the table and use columnAtPoint and rowAtPoint to determine what has been clicked.
If you want a table cell to look like a button, set a renderer that uses JButton.
I want to use the names as buttons, when we click on them the program redirect to another frame with more details of the nameholder.
You need to create a custom renderer and editor for your table. Read the section from the Swing tutorial on Concepts: Editors and Renderers for basic information.
Then you can check out Table Button Column which is an example of a render/editor that you can use. You will need to provide your own custom Action to display the details.
On my form I have a jtable and a textarea. My table only has 2 columns - ID and Comment
Is it possible that when a user clicks on a cell in the comment column. The cell value will appear in the textarea in edit mode?
I did set the cell editor to singleclick
selectTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
TableColumn col = selectTable.getColumnModel().getColumn(1);
DefaultCellEditor singleclick = new DefaultCellEditor(new JTextField());
singleclick.setClickCountToStart(1);
col.setCellEditor(singleclick);
I have a method outputSelection() that gets called from a edit button. The method gets the value from the selected cell and puts the value in the textarea for edit.
Can the click activate the method so the user does not have to click a edit button?
You could attach a mouse listener to the table and monitor the mouse clics from there, getting the selected column/row & thus the value
You could supply your own cell editor that updates the text area when the editors value is set
You could extend te jtable & when cell editing is started, update the text area
Yes, this is a process I learned to use after having duplicate code throughout my swing application. I started making standalone methods that did the work I wanted, and then I call those methods from the action events from the button or mouse click. That way they all execute the same code.
Even if you have a tab or enter key command, you can also have it execute your same method as the others for more consistent code.
If your button performs specific code with cell values, just extract all of that code out into a method that takes the cell value as input. Then you can call that same method from any event and pass in the input data you want to display in the text area.
I'm using JPasswordField as JTable cell editor in a column.
It works fine while entering text; but after moving the cursor to another cell, it displays simple text.
So what can I do to keep text hidden while moving in other cells? Thanks in advance.
The table uses a different component for the editor vs the regular cell renderer. You need a custom TableCellEditor that contains a JPasswordField.
I have a JTable that has a table header which contains a JPanel with a JLabel with a mouse listener on the JLabel. When the JLabel is clicked a dialog is shown. I'm trying to trigger this JLabel in the JTable heading without a mouse. In order to trigger this JLabel without a mouse, I need the focus to be on the JTable heading (that is, I need the JTable heading to be in the focus traversal policy). Does anybody know if its possible to Tab to a JTable heading cell?
table.getTableHeader().setFocusable(true)
alternatively, you might add the action "triggered by the JLabel" to the table's action map and bind it to a keyStroke in the table's when_ancestor inputMap
Focus can be placed on the TableHeader by using F8 when focus is on the table.
You can then use the right/left keys to move between the header cells.
You use the Tab key to place focus back on the table.