JScrollPane doesn't work for my JPanel - java

first of all I must say that I have checked these questions and didn't find my answer :
1 , 2 , 3 , 4 , 5 , 6 , 7
and many other questions like so
also I have checked these tutorials and examples:
1 , 9 , 10 , 11
and many other sites. but I couldn't fix my problem.
and
this is the simple kind of my code:
public class Question extends JFrame {
public Question() {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLayout(new BorderLayout());
setSize(d.width, d.height);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(d.width, d.height));
panel.setBorder(BorderFactory.createLineBorder(Color.red));
panel.setLayout(new BoxLayout(panel, 1));
for (int i = 0; i < 100; i++) {
panel.add(new JButton("kjdh"));
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setPreferredSize(new Dimension(500, 500));
getContentPane().add(scrollPane);
getContentPane().add(panel);
setVisible(true);
}
public static void main(String[] args) {
new Question();
}
}
but the JScrollPane doesn't appear. I have tested many things. I have changed the way adding panel and scrollPane to my frame but it didn't work. can any one help me plz?

Don't set a preferred size on the panel. See Should I avoid the use of setPreferred/Maximum/MinimumSize methods in Java Swing? for the reasons why.
Add only the scroll pane to the content pane.
A content pane using the default layout (BorderLayout) will default to putting the component in the CENTER constraint if none is supplied, and the CENTER area can only accept a single component.
Besides that, the panel has already been added to the scroll pane, it will already appear inside it, and can only appear in a single container.
Don't extend frame, just use an instance of one.
Don't setSize, but setExtendedState.
GUIs should be constructed and updated on the EDT.
A better close operation is DISPOSE_ON_CLOSE.
import java.awt.*;
import javax.swing.*;
public class Question {
public Question() {
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.red));
panel.setLayout(new BoxLayout(panel, 1));
for (int i = 0; i < 100; i++) {
panel.add(new JButton("kjdh"));
}
JScrollPane scrollPane = new JScrollPane(panel);
f.getContentPane().add(scrollPane);
f.pack();
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new Question();
}
};
SwingUtilities.invokeLater(r);
}
}

You've added an unecessary duplicate panel on the context pane. Instead of:
getContentPane().add(scrollPane);
getContentPane().add(panel);
use only
getContentPane().add(scrollPane);
It makes sense as a scrool pane is a container for a panel, so it's enough to add a container on the context pane.

Related

Adding JScrollPane to a JFrame

I want to add JPanel containers to a JScrollPane and add this scroll pane to a JFrame. But when I add multiple panels to the scroll pane this happens. The gap between the scroll pane and the top bar increases. I use BoxLayout as layout manager for all the components that I use.
Here is my take on laying out this GUI. Some notes:
Rather than use a BoxLayout in the JScrollPane it puts a GridLayout in the PAGE_START of a BorderLayout. This is fine for when it's OK to stretch the elements in the scroll pane to the full width of the GUI. Stick to a BoxLayout (which I rarely use) or a GridBagLayout if it's necessary to keep the elements at their preferred size.
This strategy of layout is basically 'divide and conquer' in that it starts with the smallest sub-divisions of the GUI (e.g. the FlowLayout for the buttons) and then adds those containers to larger containers with different layouts and constraints (e.g. adding that button panel to the LINE_END of a BorderLayout - to push I to the right of the GUI) as needed for the overall effect.
I'd also consider using a JList (using a panel for the renderer) in the scroll pane. It depends on the use as to whether that makes sense.
Note that this code is an MRE. An MRE should have everything that's needed (including imports, a class structure and the main method) for another person to compile and run the code.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.ActionEvent;
// ref: https://stackoverflow.com/a/70934802/418556
public class ScrollPaneTestGUI {
int elementCount = 1;
JPanel elementsPanel = new JPanel(new GridLayout(0,1,2,2));
public ScrollPaneTestGUI() {
initGUI();
}
private void initGUI() {
// this will become the content pane of the frame
JPanel gui = new JPanel(new BorderLayout(4,4));
gui.setBorder(new EmptyBorder(4,4,4,4));
JPanel pageStartPanel = new JPanel(new BorderLayout(2,2));
gui.add(pageStartPanel, BorderLayout.PAGE_START);
pageStartPanel.add(new JLabel("LINE START label"), BorderLayout.LINE_START);
// default flow layout is good for this one
JPanel buttonPanel = new JPanel();
pageStartPanel.add(buttonPanel, BorderLayout.LINE_END);
buttonPanel.add(new JButton("Does Nothing"));
Action addToScrollAction = new AbstractAction("Add to scrollPane") {
#Override
public void actionPerformed(ActionEvent e) {
elementsPanel.add(getPanelForScroll());
elementsPanel.revalidate();
}
};
JButton addToScrollButton = new JButton(addToScrollAction);
buttonPanel.add(addToScrollButton);
JPanel scrollPanel = new JPanel(new BorderLayout());
scrollPanel.add(elementsPanel, BorderLayout.PAGE_START);
gui.add(new JScrollPane(scrollPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)
);
for (int ii=0; ii<2; ii++) {
elementsPanel.add(getPanelForScroll());
}
JFrame frame = new JFrame("ScrollPane GUI");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setContentPane(gui);
frame.pack(); // sets the GUI to the exact size needed
frame.setMinimumSize(frame.getSize());
frame.setVisible(true);
}
private JPanel getPanelForScroll() {
JPanel p = new JPanel();
p.add(new JLabel("Panel " + elementCount++));
p.setBorder(new EmptyBorder(10,200,10,200));
p.setBackground(Color.GREEN);
return p;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new ScrollPaneTestGUI();
}
};
SwingUtilities.invokeLater(r);
}
}

