Click button to open a JPanel

Page 1 of 1

1 Replies - 947 Views - Last Post: 15 November 2012 - 04:55 PM

#1 A.K  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-November 12

Click button to open a JPanel

Posted 15 November 2012 - 04:43 PM

Hey all, I'm new to Java literally just started and got to make a stock control program. Anyway in this code below I need it so when the Button (MenuButton) is clicked the JPanel (Menu) will open however, I want the JPanel(Menu) hidden so it only show when the button is clicked. I've tried a lot of things but nothing seems to work, all the help would be immensely appreciated! Thanks!

Here's the code so far for the GUI:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Toolkit;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.UIManager;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JComboBox;
import javax.swing.JTree;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.SystemColor;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.border.MatteBorder;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;

public class Homepage extends JFrame {

private JPanel contentPane;
private JPanel Menu;
private JButton MenuButton;

/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Homepage frame = new Homepage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Homepage() {
setTitle("Homepage");
setIconImage(Toolkit.getDefaultToolkit().getImage(Homepage.class.getResource("/Monoceros/resources/home page icon.png")));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1040, 738);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);

MenuButton = new JButton("");
MenuButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
}
});
MenuButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JPanel Menu = new JPanel();
getContentPane().add(Menu);
}
});
MenuButton.setIcon(new ImageIcon(Homepage.class.getResource("/Monoceros/resources/monoceros logo Menu.png")));
MenuButton.setToolTipText("Click to see Menu");


Menu = new JPanel();
Menu.addAncestorListener(new AncestorListener() {
public void ancestorAdded(AncestorEvent arg0) {

}
public void ancestorMoved(AncestorEvent arg0) {
}
public void ancestorRemoved(AncestorEvent arg0) {
}
});
Menu.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(MenuButton, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
.addComponent(Menu, GroupLayout.PREFERRED_SIZE, 452, GroupLayout.PREFERRED_SIZE))
.addContainerGap(562, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(MenuButton, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(Menu, GroupLayout.DEFAULT_SIZE, 604, Short.MAX_VALUE)
.addContainerGap())
);

JPanel panel_1 = new JPanel();
panel_1.setBackground(UIManager.getColor("scrollbar"));

JPanel panel_2 = new JPanel();
panel_2.setBackground(SystemColor.scrollbar);

JLabel lblSales = new JLabel("Sales");
lblSales.setIcon(new ImageIcon(Homepage.class.getResource("/Monoceros/resources/Sales Icon - Small.png")));
lblSales.setForeground(Color.WHITE);
lblSales.setFont(new Font("Calibri", Font.BOLD, 28));
GroupLayout gl_panel_2 = new GroupLayout(panel_2);
gl_panel_2.setHorizontalGroup(
gl_panel_2.createParallelGroup(Alignment.LEADING)
.addGap(0, 169, Short.MAX_VALUE)
.addGroup(gl_panel_2.createSequentialGroup()
.addContainerGap()
.addComponent(lblSales)
.addContainerGap(21, Short.MAX_VALUE))
);
gl_panel_2.setVerticalGroup(
gl_panel_2.createParallelGroup(Alignment.LEADING)
.addGap(0, 94, Short.MAX_VALUE)
.addGroup(gl_panel_2.createSequentialGroup()
.addGap(25)
.addComponent(lblSales)
.addContainerGap(27, Short.MAX_VALUE))
);
panel_2.setLayout(gl_panel_2);

JPanel panel_3 = new JPanel();
panel_3.setBackground(SystemColor.scrollbar);

