Two Split pane place on a tabpane - java

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JList;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
public class MenuPage extends JFrame{
final static int extraWindowWidth = 100;
JSplitPane jSplitPane, jSplitPane2,jSplitPane3,jSplitPane4;
JPanel jPanel2a, jPanel2b, jPanel3;
public static JFrame frame;
public MenuPage(){
}
private final JList<String> list1=new JList<>(new String[]{"A","B"});
private final JList<String> list2 = new JList<>();
private final List<DefaultListModel> models = new ArrayList<>();
public void addComponentToPane(Container pane) {
JTabbedPane tabbedPane = new JTabbedPane();
// Create the "cards".
JPanel card1 = new JPanel() {
// Make the panel wider than it really needs, so
// the window's wide enough for the tabs to stay
// in one row.
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width += extraWindowWidth;
return size;
}
};
DefaultListModel<String> model1 = new DefaultListModel<>();
model1.addElement("A1");
model1.addElement("A2");
model1.addElement("A3");
models.add(model1);
DefaultListModel<String> model2 = new DefaultListModel<>();
model2.addElement("B1");
model2.addElement("B2");
models.add(model2);
list2.setModel(model1);
list1.addListSelectionListener((ListSelectionEvent e) -> {
if (!e.getValueIsAdjusting()) {
list2.setModel(models.get(list1.getSelectedIndex()));
}
});
jPanel2a = new JPanel();
jPanel2b = new JPanel();
jPanel3 = new JPanel();
jSplitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel2a, jPanel2b);
jSplitPane3 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list1, list2);
jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jSplitPane3,jPanel3);
// panelin icerisine component eklendi
JLabel lblHelloWorld = new JLabel("Hello World!");
jPanel2a.add(lblHelloWorld);
jSplitPane2.setOneTouchExpandable(true);
jSplitPane2.setDividerLocation(100);
jSplitPane3.setOneTouchExpandable(true);
jSplitPane3.setDividerLocation(150);
jSplitPane4.setOneTouchExpandable(true);
jSplitPane4.setDividerLocation(300);
JPanel card3 = new JPanel();
JPanel card4 = new JPanel();
JPanel card5 = new JPanel();
JPanel card6 = new JPanel();
JPanel card7 = new JPanel();
JPanel card8 = new JPanel();
card3.add(jSplitPane3,jSplitPane4);
tabbedPane.addTab("Main", jSplitPane2);
tabbedPane.addTab("Leagues",jSplitPane4);
tabbedPane.addTab("Teams",card3 );
tabbedPane.addTab("Ratios", card4);
tabbedPane.addTab("Competitions", card5);
tabbedPane.addTab("Analyze", card6);
tabbedPane.addTab("Help", card7);
tabbedPane.addTab("About", card8);
pane.add(tabbedPane, BorderLayout.CENTER);
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event dispatch thread.
*/
public static void createAndShowGUI() {
// Create and set up the window.
frame = new JFrame("BetAnalyzer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(594, 310);
// Create and set up the content pane.
MenuPage demo = new MenuPage();
demo.addComponentToPane(frame.getContentPane());
// Display the window.
frame.pack();
frame.setVisible(true);
frame.setBounds(100, 100, 635, 251);
}
}
I want the list1, list2, and jpanel3 which takes part 2 JSplitPane to be in the league tab There is something wrong in my code. Just jSplitPane3 or jSplitPane4 could be appear in the layout.
Also, I tried to add two JSplitPane into Box then I put the Box in tabbedpane but this's not working neither.

