How to get/set JRadionButtonMenuItem dynamically? - java

This example has Radio Buttons located on a Sub Menu as seen here.
What I would like to do is anytime the "Change Radio Button" button is pressed, it will change which button is selected on the menu. This means it has to first retrieve which is currently set then select the other.
Granted for this simple sample the Radio Buttons could be made instance variables to make things easy but image the JMenu and associated sub-menus and radio buttons are generated is some class further down in the bowels of the program. Direct access is not that direct.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
public class RadioButtonsOnMenu
{
public static void main(final String args[])
{
JFrame frame = new JFrame("MenuSample Example");
JButton jButton = new JButton("Change Radio Button");
jButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("Changing Radion Button");
//How to change the JButton on the menu?
//frame.getMenuBar().......
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jPanel = new JPanel();
jPanel.add(jButton);
frame.add(jPanel);
frame.setJMenuBar(buildMenu());
frame.setSize(350, 250);
frame.setVisible(true);
}
public static JMenuBar buildMenu()
{
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
fileMenu.add(newMenuItem);
JMenu findOptionsMenu = new JMenu("Options");
findOptionsMenu.setMnemonic(KeyEvent.VK_O);
fileMenu.add(findOptionsMenu);
ButtonGroup directionGroup = new ButtonGroup();
JRadioButtonMenuItem forwardMenuItem = new JRadioButtonMenuItem("Forward", true);
forwardMenuItem.setMnemonic(KeyEvent.VK_F);
findOptionsMenu.add(forwardMenuItem);
directionGroup.add(forwardMenuItem);
JRadioButtonMenuItem backwardMenuItem = new JRadioButtonMenuItem("Backward");
backwardMenuItem.setMnemonic(KeyEvent.VK_B);
findOptionsMenu.add(backwardMenuItem);
directionGroup.add(backwardMenuItem);
return menuBar;
}
}
It is not clear how best to access the sub-menu and associated radio button settings within the JButton action.
jButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("Changing Radion Button");
//How to change the JButton on the menu?
//frame.getMenuBar().......
}
});
I could probably get the Menu Bar from the Frame and drill down figure the code can get messy if there Menu Bar has numerous items, sub items and even multiple radio button groups.
Is there a more direct way to find out which Radio Buttons on the menu are selected as well as a more direct way to change their value?

The "trick" is to create an application model to hold the value of the menu radio buttons.
Here's the GUI I created.
I started the Swing application with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
I created a JFrame and a JButton JPanel. I separated the creation of the JFrame and JPanel.
I created an application model class to hold a boolean which determines whether forward or backward is selected. The JButton ActionListener switches the state of the boolean. The updateRadioButtonMenu method updates the selected state of the radio button menu items.
Here's the complete runnable code.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
public class RadioButtonsOnMenu implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new RadioButtonsOnMenu());
}
private ApplicationModel model;
private JRadioButtonMenuItem backwardMenuItem;
private JRadioButtonMenuItem forwardMenuItem;
public RadioButtonsOnMenu() {
this.model = new ApplicationModel();
}
#Override
public void run() {
JFrame frame = new JFrame("MenuSample Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(createMenuBar());
frame.add(createButtonPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
fileMenu.add(newMenuItem);
JMenu findOptionsMenu = new JMenu("Options");
findOptionsMenu.setMnemonic(KeyEvent.VK_O);
fileMenu.add(findOptionsMenu);
ButtonGroup directionGroup = new ButtonGroup();
forwardMenuItem = new JRadioButtonMenuItem("Forward", model.isForward());
forwardMenuItem.setMnemonic(KeyEvent.VK_F);
findOptionsMenu.add(forwardMenuItem);
directionGroup.add(forwardMenuItem);
backwardMenuItem = new JRadioButtonMenuItem("Backward", !model.isForward());
backwardMenuItem.setMnemonic(KeyEvent.VK_B);
findOptionsMenu.add(backwardMenuItem);
directionGroup.add(backwardMenuItem);
return menuBar;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
JButton button = new JButton("Change Radio Button");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
model.setForward(!model.isForward());
updateRadioButtonMenu();
}
});
panel.add(button);
return panel;
}
public void updateRadioButtonMenu() {
forwardMenuItem.setSelected(model.isForward());
backwardMenuItem.setSelected(!model.isForward());
}
public class ApplicationModel {
private boolean isForward;
public ApplicationModel() {
this.isForward = true;
}
public boolean isForward() {
return isForward;
}
public void setForward(boolean isForward) {
this.isForward = isForward;
}
}
}

