2

I'm trying to increase the distance of my JButtons from the top of my Panel to make it more visually appealing, i've tried using an invisible button but have had no luck.

public class SimpleBorder {

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       

    Border etched = (Border) BorderFactory.createEtchedBorder();


    String[] items = {"A", "B", "C", "D"};
    JList list = new JList(items);

    JTextArea text = new JTextArea(10, 40);

    JScrollPane scrol = new JScrollPane(text);
    JScrollPane scrol2 = new JScrollPane(list);

    JPanel panel= new JPanel();
    panel.add(scrol2,BorderLayout.WEST);
    panel.add(scrol, BorderLayout.EAST);    
    panel.setBorder(etched);

    frame.add(panel);
    frame.setVisible(true);
}

}

Any ideas ?

bgigurtsis
  • 145
  • 1
  • 2
  • 15
  • 1
    Why can't you change top of the Insets you define? – StanislavL Sep 24 '13 at 09:13
  • @StanislavL Just tried, changed the first value to 25. That increases the space between the buttons, but I want to keep that space the same and at the same time shift them down a bit. – bgigurtsis Sep 24 '13 at 09:27
  • 2
    Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe. In your [sscce](http://sscce.org/), access posted images via `URL`, as shown [here](http://stackoverflow.com/a/10862262/230513); use synthetic images as shown [here](http://stackoverflow.com/a/15982915/230513); or use `UIManager` icons, as shown [here](http://stackoverflow.com/a/12228640/230513). – trashgod Sep 24 '13 at 10:14
  • 2
    Rather, add an `EmptyBorder` to God knows which container, you are using (don't know if it's `JLabel` or `JPanel`) :-) just do this thingy. `panel.setBorder(BorderFactory.createEmptyBorder(25, 10, 5, 10))`, that I guess will do, or else, try to change these values to suite your taste :-) – nIcE cOw Sep 24 '13 at 12:00
  • @nIcEcOw Thank you for trying but it doesn't seem to do anything to the program, the components stay in the place that they were, even if I up all of the values. Any ideas ? – bgigurtsis Sep 25 '13 at 12:51
  • 1
    @Icy100 : Please give me half an hour, I will present an example for you to look at, regarding the same. Let me finish my milk :-) – nIcE cOw Sep 25 '13 at 12:55
  • 1
    @nIcEcOw Thank you very much! Enjoy your milk :) – bgigurtsis Sep 25 '13 at 12:59

1 Answers1

2

The key basically lies, in the fact, that the component doesn't knows it's actual size, till frame.pack() won't be called. Hence after this I am performing this calculation, to determine how much empty space to put for Border and again calling frame.pack() to repack() everything after putting the Border.

Please do have a look at this example, and see if this suite your needs :

import java.awt.*;
import javax.swing.*;

public class MainMenu {

    private JButton playButton;
    private JButton instructionButton;
    private JButton scoreboardButton;
    private JButton exitButton;

    private JPanel menuPanel;

    private GridBagConstraints gbc;

    public MainMenu() {
        gbc = new GridBagConstraints();
        gbc.insets = new Insets(15, 15, 15, 15);
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    }

    private void displayGUI() {
        JFrame frame = new JFrame("Main Menu");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(new BorderLayout());

        menuPanel = new JPanel(new GridBagLayout());
        menuPanel.setOpaque(true);
        menuPanel.setBackground(Color.BLACK);       

        playButton = new JButton("Play");
        instructionButton = new JButton("Instructions");
        scoreboardButton = new JButton("Scoreboard");
        exitButton = new JButton("Exit");

        addComp(menuPanel, playButton, 0, 0, 1, 1, 1.0, 0.20,
                                    GridBagConstraints.HORIZONTAL);
        addComp(menuPanel, instructionButton, 0, 1, 1, 1, 1.0, 0.20,
                                    GridBagConstraints.HORIZONTAL);
        addComp(menuPanel, scoreboardButton, 0, 2, 1, 1, 1.0, 0.20,
                                    GridBagConstraints.HORIZONTAL);
        addComp(menuPanel, exitButton, 0, 3, 1, 1, 1.0, 0.20,
                                    GridBagConstraints.HORIZONTAL);

        contentPane.add(menuPanel);

        frame.setContentPane(contentPane);
        frame.pack();
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(
                    contentPane.getHeight() - (contentPane.getHeight() / 4),
                    20, 5, 20));
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void addComp(JPanel panel, JComponent comp,
                                int gridx, int gridy,
                                int gridwidth, int gridheight,
                                double weightx, double weighty,
                                int fill) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = gridwidth;
        gbc.gridheight = gridheight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.fill = fill;