A panel using FlowLayout cannot contain JScrollPanes?

That's what I did at first.
public class MyFrame extends JFrame {
public MyFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(500 ,300));
setResizable(false);
pack();
setLocationRelativeTo(null);
initComponents();
}
private void initComponents() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for (int i=0; i < 100; i++)
panel.add(new JLabel("some text"));
JScrollPane scrollPane = new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
// Here I create a JPanel to replace the contentPane of JFrame
JPanel contentPane = new JPanel();
contentPane.add(scrollPane);
setContentPane(contentPane);
}
If instead I replace the last 3 lines with this:
getContentPane().add(scrollPane);
everything is ok. But as I did before, the vertical scrollbar is not showing up. What is causing this? Is setting a JPanel as contentPane wrong?
Update:
If contentPane changes to BorderLayout everything work fine.
// Here I create a JPanel to replace the contentPane of JFrame
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.add(scrollPane);
setContentPane(contentPane);
So the problem is default FlowLayout?
Solved:
The problem is FlowLayout. It wraps around JScrollPane and hides the Toolbars. using
scrollPane.setPreferredSize(new Dimension(500, 400)); // longer space in x-axis
solves it.
Answer:
JSrollPane should not be used inside a Container that uses FlowLayout.
First of all - there is nothing bad in using your own component as content pane. But default content pane is also a JPanel instance so there is actually no point to replace it with your own panel, unless you want to use non-panel content pane or customized panel component.
This is how the default content pane looks like:
/**
* Called by the constructor methods to create the default
* <code>contentPane</code>.
* By default this method creates a new <code>JComponent</code> add sets a
* <code>BorderLayout</code> as its <code>LayoutManager</code>.
* #return the default <code>contentPane</code>
*/
protected Container createContentPane() {
JComponent c = new JPanel();
c.setName(this.getName()+".contentPane");
c.setLayout(new BorderLayout() {
/* This BorderLayout subclass maps a null constraint to CENTER.
* Although the reference BorderLayout also does this, some VMs
* throw an IllegalArgumentException.
*/
public void addLayoutComponent(Component comp, Object constraints) {
if (constraints == null) {
constraints = BorderLayout.CENTER;
}
super.addLayoutComponent(comp, constraints);
}
});
return c;
}
This method is taken from JRootPane. It is basically a simple JPanel with a but customized layout manager as you can see.
Now, you have a few problems in your example.
First is the order of calls - you are sizing frame before adding content into it. Simply change the order and you will see your scrollpane:
public class MyFrame extends JFrame
{
public MyFrame ()
{
super();
// Add components first
initComponents ();
// Setup frame after so it fits its new content
setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
setPreferredSize ( new Dimension ( 500, 300 ) );
setResizable ( false );
pack ();
setLocationRelativeTo ( null );
}
private void initComponents ()
{
JPanel panel = new JPanel ();
panel.setLayout ( new BoxLayout ( panel, BoxLayout.Y_AXIS ) );
for ( int i = 0; i < 100; i++ )
{
panel.add ( new JLabel ( "some text" ) );
}
JScrollPane scrollPane =
new JScrollPane ( panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
// Here I create a JPanel to replace the contentPane of JFrame
JPanel contentPane = new JPanel ();
contentPane.add ( scrollPane );
setContentPane ( contentPane );
}
public static void main ( String[] args )
{
SwingUtilities.invokeLater ( new Runnable ()
{
public void run ()
{
new MyFrame ().setVisible ( true );
}
} );
}
}
It will still look different because your new JPanel () uses FlowLayout by default, instead of BorderLayout used by default content pane component:
Simply set BorderLayout and you will have the result you want to see:
JPanel contentPane = new JPanel ( new BorderLayout () );
Have a look at Rob Camick's WrapLayout, which is an extension of FlowLayout
import java.awt.*;
import javax.swing.*;
public class TestWrapLayout {
public TestWrapLayout () {
ImageIcon icon = new ImageIcon(getClass().getResource("/resources/stackoverflow2.png"));
JPanel panel = new JPanel(new WrapLayout());
for (int i = 1; i <= 250; i++) {
JLabel iconlabel = new JLabel(icon);
iconlabel.setLayout(new BorderLayout());
JLabel textlabel = new JLabel(String.valueOf(i));
textlabel.setHorizontalAlignment(JLabel.CENTER);
textlabel.setForeground(Color.WHITE);
textlabel.setFont(new Font("impact", Font.PLAIN,20));
iconlabel.add(textlabel);
panel.add(iconlabel);
}
JFrame frame = new JFrame();
frame.add(new JScrollPane(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new TestWrapLayout();
}
});
}
}
You must update your frame after adding your panel using pack() function. When you do
getContentPane().add(scrollPane);
the function add does that for you (ref)
Firstly, what was the reason to create the second panel? There
is already the first panel that has BoxLayout as the layout
manager set. Simply setting the scroll pane having the first panel
as parent works as expected.
You either call
setContentPane(scrollPane);
or
add(scrollPane);
Now I am going to explain what caused this unexpected behaviour. This
is a quirk that sometimes happens to those who use nesting technique
when building their layouts. When nesting is used, the layouts may
influence each other.
By choosing another layout -- FlowLayout -- as the underlying base layout,
you caused the first panel to be displayed in its preferred size. Instead
of one panel, you have now two panels, the base panel influences the
panel with labels -- it controls how it is sized. The FlowLayout
shows all its children in preferred size; it does not honour mimimum nor
maximum sizes. So the (first) visible panel is sized to show all its labels;
this is how preferred size is calculated -- just big enough to show all its
children. However, with 100 labels, it is very big; the layout is broken. It is
vertically so big that we cannot practically get to the bottom of the window.
So with our visible panel showing all its labels, what's the purpose of showing
a verticall scrollbar? No need for one, since all labels are "visible" (placed on
the window area), though the desing is broken.
So the problem does not lie with the scrollbars; they work normally. If you
set (in your example) the vertical scrollbar policy to VERTICAL_SCROLLBAR_ALWAYS
you will see the scrollbar there but without a slider, since all labels are "visible"
and there is nothing to be scrolled. (Scrollbars show items that are hidden from the
layout.) The issue lies in the fact that FlowLayout shows its components in the preferred size only.
The following is a fixed code example that works as expected:
package com.zetcode;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class MyFrame extends JFrame {
public MyFrame() {
initComponents();
setTitle("Scrollbar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 200));
pack();
setLocationRelativeTo(null);
}
private void initComponents() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for (int i=0; i < 100; i++)
panel.add(new JLabel("some text"));
JScrollPane scrollPane = new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
setContentPane(scrollPane);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MyFrame ex = new MyFrame();
ex.setVisible(true);
}
});
}
}

