How can I replace a JPanel or JFrame & its contents with another one by a simple button click in the same container ?
here a simple example that you can follow please show code at lest so we can follow your problem this a snap of code from unknowing source (old one)
package layout;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class CardLayoutExample extends JFrame{
JPanel totelPanel,btnPan,showPan;
JButton btn1,btn2;
public static void main(String[] args) {
CardLayoutExample ex = new CardLayoutExample();
ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ex.pack();
ex.setLocationRelativeTo(null);
ex.setTitle("BookClube library system");
ex.setVisible(true);
}
public CardLayoutExample(){
btn1 = new JButton("menu button");
btn2 = new JButton("back button");
CardLayout c1 = new CardLayout();
btnPan = new JPanel();
btnPan.add(btn1);
showPan = new JPanel();
showPan.add(btn2);
totelPanel = new JPanel(c1);
totelPanel.add(btn1,"1");
totelPanel.add(btn2,"2");
c1.show(totelPanel,"1");
JPanel fullLayout = new JPanel(new BorderLayout());
fullLayout.add(totelPanel,BorderLayout.NORTH);
add(fullLayout);
btn1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
c1.show(totelPanel,"2");
}
});
btn2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
c1.show(totelPanel,"1");
}
});
}
}
The trick would be to use CardLayout or modify panel visibility.
Please look at the following example that modifies panel visibility.
public class PanelExample {
private JPanel _myPanel1,_myPanel2;
public void init() {
JFrame frame = new JFrame("Testing");
JPanel mainPanel = new JPanel(new GridBagLayout());
_myPanel1 = new JPanel();
_myPanel1.add(new JLabel("Panel 1"));
_myPanel1.setVisible(true);
_myPanel2 = new JPanel();
_myPanel2.add(new JLabel("Panel 2"));
_myPanel2.setVisible(false);
JButton button = new JButton("Switch to Panel2");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(_myPanel1.isVisible()) {
_myPanel1.setVisible(false);
_myPanel2.setVisible(true);
button.setText("Switch to Panel1");
} else {
_myPanel1.setVisible(true);
_myPanel2.setVisible(false);
button.setText("Switch to Panel2");
}
}
});
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.weighty = 0;
mainPanel.add(button,gbc);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 2;
gbc.gridheight = 2;
gbc.weightx = 1;
gbc.weighty = 1;
mainPanel.add(_myPanel1,gbc);
mainPanel.add(_myPanel2,gbc);
frame.add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
PanelExample exmp = new PanelExample();
exmp.init();
}
}
Related
I have a JTextArea that is filled with numbers with no duplicates. There is an add and remove button. I have programmed the add button, but I am struggling with programming the remove button. I know how to remove the number from the array, but I'm not sure how to remove the number from the text area.
How do I remove a line from a text area that contains a certain number?
Extra notes:
The only input is integers.
Your question may in fact be an XY Problem where you ask how to fix a specific code problem when the best solution is to use a different approach entirely. Consider using a JList and not a JTextArea. You can easily rig it up to look just like a JTextArea, but with a JList, you can much more easily remove an item such as a line by removing it from its model.
For example:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
#SuppressWarnings("serial")
public class NumberListEg extends JPanel {
private static final int VIS_ROW_COUNT = 10;
private static final int MAX_VALUE = 10000;
private DefaultListModel<Integer> listModel = new DefaultListModel<>();
private JList<Integer> numberList = new JList<>(listModel);
private JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, MAX_VALUE, 1));
private JButton addNumberButton = new JButton(new AddNumberAction());
public NumberListEg() {
JPanel spinnerPanel = new JPanel();
spinnerPanel.add(spinner);
JPanel addNumberPanel = new JPanel();
addNumberPanel.add(addNumberButton);
JPanel removeNumberPanel = new JPanel();
JButton removeNumberButton = new JButton(new RemoveNumberAction());
removeNumberPanel.add(removeNumberButton);
JPanel eastPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
// gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(3, 3, 3, 3);
eastPanel.add(spinner, gbc);
gbc.gridy = GridBagConstraints.RELATIVE;
eastPanel.add(addNumberButton, gbc);
eastPanel.add(removeNumberButton, gbc);
// eastPanel.add(Box.createVerticalGlue(), gbc);
numberList.setVisibleRowCount(VIS_ROW_COUNT);
numberList.setPrototypeCellValue(1234567);
numberList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane listPane = new JScrollPane(numberList);
listPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setLayout(new BorderLayout());
add(listPane, BorderLayout.CENTER);
add(eastPanel, BorderLayout.LINE_END);
}
private class AddNumberAction extends AbstractAction {
public AddNumberAction() {
super("Add Number");
putValue(MNEMONIC_KEY, KeyEvent.VK_A);
}
#Override
public void actionPerformed(ActionEvent arg0) {
int value = (int) spinner.getValue();
if (!listModel.contains(value)) {
listModel.addElement(value);
}
}
}
private class RemoveNumberAction extends AbstractAction {
public RemoveNumberAction() {
super("Remove Number");
putValue(MNEMONIC_KEY, KeyEvent.VK_R);
}
#Override
public void actionPerformed(ActionEvent e) {
Integer selection = numberList.getSelectedValue();
if (selection != null) {
listModel.removeElement(selection);
}
}
}
private static void createAndShowGui() {
NumberListEg mainPanel = new NumberListEg();
JFrame frame = new JFrame("Gui");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
can not be remove from the array,you can to make index to be empty,for example String a= Integer.toString(type for int),then a.replace("your int","");
I'd like to ask something about GridBagLayout because whenever I try to put the lblStudentID under lblName it just makes an output wherein it stays in the right side of lblName
import javax.swing.*;
import java.awt.*;
public class Student extends JPanel {
JLabel picture;
JLabel lblStudentID = new JLabel("Student ID:");
JLabel lblName = new JLabel("Name:");
JLabel lblProg = new JLabel("Program:");
JLabel lblGen = new JLabel("Gender:");
JPanel panel = new JPanel();
JRadioButton rbtn1 = new JRadioButton("Male");
JRadioButton rbtn2 = new JRadioButton("Female");
JComboBox cmbProg = new JComboBox();
TextField txtStudentID = new TextField();
TextField txtName = new TextField();
GridBagConstraints gbc = new GridBagConstraints();
public void setName() {
}
public Student() {
setLayout(new GridBagLayout());
picture = new JLabel(createImageIcon("images/student.jpg"));
picture.setSize(100, 100);
add(picture);
add(lblName, gbc);
gbc.gridx = 0;
gbc.gridy = 0;
add(txtName, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
add(lblStudentID, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
add(txtStudentID, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
}
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Student");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// Display the window.
Student LoginPane = new Student();
LoginPane.setOpaque(true); // content panes must be opaque
LoginPane.setLayout(new FlowLayout());
LoginPane.setBackground(Color.white);
frame.setContentPane(LoginPane);
frame.setVisible(true);
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = Student.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
First you change the GridBagConstraints too late.
gbc.gridx = 0;
gbc.gridy = 0;
add(lblName,gbc);
gbc.gridx = 1;
gbc.gridy = 0;
add(txtName,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
add(lblStudentID,gbc);
gbc.gridx = 1;
gbc.gridy = 1;
add(txtStudentID,gbc);
Second you change the layout back to FlowLayout of your LoginPane.
Edit
I have stripped-down your code. Maybe this will help you:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.TextField;
import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Student extends JPanel {
private static final long serialVersionUID = 8923497527096438302L;
private JLabel picture;
private JLabel lblStudentID = new JLabel("Student ID:");
private JLabel lblName = new JLabel("Name:");
TextField txtStudentID = new TextField();
TextField txtName = new TextField();
public Student() {
setLayout(new BorderLayout());
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setBackground(Color.WHITE);
JPanel formPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
formPanel.setOpaque(false);
picture = new JLabel(createImageIcon("images/student.jpg"));
picture.setPreferredSize(new Dimension(100, 100));
picture.setBorder(BorderFactory.createLineBorder(Color.black));
gbc.fill = GridBagConstraints.BOTH;
gbc.gridy = 0;
formPanel.add(picture, gbc);
gbc.gridy = 1;
gbc.gridwidth = 1;
formPanel.add(lblName, gbc);
gbc.gridy = 1;
gbc.weightx = 1;
formPanel.add(txtName, gbc);
gbc.weightx = 0;
gbc.gridy = 2;
formPanel.add(lblStudentID, gbc);
gbc.gridy = 2;
formPanel.add(txtStudentID, gbc);
add(formPanel, BorderLayout.PAGE_START);
}
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Student");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// Display the window.
new Student().setBackground(Color.white);
frame.setContentPane(new Student());
frame.setVisible(true);
}
protected static ImageIcon createImageIcon(String path) {
URL imgURL = Student.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
So what I am trying to do is create this:
I am using a gridbag layout and here is what I have so far:
public class board {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel leftTop = new JPanel();
leftTop.setPreferredSize(new Dimension(251,300));
leftTop.setBackground(Color.black);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(leftTop, c);
JPanel middleTop = new JPanel();
middleTop.setPreferredSize(new Dimension(251,200));
middleTop.setBackground(Color.green);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
pane.add(middleTop, c);
JPanel rightTop = new JPanel();
rightTop.setPreferredSize(new Dimension(251,600));
rightTop.setBackground(Color.blue);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
pane.add(rightTop, c);
JPanel leftBottom = new JPanel();
leftBottom.setPreferredSize(new Dimension(251,300));
leftBottom.setBackground(Color.red);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
pane.add(leftBottom, c);
JPanel middleBottom = new JPanel();
middleBottom.setPreferredSize(new Dimension(251,400));
middleBottom.setBackground(Color.yellow);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
pane.add(middleBottom, c);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
It creates something like:
How would I push up the panels so they are touching each other like in my first picture. I looked through the GridBagConstraints but I could not find anything that looked like it would work. Thanks!
Instead of trying to solve the complete layout problem with one layout manager, it's often simpler to nest layouts. For example, your example code could be modified to use a horizontal grid layout (to keep the columns equal width - I don't actually know if you want to force that. If not, then FlowLayout or BoxLayout would be better), and the columns use a BoxLayout each:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class board {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridLayout(1, 0));
JPanel left = new JPanel();
pane.add(left);
left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
JPanel leftTop = new JPanel();
leftTop.setPreferredSize(new Dimension(125, 150));
leftTop.setBackground(Color.black);
left.add(leftTop);
JPanel leftBottom = new JPanel();
leftBottom.setPreferredSize(new Dimension(125, 150));
leftBottom.setBackground(Color.red);
left.add(leftBottom);
JPanel middle = new JPanel();
pane.add(middle);
middle.setLayout(new BoxLayout(middle, BoxLayout.Y_AXIS));
JPanel middleTop = new JPanel();
middleTop.setPreferredSize(new Dimension(125, 100));
middleTop.setBackground(Color.green);
middle.add(middleTop);
JPanel middleBottom = new JPanel();
middleBottom.setPreferredSize(new Dimension(125, 200));
middleBottom.setBackground(Color.yellow);
middle.add(middleBottom);
JPanel right = new JPanel();
right.setPreferredSize(new Dimension(125, 300));
right.setBackground(Color.blue);
pane.add(right);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
Results in:
(I modified the preferred sizes a bit to make the image smaller. As a further note it's usually better to override getPreferredSize() rather than use setPreferredSize(); setPreferredSize() is convenient for the quick example though)
So I'm trying to create a series of radio buttons and check boxes that are displayed as follows:
Radio Button
Check Box
Radio Button
Check Box
Radio Button
However, I'm still in the learning process for java and I was wondering if anyone could solve this problem. At the moment the buttons and boxes are being displayed in the correct location, however the first radio button ("Courier") is not being displayed for some reason. If you could perhaps describe the reason and a possible solution that'd be great.
Thanks
Updated Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class Question2 {
public static void main(String[] args) {
MyFrame f = new MyFrame("Font Chooser");
f.init();
}
}
class MyFrame extends JFrame {
MyFrame(String title) {
super(title);
}
private JPanel mainPanel;
private GridBagConstraints gbc = new GridBagConstraints();
private GridBagLayout gbLayout = new GridBagLayout();
void init() {
mainPanel = new JPanel();
mainPanel.setLayout(gbLayout);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
this.setContentPane(mainPanel);
gbc.gridx = 0;
gbc.gridy = 1;
JCheckBox cb = new JCheckBox("Bold");
gbLayout.setConstraints(cb, gbc);
mainPanel.add(cb);
gbc.gridy = 3;
gbLayout.setConstraints(cb, gbc);
cb = new JCheckBox("Italic");
mainPanel.add(cb);
gbc.gridx = 1;
gbc.gridy = 0;
JRadioButton rb = new JRadioButton("Times");
gbLayout.setConstraints(rb, gbc);
mainPanel.add(rb, gbc);
gbc.gridy = 2;
rb = new JRadioButton("Helvatica");
mainPanel.add(rb, gbc);
rb = new JRadioButton("Courier");
gbc.gridy = 4;
mainPanel.add(rb, gbc);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
3 issues
Y Coordinate not re-assigned to different value causing last 2 radio buttons to exist at same location
GridBagConstraints not being used for left-hand side components
setConstraints erroneously being used to set constraints
Resultant code:
public class GoodGridBagApp {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Font Chooser");
frame.add(getMainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private JPanel getMainPanel() {
JPanel mainPanel = new JPanel();
GridBagConstraints gbc = new GridBagConstraints();
mainPanel.setLayout(new GridBagLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
gbc.gridx = 1;
gbc.gridy = 2;
JCheckBox cb = new JCheckBox("Bold");
mainPanel.add(cb, gbc);
gbc.gridy = 4;
cb = new JCheckBox("Italic");
mainPanel.add(cb, gbc);
gbc.gridx = 2;
gbc.gridy = 1;
JRadioButton rb = new JRadioButton("Times");
mainPanel.add(rb, gbc);
gbc.gridy = 3;
rb = new JRadioButton("Helvatica");
mainPanel.add(rb, gbc);
rb = new JRadioButton("Courier");
gbc.gridy = 5;
mainPanel.add(rb, gbc);
return mainPanel;
}
});
}
}
Read: How to Use GridBagLayout
I'm working on a simple gui.
I can't seem to spot the reason as to why my JMenuBar Is not appearing. What am i missing?
Here is the code below.
myMenuBar = new JMenuBar();
myFileMenu = new JMenu("File");
myRegisterItem = new JMenuItem("Register");
myMenuBar.add(myFileMenu);
myFileMenu.add(myRegisterItem);
setJMenuBar(myMenuBar);
The full class:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.rmi.Naming;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class GUI extends JFrame{
private JTextArea recievedField;
private JButton sendButton;
private JTextField messageField;
private JComboBox itemComboBox;
private JButton connectButton;
private JTextField userNameField;
private JPasswordField passwordField;
private JButton loginButton;
private JMenuBar myMenuBar;
private JMenu myFileMenu;
private JMenuItem myRegisterItem;
public static void main(String[] args) {
new GUI();
}
public GUI() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
public MainPane() {
myMenuBar = new JMenuBar();
setJMenuBar(myMenuBar);
myFileMenu = new JMenu("File");
myRegisterItem = new JMenuItem("Register");
myMenuBar.add(myFileMenu);
myFileMenu.add(myRegisterItem);
setBorder(new EmptyBorder(4, 4, 4, 4));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(new LoginPane(), gbc);
gbc.gridy++;
add(new ConnectPane(), gbc);
gbc.gridy++;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(new JScrollPane(recievedField = new JTextArea(5, 20)), gbc);
gbc.gridwidth = 1;
messageField = new JTextField(10);
sendButton = new JButton("Send");
gbc.gridy++;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(messageField, gbc);
gbc.gridx++;
gbc.weightx = 0;
gbc.insets = new Insets(5,5,5,5);
add(sendButton, gbc);
}
}
public class ConnectPane extends JPanel {
public ConnectPane() {
itemComboBox = new JComboBox();
itemComboBox.addItem("Select an Item");
connectButton = new JButton("Connect");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5,5,5,5);
add(itemComboBox, gbc);
gbc.gridx++;
gbc.weightx = 1;
gbc.insets = new Insets(5,5,5,5);
add(connectButton, gbc);
}
}
public class LoginPane extends JPanel {
public LoginPane() {
userNameField = new JTextField(10);
passwordField = new JPasswordField(10);
loginButton = new JButton("Login");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5,5,5,5);
add(new JLabel("User name:"), gbc);
gbc.gridx++;
gbc.insets = new Insets(5,5,5,5);
add(userNameField, gbc);
gbc.gridx++;
gbc.insets = new Insets(5,5,5,5);
add(new JLabel("Password:"), gbc);
gbc.gridx++;
gbc.insets = new Insets(5,5,5,5);
add(passwordField, gbc);
gbc.gridx++;
gbc.weightx = 1;
add(loginButton, gbc);
}
}
Your problem is you call setJMenuBar on JPanel instance this is wrong you should call:
frame.setJMenuBar(myMenuBar);
Also no need for:
frame.setLayout(new BorderLayout());
as JFrame contentPane Layout by default is BorderLayout
i.e change your code to:
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPane());
frame.setJMenuBar(myMenuBar);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Also do not use EventQueue rather SwingUtilities:
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPane());
frame.setJMenuBar(myMenuBar);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
Dont forget to comment out the setJmenuBar(..) in your MainPane class:
public MainPane() {
myMenuBar = new JMenuBar();
//notice no call to setJMenuBar
myFileMenu = new JMenu("File");
myRegisterItem = new JMenuItem("Register");
myMenuBar.add(myFileMenu);
...
}
Its because you have not set Your JMenuBar Visible.
Add this to your code
frame.setJMenuBar(myMenuBar); // This line to be added
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);