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.
Related
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));
I have two panel forms; one with JTable with populated data. I would like to have the entire table passed to another table in another panel form. I believe I need to overload the constructor in the second panel form with the DefaultTableModel, but I want to ensure I have the correct code in the first panel to pass that data via a button called "report". Any ideas would be much appreciated in advance. Thank you.
How to create jTable in JTextPane using null layout?
There are multiple approaches. E.g. insert table http://java-sl.com/JEditorPaneTables.html
You can add JTable as usual component using add() method and setting bounds to the JTable.
The third way is to call insertComponent() passing the JTable
one question with the java jTable class. Actually I am not a Java programmer and just now using Java to design a GUI in Matlab. What I've done is:
A jTable is built into a Matlab GUI.
I used/called a RowFilter in jTable, which can make the jTable to show the filtering results.
Then from the results in this filtered view I used the removeRow method from table model to remove one or several selected rows.
The problem is that everytime if I remove a row, the table content refreshs itself as wanted, but the scroll bar jumps back to the beginning.
Does anyone know how to inhibit this jumping and keep the original view of jTable? Because this helps me not to have to scroll back to find the original position where I started the deleting.
Thank u for ur advice and help.
Invoke the table's scrollRectToVisible() method; pass it the Rectangle returned by getCellRect() for the desired row.
I have a problem in my table model to update the data I print in it.
I have the class AgendaTableModel who is the table model, and the class Screen that is the main UI, when I press the button 'Listar Contatos' the table model should appear on JScrollPane in the center of the JFrame, but it continues blank.
What should be the problem ?
You should really post the code or better an SSCCE.
Here's the Oracle's tutorial on JTable.
I'll give you some hints:
Each JTable has a TableModel associated
You don't display the TableModel but the JTable, that is a view of your model
When you add components dynamically you should revalidate the parent component, so if you are adding a JTable somewhere, try to revalidate its container.
If you are trying to add the JTable to an already existing JScrollPane (empty or containing something else), consider to instantiate a new JScrollPane rather than updating its content.