How do I arrange buttons vertically in a JOptionPane? - java

I'm trying to make a text-based adventure game where the top of the screen is a JTextArea inside a JScrollPane that shows what is happening, and the bottom is a JOptionPane where you click on a button to make a choice. By default, the buttons are arranged horizontally. The only problem is that if I have too many buttons, there is no room for new ones and they are pushed off the screen. I need them to be arranged vertically since they are fatter than they are tall. The JOptionPane and the JScrollPane are nested in a gridLayout, which is nested in a JFrame. This is the method I am using to make the frame:
/**
* Make the frame and everything in it
*/
private void makeFrame()
{
frame = new JFrame("Adventure!");
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
contentPane.setLayout(new GridLayout(0, 1));
textArea = new JTextArea(20, 50);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setFont(new Font("font", Font.BOLD, 15));
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(textArea);
optionPane = new JOptionPane("", JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION, null, null);
contentPane.add(optionPane);
frame.pack();
// place the frame at the center of the screen and show
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
frame.setVisible(true);
}

Instead of using a JOptionPane, use JButtons in a GridLayout. You can specify how many components you want across and down upon creation like this: new GridLayout(0, 3). This would result in 3 buttons stacked on top of each other, the first int being how many you want across, and the second, how many you want down. Try this:
/**
* Make the frame and everything in it
*/
private void makeFrame()
{
frame = new JFrame("Adventure!");
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));
contentPane.setLayout(new GridLayout(0, 1));
textArea = new JTextArea(20, 50);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setFont(new Font("font", Font.BOLD, 15));
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(textArea);
//This replaces your JOptionPane block
buttonPane = new JPanel();
buttonPane.setLayout(new GridLayout(0, 1));
contentPane.add(buttonPane);
frame.pack();
// place the frame at the center of the screen and show
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
frame.setVisible(true);
}

Related

java swing borderlayout adding panes difficulties

so basically, when I add two panes to container with BorderLayout I have a something like padding and I have no idea how to fix it
below the code is a pic of what I mean
Container mainContainer = this.getContentPane(); //
mainContainer.setLayout(new BorderLayout(8, 6));
mainContainer.setBackground(Color.BLACK);
this.getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.CYAN));
JPanel panelZTekstem = new JPanel();
panelZTekstem.setBackground(Color.ORANGE);
poleTekstowe.setEditable(false);
poleTekstowe.setText("0");
poleTekstowe.setSize(400, 100);
poleTekstowe.setOpaque(true);
poleTekstowe.setFont(new Font("MV Boli", Font.BOLD, 20));
poleTekstowe.setHorizontalAlignment(JTextField.RIGHT);
panelZTekstem.setLayout(new FlowLayout());
panelZTekstem.add(poleTekstowe);
mainContainer.add(panelZTekstem,BorderLayout.NORTH);
JPanel panelZLiczbami = new JPanel();
for (int i = 0; i <= 16; i++) {
JButton test = new JButton();
panelZLiczbami.add(test);
}
panelZLiczbami.setBackground(Color.BLUE);
mainContainer.add(panelZLiczbami, BorderLayout.CENTER);
when I add two panes to container with BorderLayout I have a something like padding
mainContainer.setLayout(new BorderLayout(8, 6));
What did you think the 8/6 values are used for?
You are creating a gap between the components.
It is best to read the API to understand how the parameters are used.

How can I center and widen out my JPanel in my JFrame?

