In the process of logging in, if the administrator is being logged in I want to enable JMenuItem called Admin. When program starts, its visibility is set to false. So when a user clicks Log In I run JMenu again and trying to recreate it just with Admin now set on true. Here's the code of my initialize method that's being run if it detects the user is an admin. The method is inside the JMenuBar class that the application uses.
public void initialize() {
file = new JMenu("File");
file.setName("File Menu");
file.addMouseListener(this);
help = new JMenu("Help");
help.setName("Help Menu");
help.addMouseListener(this);
login = new JMenu("Login");
login.setName("Login Menu");
login.addMouseListener(this);
language = new JMenu("Language");
language.setName("Language Menu");
language.addMouseListener(this);
admin = new JMenu("Admin");
admin.setName("Admin Menu");
if(LoginDialog.AdminLoggedIn) {
admin.setEnabled(true);
} else
admin.setEnabled(false);
admin.addMouseListener(this);
add(file);
add(login);
add(help);
add(language);
add(admin);
this.revalidate();
this.repaint();
}
Thanks.
I would suggest not re-creating your JMenu but rather modifying it, depending on the Administrator state of your program. You could give the program a Privileges enum, one with REGULAR and ADMINISTRATOR (and possibly other) fields, something like:
public enum Privileges {
REGULAR("Regular"), ADMINISTRATOR("Administrator");
private String text;
private Privileges(String text) {
this.text = text;
}
#Override
public String toString() {
return text;
}
}
And then change the state of your JMenu depending on the program's Privileges state. Also side note: don't use a MouseListener on your menus since these do not respect the enabled/disabled state of your menus / menuitems / buttons. For example, here is my small MVCE example, one that uses a Model-View-Controller structure (of sorts):
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;
#SuppressWarnings("serial")
public class MenuChanger extends JPanel {
private CreateMenu createMenu = new CreateMenu();
private MenuChngModel model = new MenuChngModel();
private ButtonGroup buttonGroup = new ButtonGroup();
public MenuChanger() {
setPreferredSize(new Dimension(400, 300));
model.addPropertyChangeListener(MenuChngModel.PRIVILEGES, new ModelListener());
JPanel privilegesPanel = new JPanel();
privilegesPanel.setLayout(new GridLayout(0, 1, 5, 5));
privilegesPanel.setBorder(BorderFactory.createTitledBorder("Privileges"));
for (final Privileges privileges : Privileges.values()) {
JRadioButton rBtn = new JRadioButton(privileges.toString());
if (privileges == Privileges.REGULAR) {
rBtn.setSelected(true);
}
rBtn.addActionListener(e -> {
createMenu.setPrivileges(privileges);
});
buttonGroup.add(rBtn);
privilegesPanel.add(rBtn);
}
add(privilegesPanel);
}
public CreateMenu getCreateMenu() {
return createMenu;
}
private class ModelListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
Privileges privileges = (Privileges) evt.getNewValue();
createMenu.setPrivileges(privileges);
}
}
private static void createAndShowGui() {
MenuChanger mainPanel = new MenuChanger();
JFrame frame = new JFrame("MenuChanger");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.setJMenuBar(mainPanel.getCreateMenu().getMenubar());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class MenuChngModel {
public static final String PRIVILEGES = "privileges";
private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(this);
private Privileges privileges = Privileges.REGULAR;
public Privileges getPrivileges() {
return privileges;
}
public void setPrivileges(Privileges privileges) {
Privileges oldValue = this.privileges;
Privileges newValue = privileges;
this.privileges = privileges;
pcSupport.firePropertyChange(PRIVILEGES, oldValue, newValue);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}
public void addPropertyChangeListener(String name, PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(name, listener);
}
public void removePropertyChangeListener(String name, PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(name, listener);
}
}
enum Privileges {
REGULAR("Regular"), ADMINISTRATOR("Administrator");
private String text;
private Privileges(String text) {
this.text = text;
}
#Override
public String toString() {
return text;
}
}
class CreateMenu {
private JMenuBar menubar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenu adminMenu = new JMenu("Administrator");
private JMenuItem fileMenuItem = new JMenuItem("File Menu Item");
private JMenuItem adminFileMenuItem = new JMenuItem("Admin File Menu Item");
public CreateMenu() {
fileMenu.setMnemonic(KeyEvent.VK_F);
adminMenu.setMnemonic(KeyEvent.VK_A);
fileMenu.add(fileMenuItem);
fileMenu.add(adminFileMenuItem);
adminMenu.add(new JMenuItem("Foo 1"));
adminMenu.add(new JMenuItem("Foo 2"));
adminFileMenuItem.setEnabled(false);
adminMenu.setEnabled(false);
menubar.add(fileMenu);
menubar.add(adminMenu);
}
public void setPrivileges(Privileges privileges) {
switch (privileges) {
case REGULAR:
adminMenu.setEnabled(false);
adminFileMenuItem.setEnabled(false);
break;
case ADMINISTRATOR:
adminMenu.setEnabled(true);
adminFileMenuItem.setEnabled(true);
break;
default:
break;
}
}
public JMenuBar getMenubar() {
return menubar;
}
}
Related
I have a JTabbedPane with two JPanels that need to stay in seperate classes. In PageOne, I want to be able to increment MyInteger by clicking the add button, and I then want to be able to print that integer in PageTwo by clicking the button there. It prints the correct value in PageOne, but prints 0 when I pass it to the PageTwo class and print it there.
How can I pass the value in such a way that it prints the correct value when clicking the button in both JPanels? I figure it has something to do with how I inherit from PageOne, but couldn't find a way of changing it on SO that solved my problem.
Main class:
import javax.swing.*;
public class MyJFrame {
PageOne pageOne;
PageTwo pageTwo;
public MyJFrame() {
JFrame f = new JFrame();
pageOne = new PageOne();
pageTwo = new PageTwo();
JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.addTab("Page One", pageOne);
jTabbedPane.addTab("Page Two", pageTwo);
f.add(jTabbedPane);
f.setSize(200,120);
f.setVisible(true);
}
public static void main(String[] args) throws InterruptedException {
new MyJFrame();
}
}
JPanel One:
import javax.swing.*;
public class PageOne extends JPanel {
public Integer myInteger = 0;
public JButton add;
public PageOne() {
add = new JButton();
add.setText("Increment number");
add(add);
add.addActionListener(actionEvent -> {
myInteger++;
printOne();
});
}
public void printOne() {
System.out.println("Page One:" + myInteger);
}
}
JPanel Two:
import javax.swing.*;
public class PageTwo extends JPanel {
PageOne pageOneRef = new PageOne();
public JButton button;
public PageTwo() {
JPanel panel = new JPanel();
button = new JButton("Click me");
panel.add(button);
add(panel);
button.addActionListener(e -> printTwo());
}
public void printTwo() {
System.out.println("Page Two:" + pageOneRef.myInteger);
}
}
The basic answer is, you need some kind of "container" which can be shared between the two components. This is commonly achieved through the use of a "model" of some kind.
See:
Model-View-Controller
Observer Pattern
Writing Event Listeners
for an overview of the concepts presented below
Runnable example
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
DefaultIntegerModel model = new DefaultIntegerModel();
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Page One", new PageOne(model));
tabbedPane.addTab("Page Two", new PageTwo(model));
frame.add(tabbedPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public interface IntegerModel {
public interface Observer {
public void valueDidChange(IntegerModel source, int value);
}
public int getValue();
public void addObserver(Observer observer);
public void removeObserver(Observer observer);
}
public interface MutableIntegerModel extends IntegerModel {
public void setValue(int value);
}
public class DefaultIntegerModel implements MutableIntegerModel {
private int value;
private List<Observer> observers;
public DefaultIntegerModel() {
this(0);
}
public DefaultIntegerModel(int value) {
this.value = value;
observers = new ArrayList<Observer>(8);
}
#Override
public void setValue(int value) {
this.value = value;
fireValueDidChange(value);
}
#Override
public int getValue() {
return value;
}
#Override
public void addObserver(Observer observer) {
observers.add(observer);
}
#Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}
protected void fireValueDidChange(int value) {
for (Observer observer : observers) {
observer.valueDidChange(this, value);
}
}
}
public class PageOne extends JPanel {
public JButton add;
private MutableIntegerModel model;
public PageOne(MutableIntegerModel model) {
this.model = model;
add = new JButton();
add.setText("Increment number");
add(add);
add.addActionListener(actionEvent -> {
model.setValue(model.getValue() + 1);
printOne();
});
}
public void printOne() {
System.out.println("Page One:" + model.getValue());
}
}
public class PageTwo extends JPanel {
private JButton button;
private JLabel label;
private IntegerModel model;
public PageTwo(IntegerModel model) {
this.model = model;
model.addObserver(new IntegerModel.Observer() {
#Override
public void valueDidChange(IntegerModel source, int value) {
System.out.println("Page two value did change to " + value);
label.setText(Integer.toString(model.getValue()));
}
});
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
label = new JLabel(Integer.toString(model.getValue()));
add(label, gbc);
button = new JButton("Click me");
button.addActionListener(e -> printTwo());
add(button, gbc);
}
public void printTwo() {
System.out.println("Page Two:" + model.getValue());
}
}
}
But why are there two models
Stop for a second and think about the responsibilities of each component.
PageOne want's to update the model, in order to do so, it also needs to know the value of the model. The model makes no assumption about "how" the consumer of this model will do that (so I didn't provide a increment method), it just allows the consumer to set the value it wants
PageTwo just wants to display the value (and be notified when some change occurs), so it doesn't need a mutable version of the model.
This restricts what consumers maybe able to do to the model rather the exposing functionality to parties which don't need it (and might be tempted to abuse it)
This is a demonstration and your needs may differ, but I'm bit of a scrooge when I design these kinds of things, I need the consumers to prove to me that they need functionality, rather then "assuming" what functionality they "might" require 😉
This is a practice known is "information hiding", which is supported by Polymorphism in OO languages
I have this java swing program, and im trying to figure out how can i create a button that upon clicking it will clear the text areas & change the icon of the person to put their hand down.
The buttons are dynamically generated using a for loop
And this
// To create buttons
for(int i=0 ; i < list.length; i++){
Participant pa = list[i];
JButton b = new JButton(pa.getNameButton(),participant);
b.addActionListener(e ->
{
String s = pa.toString() + questionPane.getText();
final ImageIcon raise = resizeIcon(new ImageIcon("src/raise.png"),30,30);
b.setIcon(raise);
JOptionPane.showMessageDialog(null,s,"Welcome to Chat Room",JOptionPane.INFORMATION_MESSAGE,pa.getImage());
});
p.add(b);
}
// Clear button logic
clearButton.addActionListener(e ->{
questionPane.setText("");
hostPane.setText("");
});
Okay, this is going to be a bit of fun.
The following example decouples much of the concept and makes use of a basic "observer pattern" to notify interested parties that the state has changed (ie, the chat's been cleared).
This is a basic concept where by you decouple the "what" from the "how", ie, "what" it is you want done (update the model) from the "how" it gets done (ie, button push). This makes it easier to adapt to more complex systems.
The example contains a ChatService, which has a single listener, which, for this example, simple tells interested parties that the chat has been cleared.
A more complex solution might have the ChatService generating events for when a user "raises" their hand, which allows the interested parties to deal with it in what ever way is relevant to them.
The example makes use of the Action API to decouple the work performed by each action from the UI itself. This helps create a single unit of work which is easier to deal with when you have a dynamic data set.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
ChatService chatService = new ChatService();
JPanel panel = new JPanel();
String[] names = new String[] {"Bryan", "Alan", "George", "Henry"};
List<PeopleAction> actions = new ArrayList<>(names.length);
for (String name : names) {
PeopleAction action = new PeopleAction(chatService, name, false);
actions.add(action);
}
Random rnd = new Random();
actions.get(rnd.nextInt(names.length)).setRaised(true);
for (Action action : actions) {
JButton btn = new JButton(action);
panel.add(btn);
}
setLayout(new GridLayout(2, 1));
add(panel);
JPanel hostPane = new JPanel();
JButton clearButton = new JButton(new ClearAction(chatService));
hostPane.add(clearButton);
add(hostPane);
}
}
public class ChatService {
private List<ChatListener> listeners = new ArrayList<>(25);
public void addChatListeners(ChatListener listener) {
listeners.add(listener);
}
public void removeChatListener(ChatListener listener) {
listeners.remove(listener);
}
protected void fireChatCleared() {
if (listeners.isEmpty()) {
return;
}
for (ChatListener listener : listeners) {
listener.chatCleared();
}
}
public void clear() {
// Do what's required
fireChatCleared();
}
}
public interface ChatListener {
public void chatCleared();
}
public class PeopleAction extends AbstractAction implements ChatListener {
private String name;
private boolean raised;
public PeopleAction(ChatService chatService, String name, boolean raised) {
// You can use either LARGE_ICON_KEY or SMALL_ICON to set the icon
this.name = name;
if (raised) {
putValue(NAME, "* " + name);
} else {
putValue(NAME, name);
}
chatService.addChatListeners(this);
}
public void setRaised(boolean raised) {
if (raised) {
putValue(NAME, "* " + name);
} else {
putValue(NAME, name);
}
}
public boolean isRaised() {
return raised;
}
#Override
public void actionPerformed(ActionEvent evt) {
// Do what ever needs to be done
setRaised(!isRaised());
}
#Override
public void chatCleared() {
setRaised(false);
}
}
public class ClearAction extends AbstractAction {
private ChatService chatService;
public ClearAction(ChatService chatService) {
this.chatService = chatService;
putValue(NAME, "Clear");
}
#Override
public void actionPerformed(ActionEvent evt) {
chatService.clear();
}
}
}
I have a help pane which appears at the start of a program, but can be turned off. If the user wants it to return, there is an option in the menu bar to reactivate it. However, when they choose to show it from the help menu, it automatically rechecks the "do not show again" box. How do I keep the box in the same state the user originally had it, but still open the help pane?
Gui:
public class Gui {
private Game game;
private JFrame frame;
private MenuBar menuBar;
private HelpDialog helpMenu;
private boolean showHelp;
public Gui(Game game) {
this.game = game;
this.showHelp = true;
this.createAndShowGUI();
}
public boolean shouldShowHelpDialog() {
return this.showHelp;
}
public void displayHelp() {
this.helpMenu.showHelpDialog();
}
MenuBar:
public class MenuBar {
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuItem;
private JFrame frame;
private Gui gui;
private Game game;
public MenuBar(JFrame frame, Gui gui, Game game) {
this.menuBar = new JMenuBar();
this.frame = frame;
this.gui = gui;
this.game = game;
}
public void buildMenuBar() {
this.buildFileMenu();
this.buildSettingsMenu();
this.buildHelpMenu();
this.frame.setJMenuBar(this.menuBar);
}
private void buildHelpMenu() {
this.menu = new JMenu("Information");
this.menu.setMnemonic(KeyEvent.VK_I);
this.menu.getAccessibleContext().setAccessibleDescription("Help menu");
JMenuItem menuHelp = new JMenuItem("Help", KeyEvent.VK_H);
menuHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MenuBar.this.gui.displayHelp();
}
});
this.menu.add(menuHelp);
this.menuBar.add(this.menu);
}
HelpDialog:
public class HelpDialog {
private boolean shouldShowHelpDialog;
private JFrame theFrame;
public HelpDialog(boolean helpDialog, JFrame frame) {
this.shouldShowHelpDialog = helpDialog;
this.theFrame = frame;
}
public boolean showHelpDialog() {
if (!this.shouldShowHelpDialog) {
return false;
}
JCheckBox shouldShowCheckBox = new JCheckBox("Do not show this message again", this.shouldShowHelpDialog);
Object[] msgContent = { this.buildHelpPane(), shouldShowCheckBox };
JOptionPane.showMessageDialog(this.theFrame, msgContent, "Help", JOptionPane.INFORMATION_MESSAGE);
return shouldShowCheckBox.isSelected();
}
private Object buildHelpPane() {
String helpMessage = "Game rules: This is how you play.";
JTextArea helpTextArea = new JTextArea(helpMessage);
helpTextArea.setRows(6);
helpTextArea.setColumns(40);
helpTextArea.setLineWrap(true);
helpTextArea.setWrapStyleWord(true);
helpTextArea.setEditable(false);
helpTextArea.setOpaque(false);
JScrollPane helpPane = new JScrollPane(helpTextArea);
return helpPane;
}
}
EDIT:
Updated HelpDialog class:
public class HelpDialog {
private boolean shouldShowHelpDialog;
private JFrame theFrame;
private JCheckBox shouldShowCheckBox;
public HelpDialog(boolean helpDialog, JFrame frame) {
this.shouldShowHelpDialog = helpDialog;
this.theFrame = frame;
this.shouldShowCheckBox = new JCheckBox("Do not show this message again", this.shouldShowHelpDialog);
}
public boolean showHelpDialog() {
if (!this.shouldShowHelpDialog) {
return false;
}
Object[] msgContent = { this.buildHelpPane(), shouldShowCheckBox };
JOptionPane.showMessageDialog(this.theFrame, msgContent, "Help", JOptionPane.INFORMATION_MESSAGE);
return shouldShowCheckBox.isSelected();
}
The checkbox remains unmarked now when displaying the help menu through the menu bar. However, now when a new game is created, it will show the help dialog even if the box is unchecked.
Full answer includes this change to the method in the GUI:
public void displayHelp() {
this.showHelp = this.helpMenu.showHelpDialog();
}
Your showHelpDialog() method creates a new checkbox each time it is called. You should create the dialog once in the constructor, and showHelpDialog() should just display it.
You can add a parameter to showHelpDialog which overrides your request
public boolean showHelpDialog(boolean override) {
if(!override){
if (!this.shouldShowHelpDialog) {
return false;
}
}
JCheckBox shouldShowCheckBox = new JCheckBox("Do not show this message again", this.shouldShowHelpDialog);
Object[] msgContent = { this.buildHelpPane(), shouldShowCheckBox };
JOptionPane.showMessageDialog(this.theFrame, msgContent, "Help", JOptionPane.INFORMATION_MESSAGE);
return shouldShowCheckBox.isSelected();
}
and call
showHelpDialog(true);
when clicked from menu.
So I have a JFrame set up with a menu with the current structure that looks something along the lines of this:
File
Exit
Pages
Reviews
A
B
C
Help
About
I want to create a Action Listener that only listens to menu items under Reviews. Is this a possibility (and if so, how) or do I have to create a generic listener and check if it's one of those items?
Yes, it is possible:
Store your menu items as fields
Add the same ActionListener to each menu item.
In the listener check for the source to know which item was clicked.
Should look like:
public class YourFrame extends JFrame implements ActionListener {
private final JMenuItem menuA, menuB;
public YourFrame(){
super("Your app");
JMenuBar menuBar = new JMenuBar();
JMenu menuReviews = new JMenu("Reviews");
menuA = new JMenuItem("A");
menuB = new JMenuItem("B");
...
menuReviews.add(menuA);
menuReviews.add(menuB);
menuBar.add(menuReviews);
setJMenuBar(menuBar);
...
menuA.addActionListener(this);
menuB.addActionListener(this);
...
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==menuA){
System.out.println("Menu A clicked");
...
}else if(event.getSource()==menuB){
System.out.println("Menu B clicked");
...
}
}
}
Note that here I let the JFrame implement ActionListener, but this is just for convenience. You could use a dedicated class, or an anonymous class created in the constructor:
ActionListener reviewsListener = new ActionListener(){
public void actionPerformed(ActionEvent event){
if(event.getSource()==menuA){
System.out.println("Menu A clicked");
...
}else if(event.getSource()==menuB){
System.out.println("Menu B clicked");
...
}
}
};
menuA.addActionListener(reviewsListener);
menuB.addActionListener(reviewsListener);
If you want to integrate this process a little more, I could also suggest to extend JMenu, so that you can pass it your action listener and add it systematically to new menu items.
public class YourJMenu extends JMenu {
private ActionListener listener;
public YourJMenu(String name, ActionListener listener){
super(name);
this.listener = listener;
}
#Override
public JMenuItem add(JMenuItem item){
item.addActionListener(listener);
return super.add(item);
}
}
With this, you just need to write:
JMenu menuReviews = new YourJMenu("Reviews", this);
and drop the:
menuA.addActionListener(this);
menuB.addActionListener(this);
Using a common method we can add the action listener to all the menu items under a menu. Below is a example code.
public class MenuItemEvent {
JFrame objFrm = new JFrame("Menu event demo");
JMenuBar mBar;
JMenu mnu;
JMenuItem mnuItem1, mnuItem2, mnuItem3;
public void show() {
objFrm.setSize(300, 300);
mBar = new JMenuBar();
mnu = new JMenu("Reviews");
mBar.add(mnu);
mnuItem1 = new JMenuItem("A");
mnu.add(mnuItem1);
mnuItem2 = new JMenuItem("B");
mnu.add(mnuItem2);
mnuItem3 = new JMenuItem("C");
mnu.add(mnuItem3);
//method call
fnAddActionListener(mnu);
objFrm.setJMenuBar(mBar);
objFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
objFrm.setVisible(true);
}
//method to add action listener to all menu items under a menu
public void fnAddActionListener(JMenu mnu) {
if (mnu.getItemCount() != 0) {
for (int iCount = 0; iCount < mnu.getItemCount(); iCount++) {
(mnu.getItem(iCount)).addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
fnMenuItemAction(e);
}
});
}
}
}
//menu item action event
public void fnMenuItemAction(ActionEvent e) {
if (e.getSource().equals(mnuItem1)) {
System.out.println("Menu Item 1");
} else if (e.getSource().equals(mnuItem2)) {
System.out.println("Menu Item 2");
} else if (e.getSource().equals(mnuItem3)) {
System.out.println("Menu Item 3");
}
}
public static void main(String[] args) {
new MenuItemEvent().show();
}
}
or with the below function
//fnMenuItemAdd(mnu,mnuItem1)
//etc.
public void fnMenuItemAdd(JMenu mnu, JMenuItem mni) {
mnu.add(mni);
mni.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
fnMenuItemAction(e);
}
});
}
My menuitem are being added thorugh database .
i have to perform action such as opening the new jframe , if a user select a particular menuitem.
Here the menu dimension is add to the Menubar , and under which various menuitem are being added such as Period , Entity, which are being fetch from database.
Now i want to open a new jframe on the click of Period menuitem .
public void MenuExp(){
JMenu DimensionMenu = new JMenu("Dimension");
JMenu editMenu = new JMenu("Help");
jMenuBar1.add(DimensionMenu);
jMenuBar1.add(editMenu);
//JMenuItem newAction = new JMenuItem("Account");
//fileMenu.add(newAction);
//JMenuItem newPeriod = new JMenuItem("Period");
//fileMenu.add(newPeriod);
try{
Class.forName("oracle.jdbc.OracleDriver");
Connection comm = (Connection)DriverManager.getConnection("jdbc:oracle:thin:#192.168.100.25:1521:orcl","SYSTEM","Admin123");
Statement st = comm.createStatement();
String Query = "select OBJECT_NAME from RAHUL_APP1.HSP_OBJECT where OBJECT_TYPE = 2 AND OBJECT_ID <> 30" ;
//and User_Name ='" + jTextField1.getText()+"'";
ResultSet rs = st.executeQuery(Query);
while(rs.next()){
JMenuItem newAction = new JMenuItem(rs.getString(1));
DimensionMenu.add(newAction);
rs.close();
st.close();
comm.close();
newAction.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
System.out.println("You have clicked on the Account");
}
});
}
} catch(Exception e){
JOptionPane.showMessageDialog(this,e);
}
}
You need to do some parametrization of the frame or have for example frame class stored also in DB and initialize it using reflexion...
Update:
Implementation can be like this:
package betlista.so.swing.menuitemdialogsfromdb;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MainFrame extends JFrame {
public MainFrame() {
super("Frame");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
add(createMenu());
pack();
}
private JMenuBar createMenu() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Open");
menu.add(new DialogCreatingMenuItem("Dialog 1", "betlista.so.swing.menuitemdialogsfromdb.MainFrame$MyDialog1"));
menu.add(new DialogCreatingMenuItem("Dialog 2", "betlista.so.swing.menuitemdialogsfromdb.MainFrame$MyDialog2"));
menuBar.add(menu);
return menuBar;
}
class DialogCreatingMenuItem extends JMenuItem implements ActionListener {
String className;
public DialogCreatingMenuItem(String text, String className) throws HeadlessException {
super(text);
this.className = className;
addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent ae) {
try {
Class<JDialog> clazz = (Class<JDialog>)Class.forName(this.className);
JDialog dialog = clazz.newInstance();
dialog.setVisible(true);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
public static class MyDialog1 extends JDialog {
public MyDialog1() {
setTitle("Dialog 1");
add(new JLabel("Dialog 1"));
pack();
}
}
public static class MyDialog2 extends JDialog {
public MyDialog2() {
setTitle("Dialog 2");
add(new JLabel("Dialog 2"));
pack();
}
}
public static void main(String[] args) {
new MainFrame().setVisible(true);
}
}
where Strings in
menu.add(new DialogCreatingMenuItem("Dialog 1", "betlista.so.swing.menuitemdialogsfromdb.MainFrame$MyDialog1"));
menu.add(new DialogCreatingMenuItem("Dialog 2", "betlista.so.swing.menuitemdialogsfromdb.MainFrame$MyDialog2"));
are retrieved from database...
Here is a sample code:
menuItem1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
...
}
});
Remember the steps to creating a menu:
1. Create a MenuBar and add to the panel
2. Create a Menu and add to MenuBar
3. Create a MenuItem and add to Menu
Then add the listener to the MenuItem
Edit: if you use it outside the try statement it should work
Now i want to open a new jframe on the click of Period menuitem
Of course you have to add an ActionListener to your menu to do that, but the real question is How do you determine the right listener to each menu item? Since you fetch your menu items from database then it's not that easy as it looks like.
Note: see The Use of Multiple JFrames, Good/Bad Practice?
Option 1 (kind of dirty)
As I've said, you could store an action command and set it back to the menu item just like you set the menu's name:
while(rs.next()) {
String menuName = rs.getString("menuname");
String actionCommand = rs.getString("actioncommand");
JMenuItem newAction = new JMenuItem(menuName);
newAction.setActionCommand(actionCommand);
DimensionMenu.add(newAction);
...
}
Then you can have a listener that make use of ActionEvent#getActionCommand() to decide the right action to perform and attach this listener to all your menu items:
class MenuActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent evt) {
String actionCommand = evt.getActionCommand();
switch (actionCommand) {
case "OpenNewDialog": openNewDialog(); break;
...
}
}
private void openNewDialog() {
// implementation here
}
}
Then:
ActionListener listener = new MenuActionListener();
while(rs.next()) {
String menuName = rs.getString("menuname");
String actionCommand = rs.getString("actioncommand");
JMenuItem newAction = new JMenuItem(menuName);
newAction.setActionCommand(actionCommand);
newAction.addActionListener(listener);
DimensionMenu.add(newAction);
...
}
Option 2
Implement Factory method pattern to create a specific ActionListener or Action based on menu's action command:
class ActionListenerFactory {
public static Action createAction(final String actionCommand) {
switch (actionCommand) {
case "OpenNewDialog": return openNewDialogAction(); break;
...
}
}
private Action openNewDialogAction() {
Action action = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent evt) {
// open new dialog here
}
};
return action;
}
}
Then:
while(rs.next()) {
String menuName = rs.getString("menuname");
String actionCommand = rs.getString("actioncommand");
JMenuItem newAction = new JMenuItem(menuName);
newAction.setActionCommand(actionCommand);
newAction.addActionListener(ActionListenerFactory.createAction(actionCommand));
DimensionMenu.add(newAction);
...
}
See also:
How to Use Actions tutorial.
Concurrency in Swing lesson.
Worker threads and SwingWorker to do database calls in a background thread and create/update Swing component in the Event Dispatch Thread