How to arrange the Components in multiple rows in a Scrollpane

I need to add about 600 Images to a Scrollpane, but all the Images are arranged side-by-side
public CollectionPanel(Controller controller)
this.setBackground(Color.white);
this.setLayout(new BorderLayout());
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
for(int i = 0; i < 100; ++i){
content.add(new Sticker(i+1));
}
scrollPane.setViewportView(content);
this.add(scrollPane, BorderLayout.CENTER);
}
How is it possible to arrange them to make a "linebreak" when they reached the end of the screen?
Have a look at Rob Camick's WrapLayout.
You can resize the frame, and all your components will be reformatted for you. Here's an example usage
import java.awt.*;
import javax.swing.*;
public class TestWrapLayout {
public TestWrapLayout () {
ImageIcon icon = new ImageIcon(getClass().getResource("/resources/stackoverflow2.png"));
JPanel panel = new JPanel(new WrapLayout());
for (int i = 1; i <= 250; i++) {
JLabel iconlabel = new JLabel(icon);
iconlabel.setLayout(new BorderLayout());
JLabel textlabel = new JLabel(String.valueOf(i));
textlabel.setHorizontalAlignment(JLabel.CENTER);
textlabel.setForeground(Color.WHITE);
textlabel.setFont(new Font("impact", Font.PLAIN,20));
iconlabel.add(textlabel);
panel.add(iconlabel);
}
JFrame frame = new JFrame();
frame.add(new JScrollPane(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new TestWrapLayout();
}
});
}
}
Use GridLayout instead of FlowLayout that fits the components side by side.
JPanel content = new JPanel();
content.setLayout(new GridLayout(rows,1));
//pass no of rows with just one column
You can try with BoxLayout as well.
JPanel content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
Please have a look at Using Layout Managers & A Visual Guide to Layout Managers for more info with working sample codes.
Myself, I would try to keep things as simple as possible by putting images, ImageIcons actually, into a JList which can handle ImageIcons quite well. Give it a horizontal wrap set its visible row count to 0, plop it into a JScrollPane, and there you go.

