Mimic default behaviour of row rendering in a JTable - java

In my Java 7 application I use a Swing JTable with two columns. The left column uses a DefaultTableCellRenderer where the setHorizontalAlignment() is set to centered, whereas the right column uses no specific renderer.
That right column shows each table row in alternating colors by default, which is not the case on the left column with the renderer used. Moreover, when I hover with the mouse over the rows on the right column, then the row under the mouse curser is highlighted when focused, which also isn't the case with the left column.
Is there any (easy) way of mimic the default behaviour of the row rendering (i.e. the alternating colors and the highlighted row) when using a DefaultTableCellRenderer?
PS: I am using the Substance L&F.

Is there any (easy) way of mimic the default behaviour of the row
rendering (i.e. the alternating colors and the highlighted row) when
using a DefaultTableCellRenderer?
Substance has own Renderers, you should change XxxXxxRenderer by add Substance before, e.g.
SubstanceDefaultTableCellRenderer instead of DefaultTableCellRenderer, the same for JComboBox, JList, JTree or JTableHeader

Related

Resizing the column of jxtable automatically

I am using JXtable in my project.When I double click some column,it will be expanded or contracted to the highest character cell in that column. Is there any way of creating JXtable so that all its columns shall be resized to the size of its highest character cell.
I'm not sure I understand correctly, but to programmatically resize all columns according to their contents you can use JXTable.packAll();.
packAll() Javadoc:
public void packAll()
Resizes all columns to fit their content.
By default this method is bound to the pack all columns Action and registered in the table's ActionMap.
This will make the column with the "broadest" content the biggest, the column with the second "broadest" content the second biggest and so on. I am using it in my projects and it works quite well.
Resizing can also be achieved by clicking on the according menu item from the table controls.
You can use the Table Column Adjuster which will automatically adjust the width of a column any time the data is changed.

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

JTable change cell colors using TableCellRenderer

I am using a JTable in my GUI application as a grid to represent positions for a game. I want the cells of the table that represent a certain position of an object to have a certain color, and on some actions, the object to move (i.e the color cell to move around in the Grid /JTable). I know that I can change cell colors by making a class that extends DefaultTableCellRenderer , is this the only way it can be done? or is there a simpler way to chage cell colors??Also Is JXTable better than JTable for such an application ?
EDIT: I didn't include the fact that I need certain cell colors to change dynamically, i.e with button clicks, keyboard clicks ...etc, is that still possible with any TableCellRenderer in case I am still using JTable ?
Thanks,
As an alternative, consider using prepareRenderer(), as suggested by #mKorbel and shown in the article Table Row Rendering.
With JTable, DefaultTableCellRenderer is the best way to do it.
However, if you use JXTable, it'll be much easier to use a Highlighter and a custom Predicate. Generally, Predicates are used to color based on contents of the cell, but you could just as easily have it color based on row/column and some external value.
Look in http://www.jarvana.com/jarvana/view/org/swinglabs/swingx-core/1.6.2/swingx-core-1.6.2-javadoc.jar!/org/jdesktop/swingx/JXTable.html under Rendering and Highlighting, which is a striped table and pattern matching. You'd essentially do the same thing as the pattern highlighter, but with your own Predicate.

using a JLabel+icon with JTable cellrenderer + selection highlight

I have a JTable with one column that has a custom cell renderer that shows one of several icons.
It works well except the selection highlight does not automatically appear, and I don't know how to apply a highlight in my custom cell renderer.
Any suggestions?
I have a JTable with one column that has a custom cell renderer that shows one of several icons.
JTable supports the display of Icons. Just add your Icon to the model and then override the getColumnClass(...) method to return Icon and the proper renderer will be used.
In your renderer code, you will have to explicitly set the background in case of selection. The usual way to do it is asking the UIManager to provide you the color for Table.background and Table.selectionBackground
In your getTableCellRendererComponent() method there is a parameter (boolean isSelected), that indicates when the row is selected. You will need to check that and do highlighting yourself in the renderer.

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