how to make a Jbutton as rectangle - java

I have a sortableTableModel. On click of column headers I sort the content. For sorting, column headers are treated as jbutton.
Now my jbutton are created with rounded corners so in my table column headers have rounded corner.
I want those as plane rectangles.
how can I achieve that?

You can do something like this -
TableColumn col = table.getColumnModel().getColumn(vColIndex);
col.setHeaderRenderer(new CustomTableHeaderRenderer());
And then create your own TableCellRenderer that extends whatever JComponent you wish.

You need to look in to TableCellRenderers and TableColumn.setHeaderRenderer

You will need to set the border/do some custom painting modifications to the buttons you are using in the table header.

if is your Java version 1.6 ++, than why not using default TableHeader that's came with JTable (by defalut returns JLabel) and with defalut RowSorter http://download.oracle.com/javase/tutorial/uiswing/components/table.html#sorting ,
but another situations could be if is implemented in your sortableTableModel custom RowFilter

Related

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

Extending (sliding down) JTable row

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

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.

Java JTable Resize Row

Is it possible by default to resize a JTable row in the same way you can resize a column by dragging the mouse at the header? If so, how can you do this?
Here is how you can do it:
http://www.jroller.com/santhosh/entry/make_jtable_resiable_better_than
It is more than you need but it is a good starting point if you really need a row header. Otherwise you can use it as it is.

Categories