I just began Swing. I'm trying to make a gallery app. I'm getting images that I manually imported and I display them. Depending on the amount of columns I put in parameters, the images' dimensions are calculated to be displayed properly. But, after a certain amount of rows, I want a scrollbar that could scroll and show the rest of images.
The images are displayed properly, like I wished but I tried to implement the scrollbar and it isn't appearing.
Could you tell me what's wrong in my code ?
GUI(String title, int width, int height, int columns) {
this.frame = new JFrame();
this.frameWidth = width;
this.columns = columns;
// set Frame's size
frame.setSize(width, height);
// set Frame's action on close button clicked
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set Frame's title
frame.setTitle(title);
frame.setResizable(false);
// set Frame's position at screen's center
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - frame.getSize().width / 2, (dim.height / 2 - frame.getSize().height / 2));
panel = new JPanel();
frame.setLayout(new GridLayout());
this.scrollBar = new JScrollPane();
scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollBar, BorderLayout.EAST);
displayImages();
frame.setContentPane(panel);
}
displayImages() :
private void displayImages() {
for (File currentImage : getImagesList()) {
// 5 is flowlayout's default borders
// 2 because we got left and right borders
int totalBorders = (columns * 5) * 2;
int buttonWidth = (frameWidth - totalBorders) / columns;
ImageIcon image = new ImageIcon(new ImageIcon(currentImage.getAbsolutePath()).getImage().getScaledInstance(buttonWidth - 20, buttonWidth - 50, Image.SCALE_DEFAULT));
JButton button = new JButton(currentImage.getName());
button.setPreferredSize(new Dimension(buttonWidth, buttonWidth));
button.setIcon(image);
button.setHorizontalTextPosition(AbstractButton.CENTER);
button.setVerticalTextPosition(AbstractButton.BOTTOM);
button.addActionListener(e -> onImageClicked(currentImage.getName()));
panel.add(button);
}
}
Thanks, best regards
panel = new JPanel();
frame.setLayout(new GridLayout());
this.scrollBar = new JScrollPane();
scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollBar, BorderLayout.EAST);
displayImages();
frame.setContentPane(panel);
The above code is mostly wrong. You need to:
The basic code should be something like:
//panel = new JPanel( new GridLayout(0, 1) );
layout = new GridLayout(0, 1);
panel = new JPanel( layout );
displayImages();
//frame.setLayout(new GridLayout());
this.scrollBar = new JScrollPane(panel);
scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scrollBar, BorderLayout.EAST);
//displayImages();
//frame.setContentPane(panel);
Also, there is no need to play with the sizes of each component on the panel. The GridLayout will make all the buttons the same size.
Edit:
When you want to increase the columns in the GridLayout you then just do:
layout.setColumns( layout.getColumns() + 1 );
panel.revalidate(); // this invokes the layout manager
//panel.repaint(); // sometimes needed to force repainting of panel.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.