I have a JTabbedPane with 2 JPanels set to GridLayout(13, 11). The first JPanel has enough of the cells filled out that it leaves the empty cells.
The second JPanel has significantly fewer cells filled and this results in each button getting stretched to fill an entire row.
Is there any way to get GridLayout to honor the empty cells, so the buttons in both JPanels are the same size?
Is there any way to get GridLayout to honor the empty cells, so the buttons in both JPanels are the same size?
It is certainly doable with GridLayout, simply 'fill' the blank squares with a JLabel that has no text.
E.G. Here are two grid layouts, both padded to 3 rows.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.LineBorder;
class FillGridLayout {
public static final JComponent getPaddedGrid(
ArrayList<BufferedImage> images, int width, int height) {
JPanel p = new JPanel(new GridLayout(height, width, 2, 2));
p.setBorder(new LineBorder(Color.RED));
int count = 0;
for (BufferedImage bi : images) {
p.add(new JButton(new ImageIcon(bi)));
count++;
}
for (int ii=count; ii<width*height; ii++ ) {
// add invisible component
p.add(new JLabel());
}
return p;
}
public static void main(String[] args) {
final ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
int s = 16;
for (int ii = s/4; ii < s; ii+=s/4) {
images.add(new BufferedImage(ii, s, BufferedImage.TYPE_INT_RGB));
images.add(new BufferedImage(s, ii, BufferedImage.TYPE_INT_RGB));
}
Runnable r = new Runnable() {
#Override
public void run() {
JPanel gui = new JPanel(new BorderLayout(3,3));
gui.add(getPaddedGrid(images, 3, 3), BorderLayout.LINE_START);
gui.add(getPaddedGrid(images, 4, 3), BorderLayout.LINE_END);
JOptionPane.showMessageDialog(null, gui);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
Use nested layouts to get your desired result. Some layouts respect the preferred size of components and some don't. GridLayout is one of the ones that don't. Have a look at this answer to see which one's do and which one's don't.
For example, you could nest the 13 buttons in a GridLayout nested in another JPanel with a FlowLayout
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
JPanel p2 = new JPanel(new GridLayout(13, 1));
for (int i = 0; i < 13; i++) {
p2.add(new JButton("Button " + i));
}
p1.add(p2);
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test6 {
public Test6() {
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEADING));
JPanel p2 = new JPanel(new GridLayout(13, 1));
for (int i = 0; i < 13; i++) {
p2.add(new JButton("Button " + i));
}
p1.add(p2);
JFrame frame = new JFrame("Test Card");
frame.add(p1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Test6 test = new Test6();
}
});
}
}
add empty JLabel to the empty cell
content.add(new JLabel(""));
Related
I'm starting with swing, I have some questions about how to align elements within a GridBagLayout, I'm not sure either whether this is the correct approach, please advice.
I have the below code
import javax.swing.*;
import java.awt.*;
public class App {
public void start() {
JPanel mainPanel = new JPanel(new FlowLayout());
mainPanel.setBorder(BorderFactory.createLineBorder(Color.CYAN, 20));
//buttons for initial options
JButton button1 = new JButton("This is option A");
JButton button2 = new JButton("option B");
JButton button3 = new JButton("Another text");
JPanel second = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
second.setBackground(Color.GREEN);
second.add(button1, gbc);
second.add(button2, gbc);
second.add(button3, gbc);
mainPanel.add(second, BorderLayout.CENTER);
//frame configuration
JFrame frame = new JFrame();
frame.setContentPane(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
SwingUtilities.invokeLater(() -> new App().start());
}
}
My goal is to produce the following output:
So far I have tried with BoxLayout with vertical alignment and it works but the problem is that it overwrites the preferred sized of the buttons and I want them all to be the same width.
Also, I tried with GridLayout and BorderLayout adding the elements to NORTH, CENTER, and SOUTH but the sizes of the buttons change.
What is the recommended way to center the elements but keeping their dimensions?
I would nest layouts:
A JPanel that holds the buttons and uses a new GridLayout(0, 1, 0, vGap) -- a grid that holds one column and variable number of rows, with a vGap gap between buttons.
Add that JPanel into another JPanel that uses GridBagLayout, and add it in a default way (no GridBagConstraints) which will center the first JPanel into the second. This would obviously have to somehow be the size desired. This can be achieved by either
overriding getPreferredSize() in a sensible way
Calling setPreferredSize(new Dimension(someWidth, someHeight)) -- this isn't quite as "clean"
Giving this a border, specifically a BorderFactor.EmptyBorder(gap, gap, gap, gap) where gap is the size of the border around the JPanel...
Done.
Test code that uses the GridBagLayout:
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.*;
public class ButtonLayout extends JPanel {
public static final int MY_WIDTH = 750;
public static final int MY_HEIGHT = 500;
private static final float BTN_SIZE = 24f;
private String[] buttonTexts = {"This is Option A", "Option B",
"Something Else Entirely"};
public ButtonLayout() {
int colGap = 20;
JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 0, colGap));
for (String btnText : buttonTexts) {
JButton button = new JButton(btnText);
// set first letter of text as mnemonic (alt-char shortcut)
int mnemonic = (int) btnText.charAt(0);
button.setMnemonic(mnemonic);
// make button bigger by increasing its font
button.setFont(button.getFont().deriveFont(BTN_SIZE));
// add to the GridLayout-using JPanel
buttonPanel.add(button);
}
// set layout of main panel to GridBag
setLayout(new GridBagLayout());
// add the button panel in a "default" manner (no constraints)
// which centers this panel
add(buttonPanel);
}
#Override
public Dimension getPreferredSize() {
Dimension superSize = super.getPreferredSize();
int width = Math.max(MY_WIDTH, superSize.width);
int height = Math.max(MY_HEIGHT, superSize.height);
return new Dimension(width, height);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
ButtonLayout mainPanel = new ButtonLayout();
JFrame frame = new JFrame("ButtonLayout");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Example 2 that uses EmptyBorder:
import java.awt.GridLayout;
import javax.swing.*;
#SuppressWarnings("serial")
public class ButtonLayout extends JPanel {
public static final int MY_WIDTH = 750;
public static final int MY_HEIGHT = 500;
private static final float BTN_SIZE = 24f;
private String[] buttonTexts = {"This is Option A", "Option B",
"Something Else Entirely"};
public ButtonLayout() {
int colGap = 20;
JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 0, colGap));
for (String btnText : buttonTexts) {
JButton button = new JButton(btnText);
// set first letter of text as mnemonic (alt-char shortcut)
int mnemonic = (int) btnText.charAt(0);
button.setMnemonic(mnemonic);
// make button bigger by increasing its font
button.setFont(button.getFont().deriveFont(BTN_SIZE));
// add to the GridLayout-using JPanel
buttonPanel.add(button);
}
add(buttonPanel);
int top = 60;
int left = top;
int bottom = 2 * top;
int right = left;
setBorder(BorderFactory.createEmptyBorder(top, left, bottom, right));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
ButtonLayout mainPanel = new ButtonLayout();
JFrame frame = new JFrame("ButtonLayout");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
I'm not sure I completely understand the issue, but if you want to vertically align the buttons, BUT allow them to keep their preferred size, just don't provide any kind of fill constraint, for example
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SoTest {
public static void main(String[] args) {
new SoTest();
}
public SoTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(new JButton("This is option A"), gbc);
add(new JButton("Option B"), gbc);
add(new JButton("Another button"), gbc);
}
}
}
Or, if you want them to have the same width, use a fill constraint
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SoTest {
public static void main(String[] args) {
new SoTest();
}
public SoTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.BOTH;
add(new JButton("This is option A"), gbc);
add(new JButton("Option B"), gbc);
add(new JButton("Another button"), gbc);
}
}
}
If you want to mix a more complex layout, then you should consider making use of compound layouts
But wait, there's no outline...
So, a number of ways you "might" be able to do this, for example, you could use a CompoundBorder....
setBorder(new CompoundBorder(new LineBorder(Color.CYAN, 16), new EmptyBorder(32, 32, 32, 32)));
But the devil is in the detail
How I can create a JPanel with a lot of buttons aligned float left and with vertical scrollbars only?
The buttons should be sorted like below.
1 2 3 4
5 6 7 8
9 10 11 12
If you use GridLayout, then you will not be able to add a scrollpane since it will resize automatically to fit all the components inside of it. An easier approach is to use a FlowLayout and setPreferredSize(...) to set the size of your panel. Though it is not advised to set the size of panels, you still need to have the scrollbar put into use somehow. Here is a MCVE:
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Example extends JFrame {
private final int BUTTON_WIDTH = 100;
private final int BUTTON_HEIGHT = 50;
private final int BUTTON_ROWS = 3;
private final int BUTTON_COLUMNS = 4;
private final int OFFSET = 20;// the width of the actual scroll bar in pixels (approximately).
private final int PANEL_WIDTH = BUTTON_WIDTH * BUTTON_COLUMNS + OFFSET;
private final int PANEL_HEIGHT = BUTTON_HEIGHT * BUTTON_ROWS + OFFSET;
private final int SCROLL_HEIGHT = 100;//or whatever you would like...
private final JButton[] buttons = new JButton[BUTTON_ROWS * BUTTON_COLUMNS];
public Example() {
JPanel panel = new JPanel(new FlowLayout());
JScrollPane scroll = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
scroll.setPreferredSize(new Dimension(PANEL_WIDTH + OFFSET, SCROLL_HEIGHT));
for (int i = 0; i < buttons.length; i++) {
JButton button = new JButton((i + 1) + "");
buttons[i] = button;
button.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
panel.add(button);
}
//if you want the panel to resize when window is stretched.
//setLayout(new FlowLayout(FlowLayout.CENTER));
add(scroll);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
new Example();
}
}
Add the buttons to a (panel with a) grid layout to arrange them in rows and columns. Add that panel to a scroll pane, then add the scroll pane to the line start constraint of a border layout, and they will appear on the left.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class LeftAlignedButtonGrid {
private JComponent ui = null;
LeftAlignedButtonGrid() {
initUI();
}
public void initUI() {
if (ui!=null) return;
/* BorderLayout offers a LINE_START constraint that will put a
single child component on the left hand side of the GUI (in any
locale that uses left-to-right text orientation) */
ui = new JPanel(new BorderLayout(4,4));
JPanel buttonPanel = new JPanel(new GridLayout(0,4,2,2));
for (int ii=1; ii<13; ii++) {
buttonPanel.add(new JButton("" + ii));
}
ui.add(new JScrollPane(buttonPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
BorderLayout.LINE_START);
ui.setBorder(new EmptyBorder(4,4,4,4));
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
LeftAlignedButtonGrid o = new LeftAlignedButtonGrid();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
// comment this out to allow the height of the GUI to be reduced,
// thus making the vertical scroll bar to have a purpose!
//f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
I have a JPanel, that lists a set of Jlabels. I would like to make the width of each label wide as the panel's size. So it will be wide but but text stays in the left. I am using BoxLayout to list labels.
Here is the code:
public class JavaApplication78 {
JFrame frame;
JPanel panel, labelsPanel;
JLabel label;
ArrayList<String> names = new ArrayList<String>();
ArrayList<JLabel> labelsArray = new ArrayList<JLabel>();
Border paddingBorder = BorderFactory.createEmptyBorder(10,10,10,10);
Border border = BorderFactory.createLineBorder(Color.BLUE);
JScrollPane labelsScroll;
public JavaApplication78(){
frame = new JFrame();
panel = new JPanel(new BorderLayout());
names.add(".mp3");names.add(".html");names.add(".jpeg");names.add(".mp4");names.add(".pdf");
labelsPanel = new JPanel();
labelsPanel.setLayout(new BoxLayout(labelsPanel, BoxLayout.PAGE_AXIS));
labelsScroll = new JScrollPane(labelsPanel);
for(String format : names){
label = new JLabel(format);
//icon
labelsArray.add(label);
labelsPanel.add(label);
label.setBorder(BorderFactory.createCompoundBorder(border,paddingBorder));
}
panel.add(labelsScroll, BorderLayout.CENTER);
frame.add(panel);
frame.setSize(200, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JavaApplication78();
}
}
Currently I could give a border around each JLabel. The height of labels are ok, but width need to be as wide as the parent panel.
Any idea ?
You could use a GridBagLayout...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
public class JavaApplication78 {
JFrame frame;
JPanel panel, labelsPanel;
JLabel label;
ArrayList<String> names = new ArrayList<String>();
ArrayList<JLabel> labelsArray = new ArrayList<JLabel>();
Border paddingBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Border border = BorderFactory.createLineBorder(Color.BLUE);
JScrollPane labelsScroll;
public JavaApplication78() {
frame = new JFrame();
panel = new JPanel(new BorderLayout());
names.add(".mp3");
names.add(".html");
names.add(".jpeg");
names.add(".mp4");
names.add(".pdf");
labelsPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
labelsScroll = new JScrollPane(labelsPanel);
for (String format : names) {
label = new JLabel(format);
//icon
labelsArray.add(label);
labelsPanel.add(label, gbc);
label.setBorder(BorderFactory.createCompoundBorder(border, paddingBorder));
}
panel.add(labelsScroll, BorderLayout.CENTER);
frame.add(panel);
frame.setSize(200, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
new JavaApplication78();
}
});
}
}
Or a JList, but in cases like this, I tend to like using the VerticalLayout from the SwingLabs SwingX library
BoxLayout respects the maximum size of the component and for a JLabel the maximum size is equal to the preferred size.
You can override the getMaximumSize() method:
label = new JLabel(format)
{
#Override
public Dimension getMaximumSize()
{
Dimension d = super.getMaximumSize();
d.width = Integer.MAX_VALUE;
return d;
}
};
label.setHorizontalAlignment(JLabel.CENTER);
This will then allow the label to expand horizontally to fill the width of the panel.
Or if you wanted you could use the Relative Layout. It functions much like the BoxLayout but has a few additional features. The basic code would be:
RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setFill( true );
JPanel labelsPanel = new JPanel( rl );
Use PreferredSize to set the dimension of the parent JPanel for your JLabels :
JLabel label = new JLabel() {
public Dimension getPreferredSize() {
return labelsPanel.getSize();
};
};
Unless you are obliged to use BoxLayout, maybe it's better to use GridBagLayout as suggested by #MadProgrammer in his comment.
EDIT :
You could also take a look at MigLayout. Adding components with it is very simple :
labelsPanel.setLayout(new MigLayout());
labelsPanel.add(label, "span") // span to take the whole row width.
I have a problem in that I'm trying to get my program to print out one particular JPanel as a 2x2 GridLayout with Icons of playing cards on top, and text indicating which player's card is it on bottom, but no matter what I do, the result is reverse, like so. I have tried changing the order in which I add the elements to no avail.
CardTable myCardTable
= new CardTable("CS 1B CardTable", NUM_CARDS_PER_HAND, NUM_PLAYERS);
myCardTable.setSize(800, 600);
myCardTable.setLocationRelativeTo(null);
myCardTable.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// show everything to the user
myCardTable.setVisible(true);
myCardTable.getGamePanel().setBorder(new TitledBorder("Playing Area"));
myCardTable.getGamePanel().add(playerCard, JLabel.CENTER);
myCardTable.getGamePanel().add(computerCard, JLabel.CENTER);
for(int i = 0; i < NUM_PLAYERS; i++)
{
JLabel temp = new JLabel(GUICard.getIcon(generateRandomCard()));
myCardTable.getGamePanel().add(temp);
}
Below is the constructor of the class that I made, CardTable, that extends JFrame.
static int MAX_CARDS_PER_HAND = 57;
static int MAX_PLAYERS = 2; // for now, we only allow 2 person games
private int numCardsPerHand;
private int numPlayers;
private JPanel computerPanel, playerPanel, gamePanel;
public CardTable(String title, int numCardsPerHand, int numPlayers)
{
super(title);
setComputerPanel(new JPanel(new GridLayout(1 , numCardsPerHand)));
setPlayerPanel(new JPanel(new GridLayout(1 , numCardsPerHand)));
setGamePanel(new JPanel(new GridLayout(2 , numPlayers)));
setLayout (new BorderLayout(20, 10));
add(getComputerPanel(), BorderLayout.NORTH );
add(getGamePanel(), BorderLayout.CENTER);
add(getPlayerPanel(), BorderLayout.SOUTH);
}
"GridLayout with Icons of playing cards on top, and text indicating which player's card is it on bottom,"
You can put text and an icon in the same label and just set the text positions with setXxxTextPosition(). Maybe you'd prefer this. IMO it looks much cleaner than separating the icon and text so far apart from each other.
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
public class PlayerCard {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ImageIcon playerIcon = new ImageIcon(getClass().getResource("/resources/1.png"));
ImageIcon compIcon = new ImageIcon(getClass().getResource("/resources/14.png"));
JLabel playerLabel = new JLabel(playerIcon);
JLabel compLabel = new JLabel(compIcon);
playerLabel.setText("Player Card");
compLabel.setText("Computer Card");
playerLabel.setVerticalTextPosition(JLabel.BOTTOM);
compLabel.setVerticalTextPosition(JLabel.BOTTOM);
playerLabel.setHorizontalTextPosition(JLabel.CENTER);
compLabel.setHorizontalTextPosition(JLabel.CENTER);
JPanel playerPanel = new JPanel();
playerPanel.setBorder(new TitledBorder("Player"));
playerPanel.add(playerLabel);
JPanel compPanel = new JPanel();
compPanel.setBorder(new TitledBorder("Computer"));
compPanel.add(compLabel);
JPanel panel = new JPanel();
panel.add(playerPanel);
panel.add(compPanel);
JOptionPane.showMessageDialog(null, panel);
}
});
}
}
UPDATE
If you really must stick with the GridLayout, you need to make sure to add the cards first, then add the text labels.
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class PlayerCard {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ImageIcon playerIcon = new ImageIcon(getClass().getResource("/resources/1.png"));
ImageIcon compIcon = new ImageIcon(getClass().getResource("/resources/14.png"));
JLabel playerLabel = new JLabel(playerIcon);
JLabel compLabel = new JLabel(compIcon);
JLabel playerText = new JLabel("Player Card");
JLabel compText = new JLabel("Computer Card");
playerLabel.setHorizontalAlignment(JLabel.CENTER);
compLabel.setHorizontalAlignment(JLabel.CENTER);
JPanel playerCardPanel = new JPanel();
playerCardPanel.add(playerLabel);
JPanel compCardPanel = new JPanel();
compCardPanel.add(compLabel);
JPanel panel = new JPanel(new GridLayout(2, 2));
panel.add(playerCardPanel);
panel.add(compCardPanel);
panel.add(playerText);
panel.add(compText);
JOptionPane.showMessageDialog(null, panel);
}
});
}
}
I have been trying for hours to find a way to solve the issue, but I had no luck with that. Here is a sample code:
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class Example extends JFrame
{
private static final long serialVersionUID = 1L;
public Example()
{
JPanel contentPane = (JPanel) getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel panTop = new JPanel(new BorderLayout());
//JPanel panBottom = new JPanel(new BorderLayout());
JPanel panTopCenter = new JPanel();
//JPanel panTopLeft = new JPanel();
//JPanel panTopRight = new JPanel();
panTop.add(panTopCenter, BorderLayout.CENTER);
//panTop.add(panTopLeft, BorderLayout.WEST);
//panTop.add(panTopRight, BorderLayout.EAST);
contentPane.add(panTop, BorderLayout.CENTER);
//contentPane.add(panBottom, BorderLayout.SOUTH);
JPanel pan = new JPanel();
pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));
for(int i = 0; i < 50; i++) pan.add(new JButton("Button " + i));
JScrollPane scrollPane = new JScrollPane(pan);
panTopCenter.add(scrollPane);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new Example();
}
});
}
}
Snapshot:
I always have to set the viewport's preferred size like this.
import java.awt.*;
import javax.swing.*;
public class Example extends JFrame {
public Example() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Box box = new Box(BoxLayout.Y_AXIS);
for (int i = 0; i < 50; i++) {
box.add(new JButton("Button " + i));
}
JScrollPane sp = new JScrollPane(box);
Dimension d = new Dimension(box.getComponent(0).getPreferredSize());
sp.getVerticalScrollBar().setUnitIncrement(d.height);
d.height *= 10; // Show at least 10 buttons
sp.getViewport().setPreferredSize(d);
add(sp, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Example e = new Example();
}
});
}
}
Don't set any sizes! The scroll-bar appears if this change is made.
JPanel panTopCenter = new JPanel(new GridLayout());
The basic problem is that FlowLayout will show components at the smallest size needed to display it, and for a scroll-pane, that is (decided to be) 0x0. By using a GridLayout with no arguments in the constructor and adding the scroll-pane as the only component, it will be forced to fill the available space.
You have to set the preferred-size, in the case that JScrollPane is single JComponent in the container or top-level container.
scrollPane.setPreferredSize(new Dimension(100,500));
Better would be to use GridLayout for the same type of JComponents.
The best solution, quick and easy, is using JXPanel from SwingX, which is quasi-standard and implements Scrollable.