How to program this GUI in Java Swing

I am currently developing a small utility with the following graphical interface:

Sample GUI

Now I have a container (JPanel) with a BorderLayout layout that contains everything in its place. I have 2 more JPanels hosted on BorderLayout.NORTH and BorderLayout.SOUTH respectively, each of which has a GridLayout (2 columns, 1 row). The table is located on the main container located in the CENTER.

Do you think this is the best approach? I have a hard time spending on the distance between the components and the borders of the frame.

I now have this code:

import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JSeparator; import javax.swing.JTable; import javax.swing.SwingUtilities; public class GUI extends JFrame { private JButton loadFileBtn = new JButton("Load File"); private JButton generateReportBtn = new JButton("Generate Report"); private JButton exitBtn = new JButton("Exit"); private JLabel fileNameLbl = new JLabel("File Name Here"); private JMenuBar menuBar = new JMenuBar(); private JMenu fileMI = new JMenu("File"); private JMenuItem openFileMenu = new JMenuItem("Open File"); private JSeparator separator = new JSeparator(); private JMenuItem exitMenu = new JMenuItem("Exit"); private JMenu reportMI = new JMenu("Report"); private JMenuItem generateReportMenu = new JMenuItem("Generate Report"); private JMenu helpMI = new JMenu("Help"); private JMenuItem aboutMenu = new JMenuItem("About"); private JTable table = new JTable(5, 2); private JPanel mainPanel = new JPanel(new BorderLayout(10, 10)); private JPanel panel1 = new JPanel(new BorderLayout()); private JPanel panel2 = new JPanel(new GridLayout(1, 2)); private JPanel panel3 = new JPanel(new GridLayout(1, 2)); public GUI() { super("Sample GUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(new Dimension(300, 300)); setResizable(false); setLayout(new BorderLayout(10, 10)); fileMI.add(openFileMenu); fileMI.add(separator); fileMI.add(exitMenu); reportMI.add(generateReportMenu); helpMI.add(aboutMenu); menuBar.add(fileMI); menuBar.add(reportMI); menuBar.add(helpMI); setJMenuBar(menuBar); panel1.add(table, BorderLayout.CENTER); panel2.add(fileNameLbl); panel2.add(loadFileBtn); panel3.add(generateReportBtn); panel3.add(exitBtn); mainPanel.add(panel2, BorderLayout.NORTH); mainPanel.add(panel1, BorderLayout.CENTER); mainPanel.add(panel3, BorderLayout.SOUTH); add(mainPanel); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { GUI app = new GUI(); app.setVisible(true); } }); } } 

What do you think is the best way to do this?

Any help would be greatly appreciated. Thanks.


UPDATE:

I now have the following graphical interface

enter image description here

I want the components to be evenly spaced from the borders, for example in a layout.

+4
source share
4 answers

2 things you can use to make this happen:

  • Use BorderFactory.createEmptyBorder(int, int, int, int)
  • Use the 4-args GridLayout constructor

There is another LayoutManager that can use the same functions (for example, GridBagLayout or use the nested BorderLayout ), but if you are comfortable working with the current LayoutManager , there is no need to require a change to those. How you did it is also acceptable.

enter image description here

You can consider wrapping the table in JScrollPane to make it more enjoyable, with headers and scrollbars, if you have ever needed it.

A small code example:

 import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JSeparator; import javax.swing.JTable; import javax.swing.SwingUtilities; public class GUI extends JFrame { private JButton loadFileBtn = new JButton("Load File"); private JButton generateReportBtn = new JButton("Generate Report"); private JButton exitBtn = new JButton("Exit"); private JLabel fileNameLbl = new JLabel("File Name Here"); private JMenuBar menuBar = new JMenuBar(); private JMenu fileMI = new JMenu("File"); private JMenuItem openFileMenu = new JMenuItem("Open File"); private JSeparator separator = new JSeparator(); private JMenuItem exitMenu = new JMenuItem("Exit"); private JMenu reportMI = new JMenu("Report"); private JMenuItem generateReportMenu = new JMenuItem("Generate Report"); private JMenu helpMI = new JMenu("Help"); private JMenuItem aboutMenu = new JMenuItem("About"); private JTable table = new JTable(5, 2); private JPanel mainPanel = new JPanel(new BorderLayout(10, 10)); private JPanel panel1 = new JPanel(new BorderLayout()); private JPanel panel2 = new JPanel(new GridLayout(1, 2, 10, 10)); private JPanel panel3 = new JPanel(new GridLayout(1, 2, 10, 10)); public GUI() { super("Sample GUI"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(new Dimension(300, 300)); setResizable(false); setLayout(new BorderLayout(10, 10)); fileMI.add(openFileMenu); fileMI.add(separator); fileMI.add(exitMenu); reportMI.add(generateReportMenu); helpMI.add(aboutMenu); menuBar.add(fileMI); menuBar.add(reportMI); menuBar.add(helpMI); setJMenuBar(menuBar); panel1.add(table, BorderLayout.CENTER); panel2.add(fileNameLbl); panel2.add(loadFileBtn); panel3.add(generateReportBtn); panel3.add(exitBtn); mainPanel.add(panel2, BorderLayout.NORTH); mainPanel.add(panel1, BorderLayout.CENTER); mainPanel.add(panel3, BorderLayout.SOUTH); mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); add(mainPanel); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { GUI app = new GUI(); app.setVisible(true); } }); } } 
+4
source

In the past, I found that using MigLayout solves all my problems

+4
source

Here is how I would organize your GUI. This is one way. This is not the only way.

  • JTable goes inside JScrollPane .

  • Button2 and Button3 go to the JPanel button using FlowLayout .

  • Label and Button1 go inside the JPanel using FlowLayout .

  • JMenuItem file, report and help on JMenuBar .

  • The main JPanel has a BoxLayout with Y_AXIS orientation.

  • Add the JPanel label, JScrollPane and the JPanel button to the main JPanel .

+3
source

The best way to create a GUI is with code that you and others will understand. Take a look at the Layout Managers Guide . You can use different layout managers for different components. This will help you set the correct interval.

If your problem is just the distance between the components, install Insets .

+2
source

All Articles