I am making a copy of the apple calendar application, and I am having trouble aligning the month name and year name with the center of my screen, while aligning the left and right buttons with the left and right sides of the screen. Here is my code:
final JPanel months = new JPanel();
months.setLayout(new BoxLayout(months,BoxLayout.X_AXIS));
months.add(back, BorderLayout.WEST); //back is a JButton
JLabel monthName = new JLabel(this.monthNames[this.month]+" ", SwingConstants.CENTER);
JLabel year = new JLabel("" + this.year, SwingConstants.CENTER);
monthName.setFont(new Font("Helvetica", 0, 24));
year.setFont(new Font("Helvetica", 0, 24));
monthName.setHorizontalAlignment(JLabel.CENTER);
months.add(monthName, BorderLayout.CENTER);
months.add(year, BorderLayout.CENTER);
months.add(front, BorderLayout.EAST);
add(months);
Yet it shows up like this:
months.setLayout(new BoxLayout(months,BoxLayout.X_AXIS));
You are using a BoxLayout. A BoxLayout just adds the components horizontally to the panel. The WEST, CENTER, EAST constrains are only used by a BorderLayout so they are ignored by the BoxLayout.
months.add(monthName, BorderLayout.CENTER);
months.add(year, BorderLayout.CENTER);
When using a BorderLayout you can only add a single component to a region of the layout. So if you want to add two components to the CENTER you need to first create a panel and add the components to the panel.
So your basic code might be something like:
JPanel centerPanel = new JPanel();
centerPanel.add(month);
centerPanel.add(year);
JPanel mainPanel = new JPanel( new BorderLayout() );
mainPanel.add(westButton, BorderLayout.WEST);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(eastButton, BorderLayout.EAST);

How to make a JPanel use the scrollbars to fit in large J components?

I have this problem where the JPanel (contentPane) re-sizes (testLabel) GUI components to be really small whenever they are too big to fit in the panel. I have added scrollbars to the JPanel but the components still re-size instead of using the scrollbars. Here is my class where I use the JPanel with scrollbars.
package marsPackage;
import java.awt.*;
import javax.swing.*;
public class DashTab{
private JLabel testLabel; //test label
private JLabel testLabel2;
private JLabel testLabel3;
private JLabel testLabel4;
private JPanel dashPanel; //Panel that holds the JTabbedPane tab
private JPanel contentPane; // Panel that holds all GUI components
private JScrollPane scrollPane; // Scrollpane used on contentPane
/*
* Constructor
* All of your GUI components should be added to
* contentPane using the gridBagLayout.
*/
public DashTab(){
//Creating the dashpanel that holds everything
dashPanel = new JPanel();
dashPanel.setBackground(Color.WHITE);
//Creating the contentPane that holds all GUI components and
//uses vertical/horizontal sidebards as needed
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
//Giving the contentPane the GridBagLayout
contentPane.setLayout(new GridBagLayout());
GridBagConstraints g = new GridBagConstraints();
contentPane.setPreferredSize(new Dimension(857, 725));
//Adding scrollPane to Content Pane and adding those two to dashPanel
scrollPane = new JScrollPane(contentPane);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBackground(Color.WHITE);
dashPanel.add(scrollPane);
/*
* You may begin adding your GUI components from this point forward.
* Remember to only use GridBagLayout with GridBagConstraints using the
* g variable.
*/
testLabel = new JLabel("Testing Here 1");
testLabel.setPreferredSize(new Dimension(500, 500));
testLabel.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray));
g.gridx = 0;
g.gridy = 0;
contentPane.add(testLabel, g);
testLabel2 = new JLabel("Testing Here 2");
testLabel2.setPreferredSize(new Dimension(250, 200));
testLabel2.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray));
g.gridx = 1;
g.gridy = 0;
contentPane.add(testLabel2, g);
testLabel3 = new JLabel("Testing Here 3");
testLabel3.setPreferredSize(new Dimension(200, 200));
testLabel3.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray));
g.gridx = 1;
g.gridy = 1;
contentPane.add(testLabel3, g);
testLabel4 = new JLabel("Testing Here 4");
testLabel4.setPreferredSize(new Dimension(200, 200));
testLabel4.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray));
g.gridx = 2;
g.gridy = 2;
contentPane.add(testLabel4, g);
}
/**
* The getDashTab method returns a DashTab object.
* #return a DashTab panel object.
*/
public JPanel getDashTab(){
return dashPanel;
}
}
This is how the above code looks like:
Whenever I remove contentPane.setPreferredSize(new Dimension(857, 725)); the panel just stretches out and complete ignores the scrollbars making it look like:
contentPane.setPreferredSize(new Dimension(857, 725));
Don't use the setPreferredSize() method to set the size. That is the job of the layout manager, in this case, the GridBagLayout to determine the size of the panel.
Scrollbars will appear automatically when the preferred size of the panel is greater than the size of the scrollpane.
GUI components to be really small whenever they are too big to fit in the panel.
The GridBagLayout will try to respect the preferred size of the component. If the preferred size is greater than the size of the panel, then the components "minimum size" will be used. In the case of a JLabel, the minimum size is the space needed to entirely display the text.
Once again, don't try to use the setPreferedSize() method.

