Reputation: 2646
I have a code where I have a lot of 'nested' layouts. Border, Flow, and in my main program I have a GridBag. I'm adding panels dynamically to one panel, and have it sized so that each element gets added vertically but it won't scroll up or down. I looked on the net and found something similar but that only alows mine to scroll horizontally and not vertically like i want. This is my test code that I wrote:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Main {
public Main() {
JFrame frame = new JFrame("Blah");
Font font = new Font("Sans-Serif", Font.BOLD, 30);
JPanel memoryPanel = new JPanel();
memoryPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
//memoryPanel.setPreferredSize(new Dimension(80 * 4, 55 * 9));
JScrollPane s = new JScrollPane(memoryPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
s.getViewport().setPreferredSize(new Dimension(80 * 4, 55 * 9));
for (int i = 0; i <= 10; i++) {
JPanel nextPanel = new JPanel();
nextPanel.setLayout(new BorderLayout());
nextPanel.setBackground(Color.CYAN);
nextPanel.setForeground(Color.BLACK);
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING, 0, 0));
JLabel label = new JLabel();
label.setText("" + i);
label.setPreferredSize(new Dimension(80 * 4, 55));
label.setHorizontalAlignment(JLabel.RIGHT);
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
JButton button1 = new JButton("A");
JButton button2 = new JButton("B");
JButton button3 = new JButton("C");
label.setFont(font);
labelPanel.add(label);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
nextPanel.add(labelPanel, BorderLayout.PAGE_START);
nextPanel.add(buttonPanel, BorderLayout.PAGE_END);
memoryPanel.add(nextPanel, 0);
memoryPanel.revalidate();
memoryPanel.repaint();
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(s);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
With that commented line, it shows the horizontal scroll bar. With it not commented, it doesn't show any. Not sure if I'm using the layouts wrong or not but I've been trying to fix it for a few hours. The line I added was s.getViewport().setPreferredSize(new Dimension(80 * 4, 55 * 9));
Which is making everything going horizontally instead of vertically. Not sure how to fix this. Suggestions?
Upvotes: 0
Views: 259
Reputation: 347334
Start by getting rid of s.getViewport().setPreferredSize(new Dimension(80 * 4, 55 * 9));
. You don't want to change the size of the view port, it's a container for your content and acts as the "window" showing the visible portion of the component.
Next, get rid of frame.setResizable(false);
, this will now allow pack to actually pack the window around the content. Because you're using a FlowLayout
, this tends to make the window as wide as the screen.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class Main {
public Main() {
JFrame frame = new JFrame("Blah");
Font font = new Font("Sans-Serif", Font.BOLD, 30);
JPanel memoryPanel = new JPanel();
memoryPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
//memoryPanel.setPreferredSize(new Dimension(80 * 4, 55 * 9));
JScrollPane s = new JScrollPane(memoryPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// s.getViewport().setPreferredSize(new Dimension(80 * 4, 55 * 9));
for (int i = 0; i <= 10; i++) {
JPanel nextPanel = new JPanel();
nextPanel.setLayout(new BorderLayout());
nextPanel.setBackground(Color.CYAN);
nextPanel.setForeground(Color.BLACK);
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING, 0, 0));
JLabel label = new JLabel();
label.setText("" + i);
label.setPreferredSize(new Dimension(80 * 4, 55));
label.setHorizontalAlignment(JLabel.RIGHT);
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
JButton button1 = new JButton("A");
JButton button2 = new JButton("B");
JButton button3 = new JButton("C");
label.setFont(font);
labelPanel.add(label);
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
nextPanel.add(labelPanel, BorderLayout.PAGE_START);
nextPanel.add(buttonPanel, BorderLayout.PAGE_END);
memoryPanel.add(nextPanel, 0);
memoryPanel.revalidate();
memoryPanel.repaint();
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(s);
// frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Main();
}
});
}
}
If you want to affect the size of the scrollable view area, then you will need to create a custom class which implements the Scrollable
interface and return appropriate values according to your needs
Upvotes: 2