I'm working with JTable, and I want two things:
I would like to add another table header to my JTable, is this possible? Both of them in horizontal.
I want it's add a separator when the one of the attributes change its value, is this possible?
How can I do these things? See here the table as I want it.
Yes, you can add a second header. You can also add a separator by using the setBorder() method.
table.add(new JScrollPane(table));
table.setBorder(BorderFactory.createLineBorder(Color.RED,1));
Related
I've read how to create a JTable on JScrollPane with data in code, but how can I create a empty table with column header without adding data to it?
I want to add data to it from a form - not from my code.
If you use Netbeans for Development, it will be quite easy to work with JTables.
Using Netbeans, simply select Table from the palate and put it wherever you wish it to display.
Then in properties, you can change column names, add new columns and delete older. Also there only you will get an option of rows to display, simply put that to zero (0).
Voila! Your table is ready with column names and No Rows.
I have a JTable object. How can I get a reference to the JScrollPane the JTable is in?
I tried getParent() but that returns null.
EDIT:
My Bad.
As mentioned in one of the comments I'm trying to create my custom JTable that has a row header by default without manually having to add it. The problem is to add the row header one needs to call scrollPane.setRowHeaderView(rowHeader);. The problem is that I was calling getParent() in the constructor befpre the table actually has a parent and before jScrollPane1.setViewportView(jTable1); was called.
So this can't be solved in one step but only afterwards.
I think this is what you need:
JScrollPane scrollPane = (JScrollPane)SwingUtilities.getAncestorOfClass(JScrollPane.class, myTable);
You can find more information about this method here.
How do I remove unused rows from a JTable?
I first create a table of a fixed size in a JPanel and then fill elements as needed.
Now I don't want unused rows to be displayed in my table. Please help.
I presently get a table with lots of empty rows in bottom, only top 5-6 rows being used, with rest blank. I want to hide them or remove them somehow
Work the other way around. Start with an empty DefaultTableModel. DefaultTableModel supports an addRow() method. So, as you get data to add to the model use:
model.addRow(...);
Don't let the GUI editor determine how you code your GUI. Adding cleanup code to remove rows is a bad design.
DefaultTableModel has a method removeRow which removes a row from the table. Pass the row index which you want to remove from the table.
Now I don't want unused rows to be displayed in my table.
This I am not sure please post an SSCCE to show us what is the flag that says these rows are not used.
Is there a way to make, let say first 3 out of 7, columns fixed in Cell table. I want to be able to see always first three columns and have horizontal scroll on others.
You will have to create a custom widget that consists of a ScrollPanel, which includes two CellTable widgets set side by side. The right table should be wrapped in a FlowPanel with overflow-x property set to AUTO (overflow-y should remain at HIDDEN).
You can use the same DataProvider for both tables to synchronize all changes. Be careful with the SelectionModel though, if you need it. I would limit selection to the first column of checkboxes and disable selection by clicking on a row.
Make sure that your widget fits into its space, or you may end up with two horizontal scrollbars - one for the ScrollPanel and one for the right table. Finally, remember to set sizes on both tables so that they have the same height.
I want to create a JTable with a header inside some cells. But I want to associate a text with this header. Is it possible? How to do?
All my best!
Leandro Lima
If I understand the question then you can create a panel using a BorderLayout. You add the table to the CENTER and the table header to the NORTH.
table.getColumnModel().getColumn(x).setHeaderValue( "text" );
I only way to do it I found was to create a table, without cells and with a column with a header title. So I get this header and put it inside other table by cell renderer.