Reputation: 1641
I have a GUI with two boxes: a text area and a label
First box with working scroll bar:
public class ResultView {
private JTextArea resultsTextArea;
private JPanel resultsPanel;
private JScrollPane scrollPane;
public ResultView() {
createGUI();
connectPanel();
}
private void createGUI() {
resultsTextArea = new JTextArea("Command results:");
resultsTextArea.setPreferredSize(new Dimension(380, 2000));
resultsTextArea.setBackground(Color.BLACK);
resultsTextArea.setForeground(Color.WHITE);
resultsTextArea.setEditable(false);
scrollPane = new JScrollPane(resultsTextArea);
scrollPane.setPreferredSize(new Dimension(400, 300));
}
private void connectPanel() {
resultsPanel = new JPanel();
resultsPanel.add(scrollPane);
}
Second box that isn't showing scroll bar:
public class DrawView {
private JLabel drawView;
private JPanel drawViewPanel;
private JPanel drawViewPaintPanel;
private int currentNumWagons;
private int currentTrain = -1;
private int OFFSET = 100;
private int TRAINLENGTH = 100;
private JScrollPane scrollPane;
public DrawView() {
createGUI();
connectPanel();
}
private void createGUI() {
drawView = new JLabel("DrawView:");
drawView.setPreferredSize(new Dimension(800,20));
drawViewPaintPanel = new JPanel();
drawViewPaintPanel.setPreferredSize(new Dimension(790, 280));
scrollPane = new JScrollPane(drawView);
scrollPane.setPreferredSize(new Dimension(800,300));
}
private void connectPanel() {
drawViewPanel = new JPanel(new BorderLayout());
drawViewPanel.add(drawView, BorderLayout.NORTH);
drawViewPanel.add(drawViewPaintPanel, BorderLayout.CENTER);
drawViewPanel.add(scrollPane);
}
Any idea how to fix this issue? I need to solve this issue because the action button is adding trains to the label, when adding more than two trains, the third train gets under the label. I'll add a picture of my GUI down here to give you a view how my GUI is designed:
Upvotes: 0
Views: 232
Reputation: 324197
drawView.setPreferredSize(new Dimension(800,20));
Get rid of all the setPreferredSize() statements.
It is the job of the layout manager to determine the size of the panel based on the components added to the panel.
The scrollbar will appear when the preferred size of the component is greater than the size of the component. So the preferred size needs to be able to change dynamically as you add components to the panel.
drawViewPanel.add(drawViewPaintPanel, BorderLayout.CENTER);
drawViewPanel.add(scrollPane);
If you don't specify a constraint when adding comoponents to a BorderLayout will be placed in the CENTER. However only a single component can be displayed in the CENTER so the scrollPane replaces the drawViewPaintPanel.
resultsTextArea = new JTextArea("Command results:");
resultsTextArea.setPreferredSize(new Dimension(380, 2000));
For a JTextArea you can suggest a preferred size by using:
resultsTextArea = new JTextArea(5, 30);
resultsTextArea.setText("Command results:");
//resultsTextArea.setPreferredSize(new Dimension(380, 2000));
The text area will now have 5 lines of text. After 5 lines the scrollbars will appear.
Upvotes: 1