How to fix JPanel with Grid that occupies full width of frame?

I'm working on a layout for a simple login screen. I am using GridLayout in order to manipulate elements, but have came across an issue, It occupies full frame width like this:
Where as I want it to be with a fixed position and width,height and be vertically, horizontally centered inside a frame, but not occupy it's full size.
Possibly make Username and Password labels occupy less space so text fields are actually closer to them. I tried setting main size of my panel like .setMaximumSize(Dimension (200, 150));
But it doesn't seem to work.
//Create the frame.
JFrame frame = new JFrame("Online Shop");
JPanel mainPanel = new JPanel();
JPanel usernamePanel = new JPanel();
JPanel passwordPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JButton loginButton = new JButton("Login");
loginButton.setMaximumSize(new Dimension(100, 50));
JTextField username = new JTextField();
JLabel usernameLabel = new JLabel("Username");
JPasswordField password = new JPasswordField();
JLabel passwordLabel = new JLabel("Password");
//Panel
frame.setContentPane(mainPanel);
usernamePanel.setLayout(new GridLayout(1,2));
usernamePanel.add(usernameLabel);
usernamePanel.add(username);
passwordPanel.setLayout(new GridLayout(1,2));
passwordPanel.add(passwordLabel);
passwordPanel.add(password);
mainPanel.setLayout(new GridLayout(3, 1));
mainPanel.add(usernamePanel);
mainPanel.add(passwordPanel);
mainPanel.add(loginButton);
//Event Listeners
frame.addWindowListener(new MyWindowListener());
loginButton.addActionListener(new MyActionListener());
//Sizes, Positioning
frame.setSize(720, 480);
frame.setLocationRelativeTo(null);
//Show Frame
frame.setVisible(true);
frame.setSize(720, 480);
Don't use the setSize() method.
Instead use:
frame.pack();
Also use:
JTextField username = new JTextField(10);
To give the text fields a preferred number of characters.
Now when the frame is made visible the components will be displayed at their preferred size and the frame will be sized appropriately to fit the components.
If you want a little extra space between the components and the frame then you can do:
mainPanel.setBorder( new EmptyBorder(20, 20, 20, 20) );

Container panels and JScrollPane

I'm working on GUI and I'm an amateur programmer. I have a problem with this code. I can't see anything on the frame.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1366, 768);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
Container midPanel = new JPanel();
midPanel.setLayout(null);
Dimension preferredSize = new Dimension(700, 700);
midPanel.setPreferredSize(preferredSize);
.....
Container k1 = new JPanel();
k1.setSize(50, 700);
k1.setLocation(0, 0);
k1.setLayout(new GridLayout(rowNum, 1));
k1.setVisible(true);
midPanel.add(k1);
.......
Dimension jspD = new Dimension(500,500);
JScrollPane jsp = new JScrollPane(midPanel);
jsp.setPreferredSize(jspD);
jsp.setLocation(0, 0);
jsp.setVisible(true);
contentPane.add(jsp);
I would appreciate your help.
midPanel.setLayout(null);
You should always use Layout Managers, never ever remove the layout for any reason, except if you had an assignment required to use absolute layout (null layout).
The problem is with absolute layout , you have to specify the location of components inside the panel by component.setBounds(x,y,width,height) every time adding a component, otherwise, it won't be visible.
See this tutorial on Using Layout Managers.

Categories

Resources