Reputation: 125
I am trying to create a JScrollPane
that contains a JPanel
that will be increasing and decreasing in height. When it becomes larger than the size of the JScrollPane
, it should create a vertical scroll bar which will allow me to scroll through the entire JPanel
. However, I am having difficulty achieving this. Yes, I know I am not using LayoutManager
s. No, I will not be using them, and I need a solution that does not involve their usage.
Here are the two button's AbstractAction
s that add and subtract from the JPanel
:
class AddACT extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
info.setSize(420,info.getHeight() + 40);
info.add(new SubPanel); // Adds another JPanel into the main JPanel (for content input)
gui.repaint();
infoS.validate();
}
}
class RemoveACT extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
info.remove(subPanel()); // This would remove the last JPanel added to the main JPanel
info.setSize(420,info.getHeight() - 40);
gui.repaint();
infoS.validate();
}
And here is the code for the main JPanel and the JScrollPane:
final JPanel info = new JPanel();
final JScrollPane infoS = new JScrollPane(info, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
info.setLayout(null);
info.setSize(420,600);
infoS.setLocation(10,80);
infoS.setSize(420,490);
gui.add(infoS); // gui is the frame's content pane (the overall JPanel)
This is the second project I've been trying to learn GUI by doing. I am a complete novice in Swing and am only intermediate in Java. Sorry if I am making a blindingly obvious mistake.
Upvotes: 1
Views: 10174
Reputation: 121
LayoutManager
is not required to solve the problem. The problem in Thrfoot's example is in these lines:
final JScrollPane infoS = new JScrollPane(info, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
info.setLayout(null);
info.setSize(420,600);
The program appears to recognize there is a need for scroll bars (it would show the scroll bar if your setting was VERTICAL_SCROLLBAR_AS_NEEDED
), but the actual scrolling does not work (the scroll bar slider is not there).
To fix this, first set the preferred size of info
, then construct the infoS
.
Example:
info.setPreferredSize(420,600);
final JScrollPane infoS = new JScrollPane(info, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
The idea is to set the preferred size of the info
panel before it is used for the scroll pane. This is the same reason to set the size and location of infoS
before adding to the gui
:
infoS.setLocation(10,80);
infoS.setSize(420,490);
gui.add(infoS); // gui is the frame's content pane (the overall JPanel)
Upvotes: 2
Reputation: 36423
1) Use LayoutManager
s (+1 to @kleopatra and @GagandeepBali comments)
The absence of LayoutManager
s only guarantees your GUI's will look very trashy (especially when run on other OSes/builds) and being a Novice you should rather learn the correct way than learn the wrong way and get into bad habits like calling setSize()
etc.
Have a read on these links to get you started:
2) See this example for how to use a JScrollPane
, it simply adds a JPanel
with buttons to a JScrollPane
which in-turn is added to the JFrame
.
3) Also see this example for how to make the JScrollPane
vertically scroll-able only.
4) For more on JScrollPane
s have a look here: How to Use Scroll Panes.
5) As for how it interacts with LayoutManager
, if you do not explicitly set its size via setPreferredSize(Dimension d)
the scroll pane computes it based on the preferred size of its nine components (the viewport, and, if present, the two scroll bars, the row and column headers, and the four corners)
6) On your usage of validate()
:
validate()
is used when new JComponent
s are added to a visible component
revalidate()
is used when JComponent
is removed/added from a visible component
revalidate()
covers validate()
too
Thus always use this:
//add or remove component(s)
revalidate();
repaint();
References:
Upvotes: 7