Your code has several issues.
do not inherit without reason
Your class inherits from JFrame but its does not extend its behavior (it does not override a public or protected method of JFrame).
Components can only be added once
In contrast to the other points this is not an issue per se but part of your problem.
jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jSplitPane3,jPanel3);
// ...
card3.add(jSplitPane3,jSplitPane4);
When the second statement is executed the jSplitPane3 is removed from the jSplitPane4 and only shown as content of card3.
use of standard layouts
The (dummy) content of your panels is rather small so that their preferedSize is less then the available space in the parent container.
The JPanels default Layout is FlowLayout which reduces each of its components to its preferedSize.
You should set the Layout of your JPanels to BorderLayout which allows its content to stretch out over the complete space.
In turn you cannot use JPanels varag method .add(Component, component)
anymore.
here are the relevant changes:
jPanel2a = new JPanel(new BorderLayout());
jPanel2b = new JPanel(new BorderLayout());
jPanel3 = new JPanel(new BorderLayout());
jSplitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel2a, jPanel2b);
jSplitPane3 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, list1, list2);
jSplitPane4 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jSplitPane3, jPanel3);
// panelin icerisine component eklendi
JLabel lblHelloWorld = new JLabel("Hello World!");
jPanel2a.add(lblHelloWorld);
// jSplitPane2.setOneTouchExpandable(true);
jSplitPane2.setDividerLocation(100);
//
// jSplitPane3.setOneTouchExpandable(true);
jSplitPane3.setDividerLocation(150);
//
// jSplitPane4.setOneTouchExpandable(true);
jSplitPane4.setDividerLocation(300);
JPanel card3 = new JPanel(new BorderLayout());
JPanel card4 = new JPanel(new BorderLayout());
JPanel card5 = new JPanel(new BorderLayout());
JPanel card6 = new JPanel(new BorderLayout());
JPanel card7 = new JPanel(new BorderLayout());
JPanel card8 = new JPanel(new BorderLayout());
// card3.add(jSplitPane3);
card3.add(jSplitPane4,BorderLayout.SOUTH);
tabbedPane.addTab("Main", jSplitPane2);

Related

Adding a JLabel to a JFrame that is using BoxLayout

