How to change icon for JmenuItem on rollover - java

I want to put a rollover effect in my JMenu this is my code:
Icon firstPicAcc= new ImageIcon(Welcome.class.getResource("/app/resources/user1.jpg"));
Icon secPicAcc= new ImageIcon(Welcome.class.getResource("/app/resources/user2.jpg"));
JMenu mnAccountSettings = new JMenu("Account Settings");
mnAccountSettings.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent arg0) {
}
});
mnAccountSettings.setFont(new Font("Dialog", Font.PLAIN, 20));
mnAccountSettings.setForeground(new Color(0, 153, 0));
mnAccountSettings.setBackground(new Color(255, 204, 255));
mnAccountSettings.setRolloverEnabled(true);
mnAccountSettings.setIcon(firstPicAcc);
mnAccountSettings.setRolloverIcon(secPicAcc);
mnAccount.add(mnAccountSettings);
how can i do that? thanks!
What should happen is when I rolled my mouse over the JMenu bar the original icon should change into another icon.

What you want to do is add a ChangeListener to the JMenuItem and check if it's selected or armed and change the icon accordingly. The ChangeListener works for both keyboard and mouse.
See this good read by #kleopatra
private JMenuItem createMenuItem(final ImageIcon icon, String title) {
JMenuItem item = new JMenuItem(title);
item.setIcon(icon);
ChangeListener cl = new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
if (e.getSource() instanceof JMenuItem) {
JMenuItem item = (JMenuItem) e.getSource();
if (item.isSelected() || item.isArmed()) {
item.setIcon(stackIcon);
} else {
item.setIcon(icon);
}
}
}
};
item.addChangeListener(cl);
return item;
}
Here is running example. Just replace images with yours
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class RolloverMenuItem {
ImageIcon stackIcon = new ImageIcon(getClass().getResource("/resources/stackoverflow2.png"));
public RolloverMenuItem() {
ImageIcon newIcon = new ImageIcon(getClass().getResource("/resources/image/new.gif"));
ImageIcon saveIcon = new ImageIcon(getClass().getResource("/resources/image/open.gif"));
ImageIcon openIcon = new ImageIcon(getClass().getResource("/resources/image/save.gif"));
JMenu menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
JMenuItem item1 = createMenuItem(newIcon, "New");
JMenuItem item2 = createMenuItem(openIcon, "Open");
JMenuItem item3 = createMenuItem(saveIcon, "Save");
menu.add(item1);
menu.add(item2);
menu.add(item3);
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
JFrame frame = new JFrame("Rollover MenuItem");
frame.setJMenuBar(menuBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 250);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JMenuItem createMenuItem(final ImageIcon icon, String title) {
JMenuItem item = new JMenuItem(title);
item.setIcon(icon);
ChangeListener cl = new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
if (e.getSource() instanceof JMenuItem) {
JMenuItem item = (JMenuItem) e.getSource();
if (item.isSelected() || item.isArmed()) {
item.setIcon(stackIcon);
} else {
item.setIcon(icon);
}
}
}
};
item.addChangeListener(cl);
return item;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new RolloverMenuItem();
}
});
}
}

Related

How to add Action Listener from another class java?

