Scrollable JPanel without scrollBar - java

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

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.

Swing: how to add two JScrollPanes with TableLayout?

For my Java program I am actually using the simple library TableLayout as layout for my main JPanel body so that I can add any widget just by specifying its row and column index, for example"
body.add(new JLabel(
"Search by date"),
"1,8");
Now I would need to add two JScrollPane (one horizontal and one vertical) but they should include all the body and not just a single cell of the layout. Shall I add another JPanel? How can I do it?
Now I would need to add two JScrollPane (one horizontal and one
vertical) but they should include all the body and not just a single
cell of the layout. Shall I add another JPanel?
IMO, yes you should. Nesting Layouts is a common approach that could be applied in this way:
Create a new JScrollPane and set your panel as its viewport view.
Give the scroll pane a reasonable preferred size to enable the scroll bars if your panel's size exceeds this preferred size.
Have a wrapper panel with BorderLayout and add the scroll pane to its CENTER location.
In a nutshell:
JScrollPane scrollPane = new JScrollPane(yourPanel);
scrollPane.setPreferredSize(new Dimension(400, 300));
JPanel wrapperPanel = new JPanel(new BorderLayout());
wrapperPanel.add(scrollPane);
See also:
How to Use Scroll Panes

How to set the size of JTextPane according to the size of JPanel?

I want to set the size of the JTextPane according to the size of the panel so that when i add other panels, it changes accordingly. But it just gives a small text pane in the center and when i add some text, it's size changes accordingly.
JPanel panel = new JPanel();
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane();
pane.add(txt);
panel.add(pane,BorderLayout.CENTER);
add(pane);
now the jtextpane just appears at the center of the screen like a small box. I want it to appear according to the size of the panel
JPanel uses FlowLayout by default which sizes components according to their preferred sizes. You can use BorderLayout which will use the maximum area possible.
Also using constraints such as BorderLayout.CENTER has no effect unless the container is actually using BorderLayout. Dont add components to the JScrollPane. This will replaces all components within the view of the component. Instead set the JTextPane as the ViewPortView, for example
JPanel panel = new JPanel(new BorderLayout());
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane(txt);
// pane.add(txt); remove
panel.add(pane, BorderLayout.CENTER);
Read:
How to Use BorderLayout
How to Use Scroll Panes
You added pane twice. Add panel to your base (a JFrame?) instead and remember to actually set your JPanel to use BorderLayout.

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)

make a jscrollpane (containing a jtable) fill its container

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));

Categories

Resources