What you could do is to save the state in a boolean. You could add listener to the radio buttons and change the boolean everytime one of them is selected
boolean isForward = true;
so when changed to backward you set the value to false. This way you don't need to get The state of the radiobuttons everytime.
In your button actionlistener you could then do:
jButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("Changing Radion Button");
forwardRadioButton.setState(!isForward);
backwardRadioButton.setState(isForward);
iSForward = !isForward;
}
});

I would say you need to do these two things:
Since you seem to have a lot of items it is best to store them somewhere else
1 Create a class to store all the items and then just pass the class
public class myItemHolder{
//declare all the items here instead of at the main
JButton jButton = new JButton("Change Radio Button");
ButtonGroup directionGroup = new ButtonGroup();
JRadioButtonMenuItem forwardMenuItem = new JRadioButtonMenuItem("Forward", true);
JRadioButtonMenuItem backwardMenuItem = new JRadioButtonMenuItem("Backward");
myListener(myItemHolder items){
directionGroup.add(forwardMenuItem);
}
public ButtonGroup getButtons() {
return directionGroup;
}
public JButton getClick() {
return jButton;
}
}
2 Create your own action listener class like so
public class myListener implements ActionListener{
myItemHolders items;
myListener(myItemHolder items){
this.items=items;
}
#Override
public void actionPerformed(ActionEvent e) {
//get the radiobutton like so and do what you want with it
items.getButtons()
}
}
Now you just need to do this in the main:
public class RadioButtonsOnMenu
{
public static void main(final String args[])
{
myItemHolder items = new myItemHolder();
items.getClick.addActionListener(new myListener(items));
}
}
And there you can access everything easily :)
It is all a matter of where you declare your stuff.
Or if you only want to send the ButtonGroup you can just do so by changing the structure a little so the actionlistener only requests the ButtonGroup and give it items.getButtons() instead of items.

Here's another possible way to go about it using Enum state tracking. I make use of a Enum and a Map to track the Radio Button that should be activated. This lets it scale as large as you want for associated Radio Button items within the same JMenu.
RadioMenu
package tools;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.JMenu;
import javax.swing.JRadioButtonMenuItem;
public class RadioMenu<E extends Enum<E>> extends JMenu {
private static final long serialVersionUID = 1L;
private E currentState;
private JRadioButtonMenuItem selectedRadioButton;
private HashMap<E, JRadioButtonMenuItem> stateMap;
public RadioMenu() {
stateMap = new HashMap<E, JRadioButtonMenuItem>();
}
public RadioMenu(String name) {
super(name);
stateMap = new HashMap<E, JRadioButtonMenuItem>();
}
public void addRadioButton(E associatedState, JRadioButtonMenuItem radioButton) {
//Set default to first added button
if(stateMap.isEmpty()) {
currentState = associatedState;
radioButton.setSelected(true);
selectedRadioButton = radioButton;
}
add(radioButton);
stateMap.put(associatedState, radioButton);
radioButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setState(associatedState);
}
});
}
public void generateButtonsFromEnum(Class<E> enumType) {
for(E enumValue : enumType.getEnumConstants()) {
addRadioButton(enumValue, new JRadioButtonMenuItem(enumValue.toString()));
}
}
public E getState() {
return currentState;
}
public void setState(E newState) {
currentState = newState;
selectedRadioButton.setSelected(false);
selectedRadioButton = stateMap.get(newState);
selectedRadioButton.setSelected(true);
}
}
RadioMenuTest
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import tools.RadioMenu;
public class RadioMenuTest implements Runnable {
public enum RadioOptions {
Forward, Backward, Left, Right
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new RadioMenuTest());
}
private RadioMenu<RadioOptions> optionsMenu;
#Override
public void run() {
JFrame frame = new JFrame("RadioMenu Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(createMenuBar());
frame.getContentPane().add(createButtonPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
optionsMenu = new RadioMenu<RadioOptions>("Options");
optionsMenu.generateButtonsFromEnum(RadioOptions.class);
fileMenu.add(optionsMenu);
return menuBar;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new FlowLayout());
JButton setBackwardButton = new JButton("Set To Backward");
setBackwardButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
optionsMenu.setState(RadioOptions.Backward);
}
});
panel.add(setBackwardButton);
JButton setRightButton = new JButton("Set To Right");
setRightButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
optionsMenu.setState(RadioOptions.Right);
}
});
panel.add(setRightButton);
return panel;
}
}

