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.
Related
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.
How to hide jTable at jFrame in java GUI, not just hide the column, but entire jTable?
I was try to use table.setVisible(false), but just hide a column not entire table
You cannot hide the table, but you can hide your panel containing the table
instead.
yourPanel.setVisible(false);
Whenever you want to show the table do yourPanel.setVisible(true);
When i try to hide the rows in a jTable, the jTable ScrollPanel need to adjust its size to remaining rows and, other components in the Frame also adjust to the change. i.e., there shouldn't be any space between the components and the jTable after hiding the table-rows..
ex: in microsoft excel, when we delete a row, the remaining rows will move upwards to fill the deleted row..
Please help me.. Thank you..
Edited:
Used a model that extends AbstractTableModel. Used RowFilter to hide the rows.
Problem is even when the rows disapper, the JScrollpane(where the JTable resides) wont get adjusted to the remaining rows..
You can use setPreferredScrollableViewportSize() to resize the scroll pane to the desired multiple of getRowHeight(). You can use validate() "to cause a Container to lay out its subcomponents again" or pack() to cause the Window "to be sized to fit the preferred size and layouts of its subcomponents."
Are you trying to hide rows or delete rows? You mention hiding rows in JTable but deleting them in Excel.
I have a JTable whose associated TableModel could be initially empty. Therefore, it currently shows a JTable with its columns and no rows.
In order to fill this JTable, I want the user to drag and drop elements from another component. The problem is that I would like to hint the user that he/she should drag elements to this table, with some message like "Drag xxx here to add a row".
I thought that I could achieve this by putting a panel over the JTable , but I don't think it is possible with any java layout.
Does anyone know how to do this? Or should I stick to a CardLayout to switch to/from the hint and the JTable?
Thanks a lot
Take a look at OverlayLayout, I think it might do what you want.
A couple of suggestions:
Add a tooltip to the JTable using setToolTipText(String). You'll need to add it to the surrounding JScrollPane in order for the tooltip to be displayed when the user hovers on the empty viewport.
Add a titled Border; e.g. scrollPane.setBorder(BorderFactory.createTitledBorder("Drag items here"));
I would do it like this...Create a JPanel to contain the JTable on top and a JLabel on the bottom. Something like this...
JPanel container = new JPanel( new BorderLayout() );
container.add( table, BorderLayout.CENTER );
JLabel label = new JLabel( "Drag XXX here to add a row" );
container.add( table, BorderLayout.SOUTH );
Then make both the label and table drop targets. You might need to play with the label to make it look and behave right. Set opaque to true possibly? Set its background to white to match the table? Also maybe play with the borders of all three to make them look integrated with each other. You could remove the label after the first row is added if that is the behavior you want.
I have Java application which adds JTextFields # runtime to JPanel. Basically user clicks a button and new JTextField is added, clicks again added again...
Each new JTextField is directly below the previous one. Obviously I run out of space pretty soon so I'm trying to use JScrollPane and thats where the hell begins, because it just doesnt work no matter what I try.
Right click on JPanel and Enclose in Scroll Pane. Didnt work.
After reading some examples I realized I must have JPanel as an argument for JScrollPane constructor. Which I did via right clicking on ScrollPane and CustomizeCode. Because apparently auto-generated code is protected in NetBeans and I cannot just change all those declarations, etc. manually. Still doesnt work.
I did try to set PreferedSize to null for JPanel and/or JScrollPane, didnt help.
JScrollPane is a child of lets call it TabJPanel (which in turn is a tab of TabbedPane). I tried to mess with their relationships, basically trying every possible way of parentship between JFrame, JPanel(holding textfields), TabJPanel and JScrollPane, but nothing worked.
I also made VerticalScrollBar "always visible" just in a case. So I see the scrollbar, it's just that populating that JPanel with JTextFields does not affect it.
When there are too many JTextFields I they go "below" the bottom border of JPanel and I cannot see them anymore.
Code for adding new JTextFields is like this, in a case it's relevant.
JTextField newField = new JTextField( columns );
Rectangle coordinates = previousTextField.getBounds();
newField.setBounds(coordinates.x , coordinates.y + 50, coordinates.width, coordinates.height);
JPanel.add(newField);
JPanel.revalidate();
JPanel.repaint();
Sorry for a long post I'm just trying to provide as much info as possible, because being newbie I dont know whats exactly relevant and whats not. Thanks in advance :)
As there is another answer now, I'm adding my suggestion too.
This sounds exactly like a problem to use a JTable with a single column. JList is not yet editable (and might never be).
JTable would handle the layout problems for you, and you can easily access the values via the table.
Use your own TableModel (a simple Vector should be sufficient in your case), and add values to it.
An option you have is to utilize a LayoutManager, instead of setting the bounds directly on the components. To test this, a simple single column GridLayout with the alignment set to vertical should prove the concept.
panel.setLayout(new GridLayout(0,1));
zero in the rows param allows for rows to be added to the layout as needed.
I do this way to add a scrollpane, create a panel and fill it with few components, then create a scrollpane in the component you want to add it, cut and paste the panel in which all your details will fall in and resize the scrollpane.Because the components take a larger space than the one visible right click on the scrollpane and select design this container, there you can increase the size of the scrollpane and add as many components as you have.