JScrollPane - how does it display JTable headers? - java

What part of JScrollPane code is responsible for displaying the column headers of JTable?
If you just add a JTable to a JPanel, it won't show the headers by default. You should either pass the table to JScrollPane's constructor or call JScrollPane setViewportView with the table as the argument. So actually, what makes the column headers visible? Is it part of the internal rendering of the JScrollPane (updateUI and getUI methods)?
Initially I thought JScrollPane uses its setColumnHeaderView to accomplish this, but it doesn't (pass null to this method, the table will still display the headers).

The JTable is responsible for doing this.
The addNotify() method of JTable is overridden. Basically this method is invoked when a component is added to a visible container.
So the JTable implementation checks if the parent of the table is a JViewport. If so it adds the table header to the scroll pane using the setColumnHeaderView(...) method.

Related

How to hide Jtable in netbeans?

I hide the table usingaccountTable.setVisible(false); but now it it not showing whenver accountTable.setVisible(true);is called. please tell me how to solve it or give an alternative option for hiding Jtable.The jtable is in a jpanel already
try calling the jpanel (where the jTable is on) revalidate() and repaint() functions after setting the table visible
an alternative option for hiding Jtable.
A JTable is typically displayed in a JScrollPane. So one approach would be:
scrollPane.setViewportView( null ); // to hide the table
and then:
scrollPane.setViewportView( table ); // to show the table.
This assumes that you have space on the panel to actually show the table. If there is no space on the panel to reshow the table then you may need to pack() the frame as well.

How to render a Component in JTable using TableModel?

I have successfully displayed a JTable using an AbstractTableModel, but I want to add delete button for each row in the last column, in the getValueAt method that returns Object there is no way that I can return JButton, JLabel or any JComponent that is clickable. I tried that and I only get the object description toString.
Is there another solution to render JComponent in a JTable without using the TableModel approach?
Is there another solution to render JComponent in JTable without using the TableModel approach?
The TableModel is used to hold the data for the data for the model.
The JTable implements the view of the data for each column. The renderer is just a picture of the data. You can easily render the data to look like a button, however a renderer does not respond to any event.
The JTable does support editors and that is how you interact with real components. When you edit a normal cell a JTextField is placed in the cell location so you can type data into the cell and then the data is save to the model.
So if you want to click on a button then you need to use a button as an editor.
Check out Table Column Button for a class that uses a JButton as the renderer and editor. You then provide the class an Action to be invoked when the button is clicked.
Read the section from the Swing tutorial on Concepts: Renderers and Editor for more information. There is also a section on Using Other Editors.
One way : TableColumn.setCellEditor(jbutton_instance) on a hand added column.

How to reset a JTable

I have a question with respect to JScrollPane and a JTable.
I have added a JTable to a JScrollPane, and added the JScrollPane to the JPanel. When I click on 'show' button, the JTable will be filled with contents from the database.
I also have another button reset, clicking on which will remove the contents of the JTable, and the JScrollPane. It is supposed to be doing that, but what happens is that, even after clicking the button, all the contents of the JTable and the JScrollPane still exists.
I used revalidate(), reinstantiate(), etc, but of no use. How do I make it work?
Assuming you are using a DefaultTableModel, then you just do:
model.setRowCount(0);
In order to remove a row from a JTable, you need to remove the target row from the underlying TableModel. If, for instance, your TableModel is an instance of DefaultTableModel, you can remove a row by doing the following:
((DefaultTableModel)myJTable.getModel()).removeRow(rowToRemove);
Updation 2
To know the rows and delete from jtable
int rows = myJTable.getRowCount();
for(int i=0;i<rows;i++)
((DefaultTableModel)myJTable.getModel()).removeRow(i);

retrieve changed contents of a jtable contained in a jscrollpane

I have a Jtabbedpane that holds a JscrollPane which in turn holds a Jtable.
I would like to access the dynamically changed contents of the JTable via the JScrollPane. I have tried the following but I am stuck up at this point.
JScrollPane c = (JScrollPane)MyTabbedPane.getSelectedComponent()
Now c, the scrollPane has the changed table.
I saw that c.getComponents is returning two viewports and two scrollbars but not the Jtable.
Please tell me how can I access the JTable that the JScrollPane holds.
JViewport viewport = scrollPane.getViewport();
JTable table = (JTable)viewport.getView();
try this,to get table in JScrollPane...
jScrollPane.getViewport().getComponents()
Hope this helps...

Clearing a JScrollPane but can't then 'attach' tables

I am creating an application where I have 3 Panes with buttons which dynamically create buttons in the next pane depending on the selection, clicking the last button shows a table of data brought up via an SQL query:
[buttonPane1][buttonPane2][buttonPane3][table]
If a user has clicked a button on all 3 panes and then wants to change their choice on buttonpanel1, it will bring up the choices in buttonpanel2 and using
buttonPanel3.removeAll();
buttonPanel3.repaint();
I can clear the third button panel, my problem is how to clear the table. I want to remove it from the Table ScrollPanel however if I try
tableScrollPanel.removeAll();
it just means that the table never shows.
How can I remove any current table but allow a table to be 'reattached' I am doing this to create and 'attach' the table
jTableTemp.setModel(new DefaultTableModel(
tableContent, tableTitles));
tableScrollPanel.setViewportView(jTableTemp);
Thanks Very Much
Try setting the table model to a DefaultTableModel with empty data and the original headers, then repaint. As long as you have a JScollPane wrapped around the JTable, the headers should show up, assuming this is what you want.
Other wise, you can set the viewport to a new instance of a JTable with new headers.

Categories