Related

How to have one JPanel show up and the second one disappear on button click?

So I have 3 separate classes, the settings button on the mainmenu class should switch to main menu, but it simply hides the first panel, same thing when i click return on the other menu, i would like to find a simple soluton without using a layout manager because i don't know how to have card layout communicate to the 2 classes, but thats the solution, it'd be nice if someone could give me some pointers on how to implement that:
public class Game extends JFrame {
MainMenu mainMenu;
Settings settings;
public Game(){
setSize(900,900);
setDefaultCloseOperation(3);
mainMenu = new MainMenu();
settings = new Settings();
mainMenu.setSettings(settings);
settings.setMainMenu(mainMenu);
add(settings,BorderLayout.CENTER);
add(mainMenu, BorderLayout.CENTER);
}
public static void main(String[] args) {
Game game = new Game();
game.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainMenu extends JPanel {
Settings settings;
public void setSettings(Settings settings) {
this.settings = settings;
}
public MainMenu() {
setLayout(new GridLayout(1,3));
JButton Newgame = new JButton("New Game");
JButton Cont = new JButton("Continue");
JButton Sett = new JButton("Settings");
add(Newgame);
add(Cont);
SwitchMenu1 switchMenu1 = new SwitchMenu1();
Sett.addActionListener(switchMenu1);
add(Sett);
}
class SwitchMenu1 implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
if(isVisible()){
settings.setVisible(true);
setVisible(false);
}
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Settings extends JPanel {
MainMenu mainMenu;
public void setMainMenu(MainMenu mainMenu) {
this.mainMenu = mainMenu;
}
public Settings(){
JButton Return = new JButton("Return");
SwitchMenu2 switchMenu2 = new SwitchMenu2();
Return.addActionListener(switchMenu2);
add(Return, BorderLayout.SOUTH);
}
class SwitchMenu2 implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
if(isVisible()){
mainMenu.setVisible(true);
setVisible(false);
}
}
}
}
I want to have the other JPanel show up on button click, but it doesn't work, the first one simply disappears. How can i fix this?
Thanks a lot!
This sounds a use case for CardLayout. You have a JPanel, named for example cards, which uses a CardLayout manager. You add all your panels (cards) to that panel, giving them unique names (e.g., "MAIN_MENU", "SETTINGS", etc.). Then, instead of passing every other panel in each of your panels, you only pass the cards panel, which can be used to show the card you wish, e.g., cl.show(cards, "SETTINGS"); on clicking a button, for instance.
Update
As per #c0der's suggestion (see comments section below), the code structure has been updated.
Game.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Game extends JFrame {
JPanel cards;
CardLayout cardLayout;
public Game(){
MainMenu mainMenu = new MainMenu();
Settings settings = new Settings();
cardLayout = new CardLayout();
cards = new JPanel(cardLayout);
cards.add(mainMenu, "MAIN_MENU");
cards.add(settings, "SETTINGS");
mainMenu.setSetBtnActionListener(new BtnController("SETTINGS"));
settings.setReturnBtnActionListener(new BtnController("MAIN_MENU"));
add(cards);
setSize(640,480);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
class BtnController implements ActionListener {
String cardName;
public BtnController(String cardName) {
this.cardName = cardName;
}
#Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(cards, cardName);
}
}
public static void main(String[] args) {
new Game();
}
}
MainMenu.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class MainMenu extends JPanel {
JButton setBtn;
public MainMenu() {
setLayout(new GridLayout(1, 3));
JButton newGameBtn = new JButton("New Game");
JButton contBtn = new JButton("Continue");
setBtn = new JButton("Settings");
add(newGameBtn);
add(contBtn);
add(setBtn);
}
public void setSetBtnActionListener(ActionListener al) {
setBtn.addActionListener(al);
}
}
Settings.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class Settings extends JPanel {
JButton returnBtn;
public Settings() {
returnBtn = new JButton("Return");
setLayout(new BorderLayout());
add(returnBtn, BorderLayout.SOUTH);
}
public void setReturnBtnActionListener(ActionListener al) {
returnBtn.addActionListener(al);
}
}

Adding a scrollable menu to a submenu

I'm trying to adapt to submenus the code found at https://coderanch.com/t/343946/java/Scrolling-JMenu in order to have a scrollbar rather than buttons like suggested in the answer of Java: Creating a scrolling submenu. However I am having a some troubles for getting a "standard" behaviour:
The parent menu should not disappear when the mouse is focused/hovering on the scrollable menu (see Font menu).
The scrollable menu should disappear when the mouse is not focused/hovering on it or on its parent submenu (see Font menu).
The scrollable menu should not disappear even if the text of the submenu is long (see Help menu).
Thanks in advance for your help. Here's my code:
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
public class ScrollingMenu extends JFrame {
private final JPopupMenu fontNameMenu = new JPopupMenu();
private final JMenu[] fontMenuArray = new JMenu[] {new JMenu("Font Name"), new JMenu("Font Style")};
private JMenu menu = new JMenu("Font");
private final JMenu[] helpMenuArray = new JMenu[] {new JMenu("Very very very very very very very long Font Name Menu")};
private JMenu hmenu = new JMenu("Help");
public static void main(String[] args) {
JFrame scrollingMenu = new ScrollingMenu();
scrollingMenu.setVisible(true);
}
public ScrollingMenu() {
// Creating a long array
String[] systemFontArray = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
JCheckBoxMenuItem[] fontNameArray = new JCheckBoxMenuItem[systemFontArray.length];
// Creating the scrollable menu
GridBagLayout fontNameLayout=new GridBagLayout();
JPanel fontNamePanel = new JPanel();
fontNamePanel.setLayout(fontNameLayout);
for (int i=0;i<systemFontArray.length;i++) {
fontNameArray[i] = new JCheckBoxMenuItem(systemFontArray[i]);
fontNameLayout.setConstraints(fontNameArray[i],new GridBagConstraints(0,i,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0));
fontNamePanel.add(fontNameArray[i]);
}
JScrollPane fontScrollPane = new JScrollPane(fontNamePanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.fontNameMenu.add(fontScrollPane);
// Adding the scrollable menu to a submenu
addML(ScrollingMenu.this.fontMenuArray[0], ScrollingMenu.this.fontNameMenu, new Dimension(200, 200));
// Creating the Font Style submenu (for underlining problem 2)
this.fontMenuArray[1].add(new JCheckBoxMenuItem("Bold"));
this.fontMenuArray[1].add(new JCheckBoxMenuItem("Italic"));
this.fontMenuArray[1].add(new JCheckBoxMenuItem("Plain"));
// Adding the submenus to the Font menu
for (int i=0;i<this.fontMenuArray.length;i++) {
this.menu.add(this.fontMenuArray[i]);
}
// Adding the scrollable menu to a long text submenu (problem 3)
addML(ScrollingMenu.this.helpMenuArray[0], ScrollingMenu.this.fontNameMenu, new Dimension(200, 200));
// Adding the submenu to the Help menu
for (int i=0;i<this.helpMenuArray.length;i++) {
this.hmenu.add(this.helpMenuArray[i]);
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(this.menu);
menuBar.add(this.hmenu);
this.setJMenuBar(menuBar);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
}
private static void addML (JMenu subMenu, JPopupMenu popupMenu, Dimension dimension) {
popupMenu.addFocusListener(new FocusAdapter() {
#Override
public void focusLost(FocusEvent focusEvent) {
if(popupMenu.isVisible() && !subMenu.isSelected())
popupMenu.setVisible(false);
}
});
popupMenu.addMouseListener(new MouseAdapter() {
#Override
public void mouseExited(MouseEvent mouseEvent) {
if(!subMenu.isSelected())
popupMenu.setVisible(false);
}
});
popupMenu.setInvoker(subMenu);
popupMenu.setPreferredSize(dimension);
subMenu.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent mouseEvent) {
if(!popupMenu.isVisible())
displayFontNameMenu(subMenu, popupMenu);
else
popupMenu.setVisible(false);
}
#Override
public void mouseEntered(MouseEvent mouseEvent) {
if(!popupMenu.isVisible() && subMenu.isSelected())
displayFontNameMenu(subMenu, popupMenu);
}
#Override
public void mouseExited(MouseEvent mouseEvent) {
if(popupMenu.isVisible() && !popupMenu.contains(mouseEvent.getPoint())) {
popupMenu.setVisible(false);
}
}
});
}
private static void displayFontNameMenu(JMenu subMenu, JPopupMenu popupMenu) {
Rectangle rectangle = subMenu.getBounds();
Point point = new Point(rectangle.x +rectangle.width, rectangle.y);
SwingUtilities.convertPointToScreen(point, subMenu.getParent());
popupMenu.setLocation(point.x, point.y);
popupMenu.setVisible(true);
}
}