I want to develop a very basic application, well here is the code, I have 4 classes one is the main class, the second one is JMenuBar, the third one is Panel and the fourth one is for Action Listener. at the moment my problem is here how to add instance of Action Listener for class of JMenuBar, till i could do some action.
public class My_Action implements ActionListener {
public My_Action() {
}
#Override
public void actionPerformed(ActionEvent actionEvent) {
}
}
public class My_Menu extends JMenuBar {
private My_Action my_action = new My_Action();
JMenu file;
JMenu Edit;
JMenu help;
public My_Menu() {
file = new JMenu("File");
Edit = new JMenu("Edit");
help = new JMenu("help");
JMenuItem item1 = new JMenuItem("file");
JMenuItem item2 = new JMenuItem("New");
JMenuItem item3 = new JMenuItem("Setting");
JMenuItem item4 = new JMenuItem("Color");
JMenuItem item5 = new JMenuItem("Print");
JMenuItem item6 = new JMenuItem("Exit");
file.add(item1);
file.add(item2);
file.add(item3);
file.add(item4);
file.add(item5);
file.add(item6);
file.addSeparator();
this.add(file);
this.add(Edit);
this.add(help);
item1.addActionListener(my_action);
}
public void actionPerformed(ActionEvent actionEvent){
System.exit(0);
}
}
This is my test program for you.
Check the console output when executing the menu program.
package just.test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class My_Menu extends JMenuBar implements ActionListener {
private static final long serialVersionUID = 1L;
private My_Action my_action = new My_Action();
JMenu file;
JMenu Edit;
JMenu help;
public My_Menu() {
file = new JMenu("File");
Edit = new JMenu("Edit");
help = new JMenu("help");
this.add(file);
this.add(Edit);
this.add(help);
JMenuItem item1 = new JMenuItem("file");
item1.setActionCommand("file");
JMenuItem item2 = new JMenuItem("New");
item2.setActionCommand("New");
JMenuItem item3 = new JMenuItem("Setting");
item3.setActionCommand("Setting");
JMenuItem item4 = new JMenuItem("Color");
item4.setActionCommand("Color");
JMenuItem item5 = new JMenuItem("Print");
item5.setActionCommand("Print");
JMenuItem item6 = new JMenuItem("Exit");
item6.setActionCommand("Exit");
JMenuItem item7 = new JMenuItem("edit1");
item7.setActionCommand("edit1");
JMenuItem item8 = new JMenuItem("edit2");
item8.setActionCommand("edit2");
JMenuItem item9 = new JMenuItem("about..");
item9.setActionCommand("about");
file.add(item1);
file.add(item2);
file.addSeparator();
file.add(item3);
file.add(item4);
file.add(item5);
file.add(item6);
Edit.add(item7);
Edit.add(item8);
help.add(item9);
item1.addActionListener(my_action);
item2.addActionListener(my_action);
item3.addActionListener(my_action);
item4.addActionListener(my_action);
item5.addActionListener(my_action);
item7.addActionListener(this);
item8.addActionListener(this);
item9.addActionListener(this);
file.addActionListener(this);
Edit.addActionListener(this);
help.addActionListener(my_action);
}
#Override
public void actionPerformed(ActionEvent actionEvent) {
String command = actionEvent.getActionCommand();
System.out.println("----1----\n" + command);
//System.exit(0);
}
class My_Action implements ActionListener {
public My_Action() {
}
#Override
public void actionPerformed(ActionEvent actionEvent) {
String command = actionEvent.getActionCommand();
System.out.println("----2----\n" + command);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame();
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
f.add(new My_Menu(), BorderLayout.NORTH);
f.add(jp, BorderLayout.CENTER);
jp.setSize(500,400);
f.setSize(800, 600);
f.setVisible(true);
//f.pack();
}
});
}
}
You can just add a addActionListener to the component in which you have to send a message.
item1.addActionListener(my_action);
or
item7.addActionListener(this);
Don't forget to implement a ActionListener on your My_Menu class.
What you have is right.
It should call action performed when you click on file menu item. Just put a print ln in that method to see it being called.

Swing - last JMenuItem occupy the rest of space on JMenuBar

I find that if I have a JMenuBar, and the last element of it (the rightmost one) is a JMenuItem, it will occupy all the rest blank space on the JMenuBar, which is definitely not what we want.
Imagine an "About" JMenuItem as the rightmost item on a JMenuBar. It should only occupy the same space as the other menus.
See my SSCCE: (click the menu and hover over the menuitem on the right to see the effect)
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class JMenuItemLastOnMenuBar {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createGUI();
}
});
}
private static void createGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 300, 300);
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Menu 1");
JMenuItem item1 = new JMenuItem("Item 1");
menu.add(item1);
bar.add(menu);
JMenuItem item2 = new JMenuItem("Item 2") {
#Override
public Dimension getPreferredSize() {
return new Dimension(120, 25);
}
};
bar.add(item2);
frame.setJMenuBar(bar);
frame.pack();
frame.setVisible(true);
}
}
You should override the method getMaximumSize
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class JMenuItemLastOnMenuBar {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createGUI();
}
});
}
private static void createGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 300, 300);
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Menu 1");
JMenuItem item1 = new JMenuItem("Item 1");
menu.add(item1);
bar.add(menu);
JMenuItem item2 = new JMenuItem("Item 2") {
#Override
public Dimension getMaximumSize() {
Dimension d1 = super.getPreferredSize();
Dimension d2 = super.getMaximumSize();
d2.width = d1.width;
return d2;
}
};
bar.add(item2);
frame.setJMenuBar(bar);
frame.pack();
frame.setVisible(true);
}
}

