Java JTable Resize Row - java

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.

Related

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.

Vertical scrollbar covering the last column data of jtable(last column data is right aligned)

I am using a jtable which have 8 rows by default. When I add a new row using a button click a vertical scrollbar comes into picture and covers up my data of last column in jtable which is right aligned. How can I overcome this?
Specify setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) on the JScrollPane. This will leave room for the scroll bar when (if) it becomes necessary.
Sounds like you need to give your table a little bit more space to account for the addition of a scroll bar. You will need to possibly mess with the preferredSize.

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.

Allowing users to resize columns of JTable

Im creating a table which is dynamically changing its width. And I add columns by time as well. And I have bounded this table to a scrollpane. Here I have set the auto resizing to false in the JTable (else it will fill the whole area from the beginning).
And I'm adding images to cells using cellrenderer as well. Now I need to allow users to resize this table columns.
Any idea?
I don't understand the question, by default the user is allowed to resize the columns by dragging the header of the column.
However if you want this to happen automatically then you can try using the Table Column Adjuster, which can automatically adjust the columns to fit the size of the data.
Well first of all - just change TableModel. You can leave column resizing on and for example set maximum width of cell by using setMaxWidth
Or just pick correct mode that fits your requirements. You can find them here.

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