JLabel lblPurchases = new JLabel("Purchases");
lblPurchases.setIcon(new ImageIcon(Homepage.class.getResource("/Monoceros/resources/Purchasing icon - Small.png")));
lblPurchases.setForeground(Color.WHITE);
lblPurchases.setFont(new Font("Calibri", Font.BOLD, 28));
GroupLayout gl_panel_3 = new GroupLayout(panel_3);
gl_panel_3.setHorizontalGroup(
gl_panel_3.createParallelGroup(Alignment.LEADING)
.addGap(0, 169, Short.MAX_VALUE)
.addGroup(gl_panel_3.createSequentialGroup()
.addContainerGap()
.addComponent(lblPurchases)
.addContainerGap(21, Short.MAX_VALUE))
);
gl_panel_3.setVerticalGroup(
gl_panel_3.createParallelGroup(Alignment.LEADING)
.addGap(0, 94, Short.MAX_VALUE)
.addGroup(gl_panel_3.createSequentialGroup()
.addGap(25)
.addComponent(lblPurchases)
.addContainerGap(27, Short.MAX_VALUE))
);
panel_3.setLayout(gl_panel_3);

JPanel panel_4 = new JPanel();
panel_4.setBackground(SystemColor.scrollbar);

JLabel lblInventory = new JLabel("Inventory");
lblInventory.setIcon(new ImageIcon(Homepage.class.getResource("/Monoceros/resources/Inventory Icon Small.png")));
lblInventory.setForeground(Color.WHITE);
lblInventory.setFont(new Font("Calibri", Font.BOLD, 28));
GroupLayout gl_panel_4 = new GroupLayout(panel_4);
gl_panel_4.setHorizontalGroup(
gl_panel_4.createParallelGroup(Alignment.LEADING)
.addGap(0, 169, Short.MAX_VALUE)
.addGap(0, 169, Short.MAX_VALUE)
.addGroup(gl_panel_4.createSequentialGroup()
.addContainerGap()
.addComponent(lblInventory)
.addContainerGap(21, Short.MAX_VALUE))
);
gl_panel_4.setVerticalGroup(
gl_panel_4.createParallelGroup(Alignment.LEADING)
.addGap(0, 94, Short.MAX_VALUE)
.addGap(0, 94, Short.MAX_VALUE)
.addGroup(gl_panel_4.createSequentialGroup()
.addGap(25)
.addComponent(lblInventory)
.addContainerGap(27, Short.MAX_VALUE))
);
panel_4.setLayout(gl_panel_4);

JPanel panel_5 = new JPanel();
panel_5.setBackground(SystemColor.scrollbar);

JLabel lblReports = new JLabel("Reports");
lblReports.setIcon(new ImageIcon(Homepage.class.getResource("/Monoceros/resources/ReportsIcon - Small.png")));
lblReports.setForeground(Color.WHITE);
lblReports.setFont(new Font("Calibri", Font.BOLD, 28));
GroupLayout gl_panel_5 = new GroupLayout(panel_5);
gl_panel_5.setHorizontalGroup(
gl_panel_5.createParallelGroup(Alignment.LEADING)
.addGap(0, 180, Short.MAX_VALUE)
.addGap(0, 169, Short.MAX_VALUE)
.addGap(0, 169, Short.MAX_VALUE)
.addGroup(gl_panel_5.createSequentialGroup()
.addContainerGap()
.addComponent(lblReports)
.addContainerGap(21, Short.MAX_VALUE))
);
gl_panel_5.setVerticalGroup(
gl_panel_5.createParallelGroup(Alignment.LEADING)
.addGap(0, 94, Short.MAX_VALUE)
.addGap(0, 94, Short.MAX_VALUE)
.addGap(0, 94, Short.MAX_VALUE)
.addGroup(gl_panel_5.createSequentialGroup()
.addGap(25)
.addComponent(lblReports)
.addContainerGap(27, Short.MAX_VALUE))
);
panel_5.setLayout(gl_panel_5);

JPanel panel_6 = new JPanel();
panel_6.setBackground(SystemColor.scrollbar);