handling events in java swing

How can I assign two buttons to share the same class for handling events in Java/swing?
For example, I have this:
private class BtnEvtHandler implements ActionListener {
private int counter=10;
public void actionPerformed(ActionEvent e) {
gs.setX(counter);
gs.repaint();
counter=counter+10;
}
public void actionPerformed(ActionEvent e) {
//action for move button
}
}
JButton jumpBtn= new JButton("JUMP");
BtnEvtHandler okButtonHandler= new BtnEvtHandler();
(jumpBtn).addActionListener(okButtonHandler);
menuPanel.add(jumpBtn);
Now I want to add another button as below which can have the same class as event handler but dispatches to different actionPerformed as mentioned in above code.
JButton moveBtn= new JButton("MOVE");
menuPanel.add(moveBtn);
(moveBtn).addActionListener(okButtonHandler);
You can't reuse one ActionListener and expect it to call a different method depending on the button you attach it to. The contract of ActionListener has one method that gets called. But you can check the source of the event and have flow control based on that. Here's an example:
package com.sandbox;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class SwingSandbox {
public static void main(String[] args) throws IOException {
JFrame frame = buildFrame();
JPanel pane = new JPanel();
MyActionListener myActionListener = new MyActionListener();
JButton button1 = new JButton("Button1");
button1.addActionListener(myActionListener);
pane.add(button1);
JButton button2 = new JButton("Button2");
button2.addActionListener(myActionListener);
pane.add(button2);
frame.add(pane);
}
private static JFrame buildFrame() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
return frame;
}
private static class MyActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
if ("Button1".equals(source.getText())) {
System.out.println("You clicked button 1");
} else {
System.out.println("You clicked button 2");
}
}
}
}