I am creating a JFrame that contains a JPanel with a grid of buttons. Everything works fine but I then want to add a JLabel above the panel of buttons but the label never appears. It does appear if I don't use BoxLayout, though. Any help is appreciated.
The first code part below is my JFrame class:
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Frame extends JFrame {
private static final long serialVersionUID = 1L;
Panel panel = new Panel();
Dimension frameDim = new Dimension(1000, 1000);
Dimension labelDim = new Dimension(100, 20);
Box box = new Box(BoxLayout.Y_AXIS);
JLabel label = new JLabel("Tic Tac Toe");
JPanel pane = new JPanel();
public Frame() {
pane.add(label);
pane.setPreferredSize(labelDim);
pane.setMinimumSize(labelDim);
add(pane);
box.add(Box.createVerticalGlue());
box.add(panel);
box.add(Box.createVerticalGlue());
add(box);
setTitle("Tic Tac Toe");
setSize(frameDim);
setMinimumSize(frameDim);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
The code below is my JPanel class with buttons:
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Panel extends JPanel {
private static final long serialVersionUID = 2L;
private int i;
JButton[] button = new JButton[9];
GridLayout layout = new GridLayout(3, 3);
Dimension dim = new Dimension(500, 500);
public Panel() {
for (i = 0; i<9; i++) {
button[i] = new JButton();
add(button[i]);
}
setPreferredSize(dim);
setMinimumSize(dim);
setMaximumSize(dim);
setLayout(layout);
}
}
The last code part below is the Main class:
public class RunGame {
public static void main(String[] args) {
new Frame();
}
}
add(box);
This is adding the label direct to the JFrame, the content pane of which is laid out using a BorderLayout. The default when adding to a component to a border layout without any constraint is the CENTER, which can only display a single component. To fix it use:
pane.add(box);
you can use the BorderLayout when adding your label into Pane into the Frame and remove that box thing as below
public Frame()
{
//create label and add it to the frame
JLabel label = new JLabel("Tic Tac Toe");
label.setHorizontalAlignment( JLabel.CENTER );
add(label, BorderLayout.NORTH);
//create buttonsPanel and add it to the frame
JPanel buttons = new JPanel();
buttons.setLayout( new GridLayout(3, 3));
for (int i = 0; i < 9; i++)
{
buttons.add(new JButton(""+i));
}
add(buttons, BorderLayout.CENTER);
//setup the title, other properties for the frame etc..
setTitle("Tic Tac Toe");
setSize(1000, 1000);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
hope this helps better :)

Aligning buttons to center in BoxLayout

I am trying to set the buttons to the center using the Box.createHorizontalStrut() method. However if I use this.getWidth()/2 is does not work. How can I do to center it in the frame.
Code
package ch17;
import java.awt.Color;
import java.awt.Container;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class Q17_1 extends JFrame{
JButton left = new JButton("<=");
JButton right = new JButton("=>");
JPanel p1 = new JPanel();
JRadioButton rb1 = new JRadioButton("Red");
JRadioButton rb2 = new JRadioButton("Yellow");
JRadioButton rb3 = new JRadioButton("White");
JRadioButton rb4 = new JRadioButton("Gray");
JRadioButton rb5 = new JRadioButton("Green");
JPanel p2 = new JPanel();
Message m = new Message("Welcome to Java");
public Q17_1(){
setLayout(new GridLayout(3,1));
p1.setBorder(new TitledBorder("Select Message Panel Background"));
ButtonGroup group = new ButtonGroup();
group.add(rb1);group.add(rb2);group.add(rb3);group.add(rb4);group.add(rb5);
rb1.setMnemonic('R');rb2.setMnemonic('Y');rb3.setMnemonic('W');rb4.setMnemonic('G');
rb5.setMnemonic('N');
p1.setLayout(new GridLayout(1,5,5,5));
p1.add(rb1);p1.add(rb2);p1.add(rb3);p1.add(rb4);p1.add(rb5);
p2.setLayout(new BoxLayout(p2,BoxLayout.X_AXIS));
add(p1);
add(m);
p2.add(Box.createHorizontalStrut(250));
p2.add(left);
p2.add(Box.createHorizontalStrut(5));
p2.add(right);
add(p2);
left.addActionListener((ActionEvent) -> {
m.moveLeft();
repaint();
});
right.addActionListener((ActionEvent)-> {
m.moveRight();
repaint();
});
rb1.addActionListener(m);
rb2.addActionListener(m);
rb3.addActionListener(m);
rb4.addActionListener(m);
rb5.addActionListener(m);
}
public static void main(String[] args) {
Q17_1 frame = new Q17_1();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
I have tried, this.getWidth()/2, p2.getWidth()/2, etc. However they don't work and the buttons are still starting from the beginning of the left side.
You could use a combination of Borderlayout, FlowLayout or GridBagLayout
For summary:
setLayout(new BorderLayout());
//...
p2.setLayout(new FlowLayout());
add(p1, BorderLayout.NORTH);
add(m);
//...
add(p2, BorderLayout.SOUTH);
The reason I might consider using BorderLayout of GridLayout for the core layout is it will give all the remaining space to the component in the CENTER position. This might not be what you want, but it's why I've used it.
Both GridBagLayout and FlowLayout will layout it's containers around the centre of the container, GridBagLayout doing it vertically and horizontally, FlowLayout doing it only horizontally (by default)
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;
public class Q17_1 extends JFrame {
JButton left = new JButton("<=");
JButton right = new JButton("=>");
JPanel p1 = new JPanel();
JRadioButton rb1 = new JRadioButton("Red");
JRadioButton rb2 = new JRadioButton("Yellow");
JRadioButton rb3 = new JRadioButton("White");
JRadioButton rb4 = new JRadioButton("Gray");
JRadioButton rb5 = new JRadioButton("Green");
JPanel p2 = new JPanel();
JLabel m = new JLabel("Welcome to Java");
// Message m = new Message("Welcome to Java");
public Q17_1() {
setLayout(new BorderLayout());
p1.setBorder(new TitledBorder("Select Message Panel Background"));
ButtonGroup group = new ButtonGroup();
group.add(rb1);
group.add(rb2);
group.add(rb3);
group.add(rb4);
group.add(rb5);
rb1.setMnemonic('R');
rb2.setMnemonic('Y');
rb3.setMnemonic('W');
rb4.setMnemonic('G');
rb5.setMnemonic('N');
p1.setLayout(new GridLayout(1, 5, 5, 5));
p1.add(rb1);
p1.add(rb2);
p1.add(rb3);
p1.add(rb4);
p1.add(rb5);
p2.setLayout(new FlowLayout());
add(p1, BorderLayout.NORTH);
add(m);
p2.add(left);
p2.add(right);
add(p2, BorderLayout.SOUTH);
left.addActionListener((ActionEvent) -> {
// m.moveLeft();
// repaint();
});
right.addActionListener((ActionEvent) -> {
// m.moveRight();
// repaint();
});
// rb1.addActionListener(m);
// rb2.addActionListener(m);
// rb3.addActionListener(m);
// rb4.addActionListener(m);
// rb5.addActionListener(m);
}
public static void main(String[] args) {
Q17_1 frame = new Q17_1();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
But If I want to use BoxLayout for practice for p2. What should I put for the arguments in order to center the buttons???
Because the container's size dynamic, you could use some horizontal glue instead
p2.add(Box.createHorizontalGlue());
p2.add(left);
p2.add(right);
p2.add(Box.createHorizontalGlue());

Position JLabel within JPanel

I want to position a JLabel within a JPanel, so that it appears at the top of the window. I then want to position two drop-down menu below that so that the user can choose from two sets of options.
How would I go about positioning these elements?
Here's a JLabel title with two JComboBoxes. I have no idea what else you mean by a "drop-down menu".
I created a JPanel with a BorderLayout to hold the GUI.
The title is a JLabel inside of a JPanel using the default FlowLayout.
The JComboBoxes are inside of a JPanel using the default FlowLayout.
Here's the code:
package com.ggl.testing;
import java.awt.BorderLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DropDownLayout implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new DropDownLayout());
}
#Override
public void run() {
JFrame frame = new JFrame("Drop Down Layout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(createTitlePanel(), BorderLayout.NORTH);
panel.add(createDropDownPanel(), BorderLayout.CENTER);
frame.add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createTitlePanel() {
JPanel panel = new JPanel();
JLabel titleLabel = new JLabel("Title");
panel.add(titleLabel);
return panel;
}
private JPanel createDropDownPanel() {
JPanel panel = new JPanel();
DefaultComboBoxModel<String> model1 = new DefaultComboBoxModel<String>();
model1.addElement("Selection 1");
model1.addElement("Selection 2");
model1.addElement("Selection 3");
model1.addElement("Selection 4");
JComboBox<String> comboBox1 = new JComboBox<String>(model1);
panel.add(comboBox1);
DefaultComboBoxModel<String> model2 = new DefaultComboBoxModel<String>();
model2.addElement("Choice 1");
model2.addElement("Choice 2");
model2.addElement("Choice 3");
model2.addElement("Choice 4");
JComboBox<String> comboBox2 = new JComboBox<String>(model2);
panel.add(comboBox2);
return panel;
}
}

Creating JSplitPane with 3 panels

I would like to create a window, with 3 jPanels, separated by splitPane-s. The left, and the right one should be resizable by the user, and the one in the middle should fill the remaining space.
I've created it, but if I move the first splitPane, then the second one is also moving. And I'm not sure, if I use the best method for what I want.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class MyWindow extends JFrame {
public MyWindow() {
this.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel centerPanel2 = new JPanel();
JPanel rightPanel = new JPanel();
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, centerPanel);
JSplitPane sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, centerPanel2, rightPanel);
centerPanel.setLayout(new BorderLayout());
this.add(sp, BorderLayout.CENTER);
centerPanel.add(sp2, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
}
}
What you're doing looks pretty weird to me i.e adding the centerPanel to the split pane, then adding the split pane to the centerPane. Not sure, but I think that the latter negates the former.
All you need to do is add the first split pane to the second split pane.
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
public class MyWindow extends JFrame {
public MyWindow() {
this.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.BLUE);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.CYAN);
JPanel rightPanel = new JPanel();
rightPanel.setBackground(Color.GREEN);
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, centerPanel);
JSplitPane sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sp, rightPanel);
this.add(sp2, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new MyWindow();
}
});
}
}