JLabel lblSettings = new JLabel("Settings");
lblSettings.setIcon(new ImageIcon(Homepage.class.getResource("/Monoceros/resources/Setting-icon - Small.png")));
lblSettings.setForeground(Color.WHITE);
lblSettings.setFont(new Font("Calibri", Font.BOLD, 28));
GroupLayout gl_panel_6 = new GroupLayout(panel_6);
gl_panel_6.setHorizontalGroup(
gl_panel_6.createParallelGroup(Alignment.LEADING)
.addGap(0, 180, Short.MAX_VALUE)
.addGap(0, 180, Short.MAX_VALUE)
.addGap(0, 169, Short.MAX_VALUE)
.addGap(0, 169, Short.MAX_VALUE)
.addGroup(gl_panel_6.createSequentialGroup()
.addContainerGap()
.addComponent(lblSettings)
.addContainerGap(21, Short.MAX_VALUE))
);
gl_panel_6.setVerticalGroup(
gl_panel_6.createParallelGroup(Alignment.LEADING)
.addGap(0, 88, Short.MAX_VALUE)
.addGap(0, 94, Short.MAX_VALUE)
.addGap(0, 94, Short.MAX_VALUE)
.addGap(0, 94, Short.MAX_VALUE)
.addGroup(gl_panel_6.createSequentialGroup()
.addGap(25)
.addComponent(lblSettings)
.addContainerGap(27, Short.MAX_VALUE))
);
panel_6.setLayout(gl_panel_6);
GroupLayout gl_Menu = new GroupLayout(Menu);
gl_Menu.setHorizontalGroup(
gl_Menu.createParallelGroup(Alignment.LEADING)
.addGroup(gl_Menu.createSequentialGroup()
.addContainerGap()
.addGroup(gl_Menu.createParallelGroup(Alignment.LEADING)
.addGroup(gl_Menu.createSequentialGroup()
.addComponent(panel_5, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
.addContainerGap(262, Short.MAX_VALUE))
.addGroup(gl_Menu.createSequentialGroup()
.addComponent(panel_6, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
.addContainerGap(262, Short.MAX_VALUE))
.addGroup(gl_Menu.createSequentialGroup()
.addGroup(gl_Menu.createParallelGroup(Alignment.TRAILING, false)
.addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(panel_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(panel_3, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addComponent(panel_4, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE))
.addContainerGap(262, Short.MAX_VALUE))))
);
gl_Menu.setVerticalGroup(
gl_Menu.createParallelGroup(Alignment.LEADING)
.addGroup(gl_Menu.createSequentialGroup()
.addContainerGap()
.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
.addGap(4)
.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(panel_5, GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(panel_6, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
.addGap(33))
);

JLabel lblNewLabel = new JLabel("General");
lblNewLabel.setIcon(new ImageIcon(Homepage.class.getResource("/Monoceros/resources/General icon.png")));
lblNewLabel.setForeground(Color.WHITE);
lblNewLabel.setFont(new Font("Calibri", Font.BOLD, 28));
GroupLayout gl_panel_1 = new GroupLayout(panel_1);
gl_panel_1.setHorizontalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addContainerGap()
.addComponent(lblNewLabel)
.addContainerGap(21, Short.MAX_VALUE))
);
gl_panel_1.setVerticalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(25)
.addComponent(lblNewLabel)
.addContainerGap(27, Short.MAX_VALUE))
);
panel_1.setLayout(gl_panel_1);
Menu.setLayout(gl_Menu);
contentPane.setLayout(gl_contentPane);
}
private static void addPopup(Component component, final JPopupMenu popup) {
component.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {

showMenu(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
private void showMenu(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Click button to open a JPanel

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,121
  • Joined: 27-December 08

Re: Click button to open a JPanel

Posted 15 November 2012 - 04:55 PM

Please remember to use code tags in the future: :code:. Also, Swing does not fall under the category of Java EE.

We do not support the visual GUI Builders due to the horrid code they produce. Write your code by hand and we will be happy to help!

In the meantime, check out CardLayout.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1