Home > Mobile >  Java Swing Glue not working between 2 buttons
Java Swing Glue not working between 2 buttons

Time:03-28

I am trying to make my own little game in Java as a personal excercise however I am finding a lot of issues using BoxLayout's in Java Swing.

So I have a basic MVC application and I need two buttons at the top both "New Game" and "Submit" to both be on the same line in the GUI. I have found out that I can use glue to do this however all of the guides I have found on it, do not work. Am I missing something obvious here?

This is my view code (my GUI):

package mvc;

import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.Set;
import javax.swing.*;

public class View extends JFrame {
    //User input Characters
    private JTextField firstChar = new JTextField(1);
    private JTextField secondChar = new JTextField(1);
    private JTextField thirdChar = new JTextField(1);
    private JTextField fourthChar = new JTextField(1);
    private JTextField fifthChar = new JTextField(1);
    
    //Displays on GUI
    private JButton submitButton = new JButton("Submit");
    private JButton newButton = new JButton("New Game");

    View() {
        JPanel gamePanel = new JPanel();
        gamePanel.setLayout(new BoxLayout(gamePanel, BoxLayout.Y_AXIS));
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 200);
        
        gamePanel.add(submitButton);
        gamePanel.add(Box.createHorizontalGlue());
        gamePanel.add(newButton);
        
        gamePanel.add(firstChar);  
        
        gamePanel.add(secondChar);
        gamePanel.add(thirdChar);
        gamePanel.add(fourthChar);
        gamePanel.add(fifthChar);
        
        this.add(gamePanel);
    }
}

Any help would be massively appreciated!

I have tried using glue and rigid area's to solve this however neither worked. I am expecting both buttons to be on the same line in my GUI

CodePudding user response:

You use Boxlayout, that according to https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/BoxLayout.html A layout manager that allows multiple components to be laid out either vertically or horizontally.

In your case it is vertical, and I never used Glue so I am not even aware whether it can change that behaviour.

But you can for sure put the two buttons in an extra panel and add that to your gamePanel, like so:

    JPanel buttonBar = new JPanel();
    buttonBar.setLayout(new FlowLayout());
    buttonBar.add(submitButton);
    buttonBar.add(newButton);
    gamePanel.add(buttonBar);

CodePudding user response:

BoxLayout can either lay out the components along the X_AXIS or along the Y_AXIS. You cannot mix this two layout directions with a single BoxLayout.

One way to solve your problem is to wrap both buttons in a JPanel and use a BoxLayout(.., BoxLayout.X_AXIS) to lay out this button panel.

You would then add the button panel as the first element of the gamePanel:

package mvc;

import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.Set;
import javax.swing.*;

public class View extends JFrame {
    //User input Characters
    private JTextField firstChar = new JTextField(1);
    private JTextField secondChar = new JTextField(1);
    private JTextField thirdChar = new JTextField(1);
    private JTextField fourthChar = new JTextField(1);
    private JTextField fifthChar = new JTextField(1);
    
    //Displays on GUI
    private JButton submitButton = new JButton("Submit");
    private JButton newButton = new JButton("New Game");

    View() {
        JPanel gamePanel = new JPanel();
        gamePanel.setLayout(new BoxLayout(gamePanel, BoxLayout.Y_AXIS));
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 200);
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
        buttonPanel.add(submitButton);
        buttonPanel.add(Box.createHorizontalGlue());
        buttonPanel.add(newButton);
        gamePanel.add(buttonPanel);

        gamePanel.add(firstChar);  
        
        gamePanel.add(secondChar);
        gamePanel.add(thirdChar);
        gamePanel.add(fourthChar);
        gamePanel.add(fifthChar);
        
        this.add(gamePanel);
    }
}
  • Related