How to program this GUI in Java Swing

I'm currently developing a small utility with the following GUI:
Right now i have a container (JPanel) with the BorderLayout layout that holds everything in place. The i have another 2 JPanels placed on BorderLayout.NORTH and BorderLayout.SOUTH respectively, each whith the GridLayout (2 columns by 1 row). The table is on the main container placed at the CENTER of it.
Do you think this is the best approach? I'm having a rough time dealing with spacing between the components and the borders of the frame.
Right now i 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 would be the best approach to do this?
Any help would be appreciated. Thanks.
UPDATE:
Right now, i have the following GUI
I want the components to space away from the borders evenly, like in the mockup.
2 things you can use to make this happen:
Use BorderFactory.createEmptyBorder(int, int, int, int)
Use the 4-args constructor of GridLayout
There are other LayoutManager's which can bring the same functionality (like GridBagLayout, or using nested BorderLayout), but if you feel comfortable with the current LayoutManager's, there is no imperious need to change to those. The way you did is also acceptable.
You might consider wrapping the table in a JScrollPane to make it nicer, with headers and scrollbars if ever needed.
Small example code:
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);
}
});
}
}
In the past, I have found that using MigLayout, solves all my problems
Here's how I would arrange your GUI. This is just one way. It's not the only way.
The JTable goes inside of a JScrollPane.
Button2 and Button3 go inside of a button JPanel with a FlowLayout.
Label and Button1 go inside of a label JPanel with a FlowLayout.
File, Report, and Help are JMenuItems on a JMenuBar.
The main JPanel has a BoxLayout with a Y_AXIS orientation.
Add the label JPanel, the JScrollPane, and the button JPanel to the main JPanel.
The best way is to create a GUI is to code in a way you and others will understand . Take a look at this Guide to Layout Managers. You can use different layout managers for different components. This will help you setting correct spacing.
If your problem is just the spacing around the components, set its Insets.

Categories