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.
Related
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.
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.
I use jPanel as my cell and the table has just one column. It looks like this ( JTable: Buttons in Custom Panel in Cell ).
By default, the panel associated with the row (celll) contains a date when it was created. i want to implement an ActionListere method that will extend( slide down) the jpanel in which the action occured to see the whole jPanel data.
Hope you get what i mean.
Basically, you want to do the same thing that JList.ensureIndexIsVisible(int) but with a table.
Component has a method called "scrollRectToVisible(Rectangle aRect)" which should just about do what you need.
So, using your JTable, you can get the cellRect(int width, int height, boolean includingSpacing) and pass it off to the "scrollRectToVisible"
You're going to have to do some clever work to find out the row of the action, but I assume that you are using a table cell editor which has already provided you with this information
In order to set the color for a specific table cell, I should create a custom TableCellRenderer which has the following method : getTableCellRendererComponent.
when is this method called : getTableCellRendererComponent ?
just when a JTable is drawn for the first time ?
In case I have a set of buttons and a jtable on a jframe, and each button when pressed will cause a certain number of cells in the table to be colored, how can I cause this method "getTableCellRendererComponent" to be called when I press on the button ?
This is fired when the table is first rendered but should be called on all cells when either a fireTableCellUpdated or any of the other "fire*" methods are called. I'm under the impression that you're using a class that extends AbstractTableModel (DefaultTableModel?) at which point these methods should be fired when you update a particular value of a table model.
An example of using custom cell renderers can be found at Example Depot. The getTableCellRendererComponent will be called when a cell is brought in to view and when a model update occurs.
Addendum: I just noticed one other part to your question, when is the getTableCellRendererComponent called? This is called for every visible cell in the table that must be rendered. Think of it this way, if you are using a JLabel internally as your renderer, you would return the JLabel as configured for displaying that cell only. It will be reused on the next cell for rendering it. Easiest way to think of it is a visual template that you want your cell to look like, you configure it on the getTableCellRendererComponent call, and return it. The calling framework renders it to the screen buffer as an "image" (for lack of a better term) then is reconfigured for the next cell and rendered again.
if you want to forgot for when/how/where is/are TableCell(s) updated or not then look for prepareRenderer best example as I see camickr blog
EDIT: for better/valuable hepl please edit your post and add your code
when is this method called :
getTableCellRendererComponent ?
This method will get called when the UI component gets displayed/rendered for your view the very first time.
To set colors in cells of a JTable
Hope you are using the custom DefaultTableCellRenderer. If so, then you can globalize the JLabel in your custom CellRenderer and then set its background color upon button click based on the row, column index.
Else you can use the DefaultTableColumnModel which you have defaultrenderer/set customrenderer which will be renderered for each of the cell. Using which you can set the background color of the cell.
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setCellRenderer(new MyTableCellRenderer());
Refer: Simple example to demonstrate usage of TableCellRenderer
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.