        panel.add(comp, gbc);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new MainMenu().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

Here is the output :

enter image description here

EDIT 1 :

Moreover, if you will use GridLayout instead of using GridBagLayout for the MainMenu, then I guess the results will be more promising. Please have a look at this example for that change :

import java.awt.*;
import javax.swing.*;

public class MainMenu {

    private JButton playButton;
    private JButton instructionButton;
    private JButton scoreboardButton;
    private JButton exitButton;

    private JPanel menuPanel;

    private void displayGUI() {
        JFrame frame = new JFrame("Main Menu");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(new GridBagLayout());

        menuPanel = new JPanel(new GridLayout(0, 1, 5, 5));
        menuPanel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        menuPanel.setOpaque(true);
        menuPanel.setBackground(Color.BLACK);

        playButton = new JButton("Play");
        instructionButton = new JButton("Instructions");
        scoreboardButton = new JButton("Scoreboard");
        exitButton = new JButton("Exit");

        menuPanel.add(playButton);
        menuPanel.add(instructionButton);
        menuPanel.add(scoreboardButton);
        menuPanel.add(exitButton);

        contentPane.add(menuPanel);

        frame.setContentPane(contentPane);
        frame.pack();
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(
                    contentPane.getHeight() - 
                        (contentPane.getHeight() - 
                            (3 * menuPanel.getHeight())),
                                                20, 0, 20));
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new MainMenu().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

EDIT 2 :

Another variant is looking much better, though, this time, the base JPanel is using GridLayout and the MenuPanel is using GridBagLayout. Please have a look at this example :

import java.awt.*;
import javax.swing.*;

public class MainMenu {

    private JButton playButton;
    private JButton instructionButton;
    private JButton scoreboardButton;
    private JButton exitButton;

    private JPanel menuPanel;

    private GridBagConstraints gbc;

    public MainMenu() {
        gbc = new GridBagConstraints();
        gbc.insets = new Insets(15, 15, 15, 15);
        gbc.anchor = GridBagConstraints.CENTER;
    }

    private void displayGUI() {
        JFrame frame = new JFrame("Main Menu");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(new GridLayout(1, 1, 5, 2));

        menuPanel = new JPanel(new GridBagLayout());
        menuPanel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        menuPanel.setOpaque(true);
        menuPanel.setBackground(Color.BLACK);

        playButton = new JButton("Play");
        instructionButton = new JButton("Instructions");
        scoreboardButton = new JButton("Scoreboard");
        exitButton = new JButton("Exit");

        addComp(menuPanel, playButton, 0, 0, 1, 1, 1.0, 0.10,
                                    GridBagConstraints.CENTER);
        addComp(menuPanel, instructionButton, 0, 1, 1, 1, 1.0, 0.10,
                                    GridBagConstraints.CENTER);
        addComp(menuPanel, scoreboardButton, 0, 2, 1, 1, 1.0, 0.10,
                                    GridBagConstraints.CENTER);
        addComp(menuPanel, exitButton, 0, 3, 1, 1, 1.0, 0.10,
                                    GridBagConstraints.CENTER);

        contentPane.add(menuPanel);

        frame.setContentPane(contentPane);
        frame.pack();
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(
                    contentPane.getHeight() - 
                        (contentPane.getHeight() - 
                            (2 * menuPanel.getHeight()) + 100),
                                20, 2, 20));
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void addComp(JPanel panel, JComponent comp,
                                int gridx, int gridy,
                                int gridwidth, int gridheight,
                                double weightx, double weighty,
                                int fill) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = gridwidth;
        gbc.gridheight = gridheight;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbc.fill = fill;

        panel.add(comp, gbc);
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new MainMenu().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143