So I made a program where a JPanel's size are dynamically updated by the click of a button and the updated panel is added to a JScrollPane. But I can't quite figure out the error in this code. The panel disappears on the click of the button instead of getting resized and put in a scrollPane. Help would be hugely appreciated as I am fairly new to Swing.
the code(hope i'm doing this right) :-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Frame extends JFrame implements ActionListener{
int scrollBarSize;
JButton button;
JPanel buttonPanel;
JPanel panel = new JPanel();
JScrollPane pane = new JScrollPane( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JTextField text;
int bigPanelPos = 495, bigPanelSize = 55 , smallPanelPos;
Frame(){
this.setSize(700,700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(null);
scrollBarSize = ((Integer)UIManager.get("ScrollBar.width")) 1;
pane.setBounds(0,0,700,550);
panel.setBackground(Color.yellow);
panel.setBounds(0,495,700,55);
panel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.setAlignmentY(Component.CENTER_ALIGNMENT);
button = new JButton("Click me!");
button.setPreferredSize(new Dimension(50,15));
button.setFont(new Font("Didot",Font.BOLD, 15));
button.addActionListener(this);
buttonPanel = new JPanel();
// buttonPanel.setPreferredSize(new Dimension(700, 150));
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
buttonPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
buttonPanel.setBorder(BorderFactory.createTitledBorder("ButtonPanel"));
buttonPanel.setBounds(0, 550, 700, 150 );
buttonPanel.add(button);
this.add(panel);
this.add(buttonPanel);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
bigPanelPos -= 55;
bigPanelSize =55;
int n = scrollBarSize;
panel.setPreferredSize(new Dimension(700 - n,bigPanelSize));
panel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.setAlignmentY(Component.CENTER_ALIGNMENT);
panel.revalidate();
pane.add(panel);
pane.revalidate();
this.add(pane);
// this.add(panel);
System.out.println(pane.getBounds());
System.out.println(panel.getBounds());
}
}
}
CodePudding user response:
Don't use a null layout.
It is the job of the layout manager to determine the size/location of components. The scrollbars of the scroll pane will only appear when the preferred size of the panel is greater than the size of the scroll pane.
Don't add the panel to the frame
The panel is a child component of the scroll pane. The panel must be added to the viewort of the scroll pane and the scroll pane must be added to the frame.
You add the panel to the viewport by using either:
JScrollPane pane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
or
panel.setViewportView( panel );
Read the Swing tutorial for Swing basics. There are section on "layout managers" and "How to Use a Scroll Pane" with more information and examples.