Swing JFrame button action listener not working

I want to move from one JFrame to another JFrame after pressing JButton named as login. I am attaching my both frame code below, if there is correction or any one want to give me instruction or guideline then can give.
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Login implements ActionListener {
private JFrame jFrame = null;
private JPanel jContentPane = null;
private JLabel id = null;
private JTextField userid = null;
private JLabel pass = null;
private JPasswordField password = null;
private JButton login = null;
private JFrame getJFrame()
{
if (jFrame == null)
{
jFrame = new JFrame();
jFrame.setSize(new Dimension(346, 301));
jFrame.setResizable( false );
jFrame.setTitle("Shree Datta Digambar");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setContentPane(getJContentPane());
}
return jFrame;
}
private JPanel getJContentPane()
{
if (jContentPane == null)
{
pass = new JLabel();
pass.setBounds(new Rectangle(21, 105, 116, 27));
pass.setText("Password");
id = new JLabel();
id.setBounds(new Rectangle(21, 54, 119, 26));
id.setText("Enter User Id");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(id, null);
jContentPane.add(getUserid(), null);
jContentPane.add(pass, null);
jContentPane.add(getPassword(), null);
jContentPane.add(getLogin(), null);
}
return jContentPane;
}
private JTextField getUserid()
{
if (userid == null)
{
userid = new JTextField();
userid.setBounds(new Rectangle(164, 53, 140, 26));
}
return userid;
}
private JPasswordField getPassword()
{
if (password == null)
{
password = new JPasswordField();
password.setBounds(new Rectangle(165, 106, 137, 27));
}
return password;
}
private JButton getLogin()
{
if (login == null)
{
login = new JButton();
login.setBounds(new Rectangle(165, 169, 136, 27));
login.setText("Login");
login.addActionListener((ActionListener) this);
}
return login;
}
public static void main(String[] args)
{
Login l=new Login();
JFrame f=new JFrame();
f=l.getJFrame();
f.setLocation(300,150);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==login)
{
if(userid.getText().equals("")||password.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Please Enter Details");
}
else
{
if(userid.getText().equals("digambar")&&password.getText().equals("dalvi"))
{
JOptionPane.showMessageDialog(null,"Welcome To Customer Details of Papers");
getJFrame().dispose();
JMenuBar menuBar = new JMenuBar();
MainMenu f = new MainMenu();
}
else
{
JOptionPane.showMessageDialog(null,"Sorry Please Enter Valid Details");
userid.setText("");
password.setText("");
}
}
}
}
}
second frame code is
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MainMenu extends JFrame implements ActionListener
{
public static void main(String[] args)
{
MainMenu f=new MainMenu();
f.setVisible(true);
f.setSize(646,401);
f.setResizable( false );
f.setTitle("Shree Datta Digambar");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
f.setJMenuBar(menuBar);
JMenu customer=new JMenu("Customer");
menuBar.add(customer);
JMenu paper=new JMenu("Paper");
menuBar.add(paper);
JMenu edit=new JMenu("Edit");
menuBar.add(edit);
JMenu view=new JMenu("View");
menuBar.add(view);
JMenu bill=new JMenu("Bill");
menuBar.add(bill);
JMenu help=new JMenu("Help");
menuBar.add(help);
JMenuItem newItem = new JMenuItem("New Customer");
customer.add(newItem);
JMenuItem deleteItem = new JMenuItem("Delete Customer");
customer.add(deleteItem);
JMenuItem addpaperItem = new JMenuItem("Add Paper");
paper.add(addpaperItem);
JMenuItem deletepaperItem = new JMenuItem("Delete Paper");
paper.add(deletepaperItem);
JMenuItem customer_detail = new JMenuItem("Customer Detail");
edit.add(customer_detail);
JMenuItem paper_detail = new JMenuItem("Paper Detail");
edit.add(paper_detail);
JMenuItem perticular_customer_detail = new JMenuItem("Perticular Customer Detail");
view.add(perticular_customer_detail);
JMenuItem customer_bill = new JMenuItem("Customer Bills");
bill.add(customer_bill);
newItem.addActionListener(new MainMenu());
deleteItem.addActionListener(new MainMenu());
addpaperItem.addActionListener(new MainMenu());
deletepaperItem.addActionListener(new MainMenu());
customer_detail.addActionListener(new MainMenu());
paper_detail.addActionListener(new MainMenu());
perticular_customer_detail.addActionListener(new MainMenu());
customer_bill.addActionListener(new MainMenu());
customer.addActionListener(new MainMenu());
paper.addActionListener(new MainMenu());
edit.addActionListener(new MainMenu());
view.addActionListener(new MainMenu());
bill.addActionListener(new MainMenu());
help.addActionListener(new MainMenu());
}
#Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
You can make use of constructors of a class. Constructors are used to initialize objects. I have used constructors. Please find updated code.
Login.java
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.*;
public class Login implements ActionListener {
private JFrame jFrame = null;
private JPanel jContentPane = null;
private JLabel id = null;
private JTextField userid = null;
private JLabel pass = null;
private JPasswordField password = null;
private JButton login = null;
//Constructor
public Login() {
pass = new JLabel();
pass.setBounds(new Rectangle(21, 105, 116, 27));
pass.setText("Password");
id = new JLabel();
id.setBounds(new Rectangle(21, 54, 119, 26));
id.setText("Enter User Id");
userid = new JTextField();
userid.setBounds(new Rectangle(164, 53, 140, 26));
password = new JPasswordField();
password.setBounds(new Rectangle(165, 106, 137, 27));
login = new JButton();
login.setBounds(new Rectangle(165, 169, 136, 27));
login.setText("Login");
login.addActionListener((ActionListener) this);
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(id, null);
jContentPane.add(userid, null);
jContentPane.add(pass, null);
jContentPane.add(password, null);
jContentPane.add(login, null);
jFrame = new JFrame();
jFrame.setSize(new Dimension(346, 301));
jFrame.setResizable( false );
jFrame.setTitle("Shree Datta Digambar");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setContentPane(jContentPane);
jFrame.setLocation(300,150);
jFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==login)
{
if(userid.getText().equals("")||password.getPassword().equals(""))
{
JOptionPane.showMessageDialog(null, "Please Enter Details");
}
else
{
if(userid.getText().equals("digambar")&& Arrays.equals(password.getPassword(), "dalvi".toCharArray()))
{
JOptionPane.showMessageDialog(null,"Welcome To Customer Details of Papers");
this.jFrame.dispose();
MainMenu f = new MainMenu();
}
else
{
JOptionPane.showMessageDialog(null,"Sorry Please Enter Valid Details");
userid.setText("");
password.setText("");
}
}
}
}
public static void main(String[] args){
Login l=new Login();
}
}
MainMenu.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MainMenu extends JFrame implements ActionListener
{
//Constructor
public MainMenu()
{
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
this.setVisible(true);
this.setSize(646,401);
this.setResizable( false );
this.setTitle("Shree Datta Digambar");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenu customer=new JMenu("Customer");
menuBar.add(customer);
JMenu paper=new JMenu("Paper");
menuBar.add(paper);
JMenu edit=new JMenu("Edit");
menuBar.add(edit);
JMenu view=new JMenu("View");
menuBar.add(view);
JMenu bill=new JMenu("Bill");
menuBar.add(bill);
JMenu help=new JMenu("Help");
menuBar.add(help);
JMenuItem newItem = new JMenuItem("New Customer");
customer.add(newItem);
JMenuItem deleteItem = new JMenuItem("Delete Customer");
customer.add(deleteItem);
JMenuItem addpaperItem = new JMenuItem("Add Paper");
paper.add(addpaperItem);
JMenuItem deletepaperItem = new JMenuItem("Delete Paper");
paper.add(deletepaperItem);
JMenuItem customer_detail = new JMenuItem("Customer Detail");
edit.add(customer_detail);
JMenuItem paper_detail = new JMenuItem("Paper Detail");
edit.add(paper_detail);
JMenuItem perticular_customer_detail = new JMenuItem("Perticular Customer Detail");
view.add(perticular_customer_detail);
JMenuItem customer_bill = new JMenuItem("Customer Bills");
bill.add(customer_bill);
}
#Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public static void main(String arg[])
{
MainMenu menu1= new MainMenu();
}
}
Instead of separate frames, you could construct your UI based on JPanels and either use CardLayout to switch between them or a JDialog for the login panel and a JFrame for the main panel
The main point is you can separate the controlling mechanism from the UI components, making it easier to change the path of logic should you have to.
Take a look at How to Use CardLayout and How to Make Dialogs for more details.
You should also make appropriate use of layout managers
You can use anonymous inner classes for your JMenuItem,
JMenuItem newItem = new JMenuItem("New Customer");
newItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu.this.dispose();
New_Customer nn = new New_Customer() {};
}
});
or
Let MainMenu implement the ActionListener interface (class MainMenu implements ActionListener)
JMenuItem newItem = new JMenuItem("New Customer");
newItem.addActionListener(this);
newItem.setActionCommand("New Customer");
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("New Customer"))
{
System.out.println("clicked new customer menu");
MainMenu.this.dispose();
New_Customer nn = new New_Customer() {};
}
}

