Extending (sliding down) JTable row - java

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

Related

How to render a Component in JTable using TableModel?

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.

Changing Content of JTable Cells Individually

So Suppose I create a JTable with a specific number of columns and rows. Is it possible to add a JPanel to a specific cell after the JTable is already initialized? The JTable would be empty at first and then I would change a cell at row X and column Y to instead contain a JPanel which would contain an amount of ImageIcons. I don't have any code to put here really, I just need to know what I would have to use to accomplish this and how. Thank you.
To change the data in the table you use:
table.setValueAt(...);
This will update the data in the TableModel.
However, adding a JPanel to the table is not the way a table is designed to be used. Typically you add data to the model. Then you use a renderer to display the data in a cell.
Start by reading the section from the Swing tutorial on Using Custom Renderers for more information and examples.

java : when is this method called : getTableCellRendererComponent?

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

JComboBox sizing issue in a table with Multi line Cells

I recently had a problem where I needed to have a field that can wrap the text and increase the height of the row as the text is wrapped, similar to Microsoft Excel. I managed to get it working correctly, the only problem is that the table contains multiple JComboBoxes. When the row's height increases from the field that wraps the text, the size of the JComboBox window and ArrowButton also increase. I am using a DefaultCellEditor for the JComboBox fields, and created my own Editor/Renderer to be used with the JTextArea field. Once the JComboBox's value is selected, the value gets displayed correctly in the field, the only problem is while I am selecting the value, the JComboBox window and ArrowButton could be HUGE depending on the size of the row. Is there any way to increase the height of the row, but have the JComboBox field height remain the same instead of growing to fill the column that it's in? I am thinking I might need to make a Custom Cell Editor for the JComboBox fields as well instead of using the default. Thanks in advance!
I am thinking I might need to make a
Custom Cell Editor for the JComboBox
fields as well instead of using the
default
That would probably be the solution since the size of the editor is determined by the size of the cell.
I would try using a JPanel with a BorderLayout as the editor component. Then you add your editor to the North of the panel.
It would be the easiest editor to create since all the mouse events and key events are passed to the editor I believe, which means the panel will get the events, not the combo box. So I guess you would need to forward these events to the combo box.
First, is the JComboBox in a BorderLayout and set to BorderLayout.CENTER?
If so, I'd change it to a different layout such as AbsoluteLayout so it does not stretch to fill the cell.
Also, I will also refer you to this post Putting JComboBox into JTable

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