I created a menu in the menu bar in which I'd like to create a JCheckBoxMenuItem to set a condition for highlighting remaining menu items.
Something like the following pseudo-code:
if login(true)
then highlight remaining menuitems
else
un-highlight the menuitems
I think for highlighting you mean enable/disable a JMenuItem. That's possible.
Use setEnabled:
JMenuItem item;
item.setEnabled(false); //to disable
Like suggested by kleopatra, the best way of doing that is to implement your own action for each JMenuItem, and let your action to enable/disable the button accordingly to the state:
For example:
public class AMenuAction extends AbstractAction {
#override
public void actionPerformed(ActionEvent e) {
//implement your action behavior here
}
}
Then construct your JMenuItem with such action:
AMenuAction afterLoginAction = new AMenuAction();
JMenuItem item = new JMenuItem(afterLoginAction );
When the user logged in/out call setEnabled method on the desired actions.
void Login()
{
afterLoginAction.setEnabled(true);
}
Enabling and disabling menu items is done in the same way as for any other JComponent by using the setEnabled( boolean ) method
Create a JCheckBoxMenuItem as userlogin menu item
JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem();
then
add action listener to it
//unhighlite other menu items before login
jMenuFileOpen.setEnabled(false);
//...
jCheckBoxMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (login(true)){
jCheckBoxMenuItem.setSelected(true);
//highlite other menu items
jMenuFileOpen.setEnabled(true);
//...
} else {
jCheckBoxMenuItem.setSelected(false);
//unhighlite other menu items
jMenuFileOpen.setEnabled(false);
//...
}
}
});
once login(true) is successful the checkbox is checked on menu and other menu items are enabled.
Related
I have a class MyPopupMenu that extends JPopupMenu. Inside this popup menu I add a JMenuItem with an ActionListener which calls a process that it takes some minutes to return. I would like to make the popup menu close right after this item is pressed. My method inside the MyPopupMenu class is this:
private JMenuItem newItem(){
JMenuItem item=new JMenuItem();
item.setText("One");
item.setToolTipText("One");
ActionListener mylistener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
MyPopupMenu.this.setVisible(false);
Class1 class1=new Class1(file);
try {
class1.start();
} catch (IOException e) {
e.printStackTrace();
}
}
};
item.addActionListener(mylistener);
return item;
}
This doesn't work. After I press the item "One" the process starts but the popup menu remains open until the process returns (for some minutes). Is it possible to make the the popup menu disappear but the process continue running?
You are performing an action on the Swing main thread. This blocks your Gui from updating. You should move the starting of you class into another Thread.
For more info see Here
I have a JCheckBox with action listener implemented.
Here, when it's checked, something appears and when not checked, the appearance disappears.
To implement this, should I have two action listeners? How do I implement this?
you can use ActionListener for this and when that action is fired you can check it's selected or not
final JCheckBox checkBox = new JCheckBox("My checkbox");
checkBox.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// check if checkBox is selected or not
if(checkBox.isSelected()){
// here you can fire an event in which your checkbox is mark as selected
// and you can display the value you want to display
} else{
// checkBox is not selected so you can fire an event in which your checkbox is not selected
}
}
}
You have a single ActionListener and you check the selected state of the JCheckBox through the use of it's isSelected property. A ActionListener can not distinguish states on it's own, it simply responds to a user input
See How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listeners for more details.
I am adding a JMenuItem (Show History) that will toggle the appearance of a JPanel upon click. But after doing so, I want to change the title of that menu item to state the opposite action (Hide History). Is there a way to change just the text for that menu item, or must I remove the old JMenuItem and add a new one?
JMenuItem history = new JMenuItem("Show History");
history.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//code here to show the history
//history.changeText("Hide History") OR viewMenu.remove(history) and create/add new one
}
});
viewMenu.add(history);
So this is what you do:
history.setText("Hide History");
And make history final.
When I code a popup menu, I can check the mouse event and then
before calling show() prepare the menu.
Now I want a similar functionality for drop down menus, that
live in the menu bar. For example I have the following menu
bar layout:
Menu 1
MenuItem 1.1
MenuItem 1.1.1
MenuItem 1.1.2
..
MenuItem 1.1.n
Menu 2
Basically I want to generate the list MenuItem 1.1.1, MenuItem 1.1.2,
..., MenuItem 1.1.n dynamically when the drop down menu is invoked and
before it is shown.
How could I do this in Java/Swing? Is there a call back that covers both
L&F specific keyboard/mouse invokations of the menu bar?
Best Regards
You need to use a javax.swing.JMenuBar instead of a java.awt.MenuBar.
These MenuBars can add javax.swing.JMenus which have a addMenuListener(MenuListener l) method.
In this MenuListener you can dynamically add and delete the menus.
#Override
public void menuSelected(MenuEvent e) {
// Put your logic here
JMenu dynamicMenu = (JMenu) e.getSource();
dynamicMenu.add(new JMenuItem("MenuItem 1.1.1"));
}
#Override
public void menuDeselected(MenuEvent e) {
// Remove all MenuItems
JMenu dynamicMenu = (JMenu) e.getSource();
dynamicMenu.removeAll();
}
#Override
public void menuCanceled(MenuEvent e) {
// Remove all MenuItems
JMenu dynamicMenu = (JMenu) e.getSource();
dynamicMenu.removeAll();
}
To add the JMenuBar to a JFrame use the setJMenuBar(JMenuBar menubar) method.
I am creating a simple text editor similiar to Notepad.
It would insert the time and date to the file if the user presses F5.
I browsed about mnemonics and accelerators but they are used in combination with Alt and Ctrl respectively.
Should I use an EventListener or is there any other solution?
You could simply use:
JMenuItem menuItem = new JMenuItem("Refresh");
KeyStroke f5 = KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0);
menuItem.setAccelerator(f5);
with KeyStroke having 0 specifying no modifiers as described in the docs.
An ActionListener is the appropriate listener for menu item events.
As partly already mentioned in some comments, the recommended approach is
use an action to configure a menuItem
configure the action with an accelerator
add the action to the menu
Some code:
Action doLog = new AbstractAction("Dummny log!") {
#Override
public void actionPerformed(ActionEvent e) {
LOG.info("doing: " + getValue(Action.NAME));
}
};
doLog.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("F5"));
JMenu menu = new JMenu("dummy");
menu.add(doLog);
frame.getJMenuBar().add(menu);
You can add a KeyBinding to your JMenuItem like this:
Action sayHello = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"Hello World, From JMenuItem :)");
}
};
jMenuItem.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F5"),"sayHello");//4.The WHEN_IN_FOCUSED_WINDOW input maps of all the enabled components in the focused window are searched.
jMenuItem.getActionMap().put("sayHello",sayHello);
References:
http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#getInputMap(int)
http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html#howto