JMenu wont appear in my jFrame

I cant see my JMenu in the frame when I run it, what should i do?
I removed the panel where it was before, then i just want to put it in my frame
package app.ui;
import java.awt.Color;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.WindowConstants;
import app.model.User;
import app.util.JMenusss;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class JMenus {
private JFrame menuu;
private SecurityQuestion securityQuestion;
private User user;
private JMenu mnAccount;
public JMenus(JFrame menuu) {
this.menuu = menuu;
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
final JLabel lblHome = new JLabel("");
lblHome.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent arg0) {
Welcome myWelcome = new Welcome();
menuu.dispose();
}
});
lblHome.setIcon(new ImageIcon(JMenus.class.getResource("/app/resources/home-icon.png")));
lblHome.setBounds(780, 4, 88, 83);
menuu.getContentPane().add(lblHome);
final JLabel lblItem = new JLabel("");
lblItem.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent arg0) {
ItemManagement myItemManagement = new ItemManagement();
myItemManagement.ItemManagement();
menuu.dispose();
}
});
lblItem.setIcon(new ImageIcon(JMenus.class.getResource("/app/resources/items.png")));
lblItem.setBounds(860, 4, 88, 83);
menuu.getContentPane().add(lblItem);
final JLabel lblGroupManagement = new JLabel("");
lblGroupManagement.setIcon(new ImageIcon(JMenus.class.getResource("/app/resources/group11.png")));
lblGroupManagement.setBounds(940, 4, 88, 83);
menuu.getContentPane().add(lblGroupManagement);
lblGroupManagement.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e) {
GroupManagement myGroupManagement = new GroupManagement();
myGroupManagement.groupManagement();
menuu.dispose();
}
});
final JLabel lblInventory = new JLabel("");
lblInventory.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e) {
IOStock myInventory = new IOStock();
myInventory.InventoryWindow();
menuu.dispose();
}
});
lblInventory.setIcon(new ImageIcon(JMenus.class.getResource("/app/resources/IO.png")));
lblInventory.setBounds(1020, 4, 88, 83);
menuu.getContentPane().add(lblInventory);
final JLabel lblLogout = new JLabel("");
lblLogout.setIcon(new ImageIcon(JMenus.class.getResource("/app/resources/lock.png")));
lblLogout.setBounds(1100, 4, 120, 83);
menuu.getContentPane().add(lblLogout);
lblLogout.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e) {
int selectedOption = JOptionPane.showConfirmDialog(null,"You are about to logout, are you sure?","Choose",JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) {
Login window = new Login();
window.frmLogin.setVisible(true);
menuu.dispose();
}
}
});
This is where my JMenu is
JMenuBar mnbMenu = new JMenuBar();
mnbMenu.setBackground(Color.WHITE);
mnbMenu.setBounds(100, 4, 80, 89);
menuu.getContentPane().add(mnbMenu);
mnAccount = new JMenu();
mnAccount.setBackground(Color.WHITE);
mnAccount.setForeground(Color.WHITE);
mnAccount.setIcon(new ImageIcon("/app/resources/Settings-icon.png"));
mnAccount.setBounds(1180, 4, 100, 100);
mnbMenu.add(mnAccount);
JMenuItem mntmChangeUsername = new JMenuItem("Change Username");
mntmChangeUsername.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent arg0) {
UpdateUserName updateUsername = new UpdateUserName(user);
updateUsername.setVisible(true);
}
});
//mntmChangeUsername.setBackground(Color.WHITE);
mnAccount.add(mntmChangeUsername);
JMenuItem mntmChangePassword = new JMenuItem("Change Password");
mntmChangePassword.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent arg0) {
ChangeUsername changeUsername = new ChangeUsername(menuu);
changeUsername.changeAcc();
}
});
mntmChangePassword.setBackground(Color.WHITE);
mnAccount.add(mntmChangePassword);
JMenuItem mntmChangeSecurityQuestion = new JMenuItem("Change Security Question");
mntmChangeSecurityQuestion.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent arg0) {
ChangeSecurityQuestion changeSecurity = new ChangeSecurityQuestion(user, securityQuestion);
changeSecurity.setVisible(true);
changeSecurity.setLocationRelativeTo(null);
changeSecurity.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
changeSecurity.setAlwaysOnTop(true);
}
});
mntmChangeSecurityQuestion.setBackground(Color.WHITE);
mnAccount.add(mntmChangeSecurityQuestion);
}
}
This is my code...
I want to add the JMenu on my Frame,,, but its not visible, why?
Here:
JMenuBar mnbMenu = new JMenuBar();
...
menuu.getContentPane().add(mnbMenu);
The correct way to set the menu bar to a JFrame is through setJMenuBar() method:
JMenuBar mnbMenu = new JMenuBar();
...
menuu.setJMenuBar(mnbMenu);
Take a look to How to Use Menus tutorial. Additionaly you may want to see this topic Why JMenuBar is not place in the JFrame content pane(...)
Side note
Take a look to all #AndrewThompson's tips:
MCTaRE (Minimal Complete Tested and Readable Example)
Nowhere in those code snippets is a call to method setJMenuBar
Don't add a mouse listener to a menu. Add an ActionListener or an Action instead.
To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space.

