make a jscrollpane (containing a jtable) fill its container - java

I have a JTable with autoResizeMode set to AUTO_RESIZE_LAST_COLUMN. I have added it to a panel by creating a JScrollPane with the JTable as its child widget, and then adding the JScrollPane to the panel.
I would like to set the size of the JScrollPane viewport to that of the parent JPanel, and have the JTable resize its last column dynamically.

JPanel have got implemented FlowLayout by defaut, you can place JScrollPane to the BorderLayout.CENTER

I think if you make the JPanel use GridLayout(1,1) and add the JScrollPane to it then you will get the desired result.

The first answer on this link How to make a JTable fill entire available space? worked perfectly for me.
I did the following
myPanel = new JPanel(new GridLayout());
myJtable = new JTable(MyTableModel);
myPanel.add(new JScrollPane(myJtable));

Related

I need to extract the panel of my JScrollPane

I need to know which method gives you the panel panelA from my scrollpane.
For example, I have:
PanelExample panelA = new PanelExample();
JScrollPane scrollpane = new JScrollPane(panelA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollpane.getPanel(); //???
scrollpane.getComponent(); //???
I guess it should be a get method, but I don't know.
You get the component from the JViewport of the JScrollPane:
Component c = scrollPane.getViewport().getView();
Read the section from the Swing tutorial on How to Use Scroll Panes for information on how the scroll pane works and the components involved in the layout of the scroll pane.

JTable header is not showing in a JScrollPane

I want to add a JTable into a JScrollPane, but the tableheader is not showing. Could anybody can help me figure out this?
JScrollPane tablePane = new JScrollPane();
tablePane.setViewportView(table);
tablePane.setRowHeaderView(table);
tablePane.setPreferredSize(new Dimension(950, 450));
this.setPreferredSize(new Dimension(1100, 500));
this.setLayout(new BorderLayout());
this.add(BorderLayout.CENTER, tablePane);
Below code should help.
JScrollPane tablePane = new JScrollPane(table);
//tablePane.setViewportView(table);
//tablePane.setRowHeaderView(table);
Seems you want to make the JTable scrollable. When you look into the documentation of JTable, you'll notice that JTable is already scrollable. So you don't need to add a JTable to a JScrollPane.
Also you should not add the JTable as ViewportView and as RowHeaderView of the JScrollPane simultaneously. The RowHeaderView of JScrollPane does not just take the RowHeaderView of the JTable. Instead you'll get two JTables side by side.
To fix this just set the viewportView of the JScrollPane or leave the JScrollPane completly out and use the JTable as standalone container.

Scrollable JPanel without scrollBar

I have a problem with scroling JPanel,
I have a lot of labels and fields which are generated dynamicly, but my frame can't show it all.
My code:
JPanel showPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(showPanel);
add(scrollPane);
scrollPane.setBounds(0, 0, 400, 400);
scrollPane.setVisible(true);
scrollPane.add(Jbuttons);
And Im adding a lot of these buttons but my scrollPane won't show it.
I don't have any scrollBar, with text area I didn't have any problems.
Do you have any idea?
scrollPane.setBounds(0, 0, 400, 400); looks like you're using a null layout, don't this
scrollPane.add(Jbuttons); isn't how you should be adding content to the scroll pane, instead, add it to the showPanel which is already inside the JScrollPane. JScrollPane contains a single component, the JViewport, you can not "add" components to the JScrollPane, you must set the JViewports view to what you want shown and the manipulate this view
Take a look at How to Use Scroll Panes for more details

Preventing JList from auto resizing

I have a JList inside a JScrollPane that's placed in a JPanel (BorderLayout.CENTER) and putting that inside another JPanel's BorderLayout.EAST (this JPanel's CENTER contains another JPanel) and this whole JPanel is placed inside a JTabbedPane. Initially, it would look like this:
Now I add some books to the list:
If I go to another tab and come back, this happens:
What I don't understand is that, the JPanel containing the JList has both its minimum and maximum size set:
JPanel listPanel = new JPanel();
listPanel.setLayout(new BorderLayout());
listPanel.add(new JScrollPane(bookList), BorderLayout.CENTER);
listPanel.setMinimumSize(listPanel.getPreferredSize());
listPanel.setMaximumSize(listPanel.getPreferredSize());
checkOutPanel.add(listPanel, BorderLayout.EAST);
How can I prevent the JList from auto resizing?
Depending on what it is you trying archive, you can either use JList#setPrototypeCellValue or JList#setFixedCellWidth. These will feed back into the PeferredScrollableViewportSize method which will effect the scroll pane

JScrollpane doesn't display properly

I have added a scroll pane to the main panel of my frame. But it doesn't display properly, here's what I get that appears on the right:
http://postimage.org/image/extp3ncql/
here is the code:
JScrollPane jScrollPane = new JScrollPane(area);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setViewportBorder(new LineBorder(Color.RED));
pane.add(jScrollPane, BorderLayout.EAST);
EDIT: Forgot to mention that area is a label.
1) Use another proper LayoutManager, I'd suggesst use Box or directly BoxLayout
or
2) all areas excluding BorderLayout.CENTER acepted PreferredSize came from JComponent
3) if your area is JTextArea the you can pretty to set JTextArea(int rows, int columns)

Categories

Resources