JTable .. colour of selected row - java

I have a JTable in Java that has a custom dataMOdel and custom renderer. Now when i select some cell it has a dark blue border around it. I want the selected row to be all highlighted in some colour. How to do that?

You have to use custom table cell renderer. Check out the tutorial here: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#renderer

Related

Export JTable with custom TableCellRenderer

I have a JTable that uses a TableCellRenderer to colour the background of some cells according to their values.
I need to export this table in an excel file with the same cell formatting.
Here is an extract of what I currently see in the JTable.
Do I have to replicate all the logic behind the renderer in Apache POI to get the same result, or there is a quicker way?
You still need to create a workbook, a worksheet, rows and then cells.
Iterate through your model for each row, and create row and cells ( based on number of columns ).
Set cell's value based on your table's data.
For color :
It will help if your JTable's model has not just the values for each cell, but also its back ground color. If not, you could get background color from the JTable given the values for row, column.
You need to apply a cellstyle for each cell ( cell styles can be created independent of number of cells ) and set background color using setFillBackgroundColor().

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.

Mimic default behaviour of row rendering in a JTable

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

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