java desktop application

i am creating a desktop application in netbeans and i want that in my menu bar if i select a new menu item than o nly the below panel is change not full frame.so it will look like that i m working on a single frame.can anyone help me out
You can use Card Layout Managers to achieve such functionality.
Here is complete example:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class JMenuExample extends JFrame implements ActionListener {
JMenu menu;
JPanel panelMain;
JPanel panelRed;
JPanel panelBlue;
CardLayout layout;
public void createUI() {
createMenu();
createPanels();
}
private void createPanels() {
panelMain = new JPanel();
layout = new CardLayout();
panelMain.setLayout(layout);
panelRed = new JPanel();
panelRed.setBackground(Color.RED);
panelMain.add(panelRed, "Red");
panelBlue = new JPanel();
panelBlue.setBackground(Color.BLUE);
panelMain.add(panelBlue, "Blue");
add(panelMain);
}
private void createMenu() {
menu = new JMenu("Change To");
JMenuItem miRed = new JMenuItem("Red");
miRed.addActionListener(this);
menu.add(miRed);
JMenuItem miBlue = new JMenuItem("Blue");
miBlue.addActionListener(this);
menu.add(miBlue);
JMenuBar bar = new JMenuBar();
bar.add(menu);
setJMenuBar(bar);
}
public static void main(String[] args) {
JMenuExample frame = new JMenuExample();
frame.createUI();
frame.setSize(150, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JMenuItem) {
JMenuItem mi = (JMenuItem) e.getSource();
layout.show(panelMain, mi.getText());
}
}
}
Hope this helps

Categories

Resources