How to add MouseListener to JList model

I have simple sample of code. How can I to add Mouse Listener for displaying simple menu(when i clicked on right mouse button) for selected JList item? In addition: how can I to add Mouse Listener for left mouse button?
Like this:
Here is my code:
package test;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class Test implements ActionListener {
private JList list;
private MyJListModel model;
public Test() {
JFrame frame = new JFrame("Test");
frame.setMinimumSize(new Dimension(400,200));
JMenuBar menubar = new JMenuBar();
JMenu filemenu = new JMenu("File");
JMenuItem addItem = new JMenuItem("Add");
filemenu.add(addItem);
menubar.add(filemenu);
addItem.addActionListener(this);
model = new MyJListModel();
list = new JList(model);
JScrollPane scrollpane = new JScrollPane(list);
frame.add(scrollpane);
frame.setJMenuBar(menubar);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
String pushedItem = event.getActionCommand();
if(pushedItem.equals("Add")) {
model.addElement("Item");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}
}
class MyJListModel extends AbstractListModel {
private ArrayList<String> arraylist = new ArrayList<String>();
public MyJListModel() {
super();
}
#Override
public int getSize() {
return arraylist.size();
}
#Override
public Object getElementAt(int i) {
return arraylist.get(i);
}
public void addElement(String string) {
arraylist.add(string);
this.fireContentsChanged(this, 0, arraylist.size() - 1);
}
}
Heres a great article tutorial i found that helped me to accomplish what your trying to accomplish.
http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html#popup
i also found that link from this stack overflow article
How do I create a right click context menu in Java Swing?
Hope this helps
You add the mouselistener to the table, not the model.
Table.addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
if(e.isPopupTrigger())
{
onShowPopup(e);
}
}
public void mousePressed(MouseEvent e)
{
if(e.isPopupTrigger())
{
onShowPopup(e);
}
}
});