Setting frame size when using GridLayout in Swing

In the below code, I am using gridbaglayout to have all my buttons displayed in two lines on a frame size of 600 X 400. Commenting out setSize() or pack() in the below code did not help. my question is how to get frame of size 600 X 400, and the bottom of frame has a panel with alpabet buttons. Thanks for help.
import javax.swing.*;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.*;
public class HangmanGUI {
public static void main(String[] args){
new HangmanGUI();
}
//constructor for Hangman
/**
* Instantiates a new hangman gui.
*/
public HangmanGUI() {
JFrame myframe= new JFrame();
JPanel myPanel = new JPanel();
myPanel.setLayout(new GridLayout());
GridBagConstraints gbc = new GridBagConstraints();
myframe.setSize(600,400);
int x =0; int y=5;
for (char alphabet = 'A';alphabet<='Z';alphabet++){
gbc.gridx=x;
gbc.gridy=y;
myPanel.add(new JButton(alphabet+""),gbc);
x++;
if (x>15){
y =6;x=0;
}
}
myframe.add(myPanel);
myframe.pack();
myframe.setTitle("Hangman Game");
myframe.setVisible(true);
myframe.setLocationRelativeTo(null);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
When packed, the frame size is computed based on the preferred size of it's content pane.
Personally, I'd care less about the window size and the functionality of the program and let the underlying framework figure it all out...but, if it's important to you...
Start with, something like, a JPanel and override it's getPreferredSize method...
public class BigPane extends JPanel {
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
}
Set this panel as the frame's content pane...
JFrame myframe= new JFrame();
myFrame.setContentPane(new BigPane());
// This is important as the panels default layout is FlowLayout...
myFrame.setLayout(new BorderLayout());
If you want you button pane to positioned in the south position, then you simply need to supply the correct layout constraint for the layout, in this case, BorderLayout...
myFrame.add(myPanel, BorderLayout.SOUTH);
This will allow you to add a "main" component to the CENTER position.
As has already being suggested, it might be better to use a GridLayout for the buttons, but this will depend on what you want to achieve.
Take a look at Laying out components within a container for more details
In your last Q&A you were advised to use GridLayout as opposed to GridBagLayout. GridBagConstraints are only used in the latter.
This is probably not how you want the GUI to look, but take it as a basic guide of what GridLayout is actually good for.
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class HangmanGUI {
/**
* Instantiates a new hangman gui.
*/
public HangmanGUI() {
JPanel gui = new JPanel(new BorderLayout(2,2));
BufferedImage bi = new BufferedImage(
600,200,BufferedImage.TYPE_INT_RGB);
gui.add(new JLabel(new ImageIcon(bi)));
JFrame myframe= new JFrame();
JPanel myPanel = new JPanel();
gui.add(myPanel,BorderLayout.PAGE_END);
myPanel.setLayout(new GridLayout(2,0,0,0));
int x =0; int y=5;
for (char alphabet = 'A';alphabet<='Z';alphabet++){
myPanel.add(new JButton(alphabet+""));
x++;
if (x>15){
y =6;x=0;
}
}
myframe.add(gui);
myframe.pack();
myframe.setTitle("Hangman Game");
myframe.setVisible(true);
myframe.setLocationRelativeTo(null);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new HangmanGUI();
}
}
JFrame myframe= new JFrame();
myframe.getContentPane().setLayout(new BorderLayout());
JPanel myPanel = new JPanel();
myPanel.setLayout(new GridLayout(2,13));
GridBagConstraints gbc = new GridBagConstraints();
myframe.setSize(600,400);
int x =0; int y=5;
for (char alphabet = 'A';alphabet<='Z';alphabet++){
gbc.gridx=x;
gbc.gridy=y;
myPanel.add(new JButton(alphabet+""),gbc);
x++;
if (x>15){
y =6;x=0;
}
}
myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
myframe.setTitle("Hangman Game");
myframe.setVisible(true);
myframe.setLocationRelativeTo(null);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I added the BorderLayout to the ContentPane and then added the Panel to the bottom of the ContentPane.
Also removed the pack.
And added the constraints in your new GridLayout to specify 2 rows of 13 columns

Keep BoxLayout From Expanding Children

I want to stack some JComponents vertically inside a JPanel so they stack at the top and any extra space is at the bottom. I'm using a BoxLayout. The components will each contain a JTextArea that should allow the text to wrap if necessary. So, basically, I want the height of each of these components to be the minimum necessary for displaying the (possibly wrapped) text.
Here's a contained code example of what I'm doing:
import javax.swing.*;
import java.awt.*;
public class TextAreaTester {
public static void main(String[] args){
new TextAreaTester();
}
public TextAreaTester(){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.PAGE_AXIS));
panel.setPreferredSize(new Dimension(100,400));
for(int i = 0; i<3; i++){
JPanel item = new JPanel(new BorderLayout());
JTextArea textarea = new JTextArea("this is a line of text I want to wrap if necessary");
textarea.setWrapStyleWord(true);
textarea.setLineWrap(true);
textarea.setMaximumSize( textarea.getPreferredSize() );
item.add(textarea,BorderLayout.NORTH);
panel.add(item);
}
panel.add(Box.createGlue());
frame.add(panel);
frame.setVisible(true);
frame.pack();
}
}
The child JPanels are expanding to fill the vertical space. I tried using glue because I thought that's what glue was for, but it seems to do nothing at all. Any help?
Note: I have found questions that look almost identical, but none with answers I can apply.
One solution: nest JPanels with the outer JPanel using Borderlayout and adding the BoxLayout using JPanel to this one BorderLayout.NORTH, also known as BorderLayout.PAGE_START:
Edit for Kleopatra:
import javax.swing.*;
import java.awt.*;
public class TextAreaTester {
public static void main(String[] args) {
new TextAreaTester();
}
public TextAreaTester() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
// panel.setPreferredSize(new Dimension(100,400));
for (int i = 0; i < 3; i++) {
JPanel item = new JPanel(new BorderLayout());
// item.setLayout(new BoxLayout(item,BoxLayout.LINE_AXIS));
JTextArea textarea = new JTextArea(
"this is a line of text I want to wrap if necessary", 3, 35);
textarea.setWrapStyleWord(true);
textarea.setLineWrap(true);
// textarea.setMaximumSize(textarea.getPreferredSize());
// item.setMaximumSize( item.getPreferredSize() );
item.add(new JScrollPane(textarea), BorderLayout.NORTH);
panel.add(item);
}
panel.add(Box.createGlue());
JPanel mainPanel = new JPanel(new BorderLayout()) {
private final int prefW = 100;
private final int prefH = 400;
#Override
public Dimension getPreferredSize() {
return new Dimension(prefW, prefH);
}
};
// mainPanel.setPreferredSize(new Dimension(100, 400));
mainPanel.add(panel, BorderLayout.PAGE_START);
frame.add(mainPanel);
frame.setVisible(true);
// frame.getContentPane().add(jp);
frame.pack();
}
}
Alternatively, you can use Box.Filler. Just replace your call to panel.add(Box.createGlue()) with
panel.add(new Box.Filler(new Dimension(0, 0),
new Dimension(0, Short.MAX_VALUE),
new Dimension(0, Short.MAX_VALUE)));
If you want to achieve the same for a horizontal layout, just use Short.MAX_VALUE for width instead of height in the Dimension call.

Categories