Changing the Panels of a JFrame "disable" the inputMap of the new Panel inserted

Description of the problem: I have a JFrame, inside this JFrame there is a JPanel with a button, when I press the button an action listener change the current JPanel with a new JPanel, which contains other two JPanels, those two have an inputMap that when the user press the key "up" make something on both of them. The problem is: when I change the JPanel with the new one the "up" key won't do anything.
Here is the code: is a SSCCE, so you just have to copy and paste to see what it does.
This modified code comes from another question that I "solved" sometimes ago. How to make two JPanels listen to the same event?
(the code is in the answer that I selected).
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class TwoPanelsTest extends JFrame {
private static MyPanel one = new MyPanel("One");
private static MyPanel two = new MyPanel("Two");
private static List<MyPanel> list = Arrays.asList(one, two);
private PanelsController panelsController;
public TwoPanelsTest() {
super("TwoPanelsTest");
panelsController= new PanelsController(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private static class MyPanel extends JPanel {
private String string = " will be updated though its action.";
private Action action = new UpdateAction(this);
private String name;
private JLabel label;
public MyPanel(String name) {
this.name = name;
this.label = new JLabel(name + string, JLabel.CENTER);
this.setLayout(new GridLayout());
this.setFocusable(true);
this.add(label);
}
public Action getAction() {
return action;
}
private void update() {
label.setText(name + ": " + System.nanoTime());
}
private static class UpdateAction extends AbstractAction {
private MyPanel panel;
public UpdateAction(MyPanel panel) {
this.panel = panel;
}
#Override
public void actionPerformed(ActionEvent ae) {
panel.update();
}
}
}//MyPanel
private static class ButtonPanel extends JPanel{
private JButton button ;
private PanelsController panelsController;
public ButtonPanel(PanelsController panelsController){
this.panelsController=panelsController;
button = new JButton("Button");
button.setActionCommand("buttonPressed");
button.addActionListener(this.panelsController);
this.setFocusable(true);
add(button);
}
}//ButtonPanel
private static class PanelsController implements ActionListener {
private TwoPanelsTest twoPanelsTest;
public PanelsController(TwoPanelsTest twoPanelsTest){
this.twoPanelsTest=twoPanelsTest;
this.twoPanelsTest.getContentPane().add(new ButtonPanel(this));
}
#Override
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("buttonPressed")){
twoPanelsTest.getContentPane().removeAll();
twoPanelsTest.getContentPane().invalidate();
JPanel panel = new JPanel(new GridLayout(0, 1, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(one);
panel.add(two);
panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
panel.getActionMap().put("up", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
for (MyPanel panel : list) {
panel.getAction().actionPerformed(e);
}
}
});
twoPanelsTest.getContentPane().add(panel);
twoPanelsTest.validate();
twoPanelsTest.repaint();
}
}//ActionPerformed
}//PanelsController
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
TwoPanelsTest t = new TwoPanelsTest();
}
});
}
}
Well, its pretty simple - if you have those two panels without any components inside and want them to listen the hotkey use:
panel.getInputMap ( JComponent.WHEN_IN_FOCUSED_WINDOW )
.put ( KeyStroke.getKeyStroke ( KeyEvent.VK_UP, 0 ), "up" );
JComponent.WHEN_IN_FOCUSED_WINDOW instead of JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT.
Otherwise you need to have somthing focused inside the panel so it could catch the key events.
By the way, there is also another way to listen global hotkeys inside Java application windows:
Toolkit.getDefaultToolkit ().addAWTEventListener ( new AWTEventListener ()
{
public void eventDispatched ( AWTEvent event )
{
// All application key events will be passed here
}
}, AWTEvent.KEY_EVENT_MASK );

Categories

Resources