Java, menuBar, JMenuitems Focus - java

I have created JMenuItems with Key Accelerators, then i have added them to the menubar directly without the need to be added to the JMenu so they look like JButtons, every thing work just fine, but i noticed that JMenuItem never get focus when clicked or key pressed, that make some problems to me for example:
One of the JMenuItems is for save, also i have one JTextField which do some validation when losing focus, but that not working since when pressing the Save, the focus kept there on the JTextField.
Any Ideas ?!

I'd suggest using a JToolBar and taking advantage of the Action API
Here's a really short example of how you might be able to achieve accelerator support for toolbar (or any) button.
public class MenuTest {
public static void main(String[] args) {
new MenuTest();
}
public MenuTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JToolBar tb = new JToolBar();
tb.add(new FastButton(new OpenAction()));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(tb, BorderLayout.NORTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class FastButton extends JButton {
private FastButton(Action action) {
super(action);
setHideActionText(true);
}
#Override
protected void configurePropertiesFromAction(Action a) {
super.configurePropertiesFromAction(a);
if (a != null) {
KeyStroke ks = (KeyStroke) a.getValue(Action.ACCELERATOR_KEY);
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
inputMap.put(ks, "Action.accelerator");
actionMap.put("Action.accelerator", a);
}
}
}
public class OpenAction extends AbstractAction {
public OpenAction() {
putValue(NAME, "Open");
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
putValue(SMALL_ICON, new ImageIcon(getClass().getResource("/folder_document.png")));
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Open");
}
}
}

Related

How to dynamically create JPanel with given parameters?

I have main JFrame in my project. And one main JPanel with Y-AXIS BoxLayout which is used to contain another panels in it. This is the way i use my JFrame to show this JPanel by default (I'm not quite convinced if this is the right way):
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
mainPanel = new MainScreenPanel();
MainFrame mainFrame = new MainFrame();
mainFrame.setContentPane(mainPanel);
mainFrame.invalidate();
mainFrame.validate();
mainFrame.setVisible(true);
}
});
}
Next I add two JPanels into mainPanel like this:
public class MainScreenPanel extends javax.swing.JPanel {
public MainScreenPanel() {
StatusPanel sPanel = new StatusPanel();
LogPanel lPanel = new LogPanel();
add(sPanel);
add(lPanel);
}
}
lPanel has different gui elements on it. One of them is a button which opens another panel (addConnectionPanel), and replaces mainPanel in the jFrame Here is the way i do it:
private void addCnctButtonActionPerformed(java.awt.event.ActionEvent evt) {
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
topFrame.setContentPane(new AddConnectionPanel());
topFrame.invalidate();
topFrame.validate();
}
AddConectionPanel has some labels and input text boxes. It has two buttons ok and cancel. Here is the code of cancel button:
private void cancelCnctBtnActionPerformed(java.awt.event.ActionEvent evt) {
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
topFrame.setContentPane(new MainScreenPanel());
topFrame.invalidate();
topFrame.validate();
}
sPanel is empty. It must be empty until input boxes on AddConnectionPanel are not filled and 'ok' button is not pressed. When these actions are performed, I want to dynamically create JLabels which take parameters from inputs on sPanel. Labels should be grouped, so when the actions performed second time new group must be created. Can some one give me advice on how to do this? And show me my mistakes? Keep in mind I'm using NetBeans.
This would be my approach:
public interface ConnectionPanelListener{
void onOkButtonClicked(String... options);
void onCancelButtonClicked();
}
public class AddConnectionPanel extends JPanel {
private static final long serialVersionUID = 1L;
private ConnectionPanelListener listener;
public AddConnectionPanel(){
final Map<ConnectionOptions, JTextField> components = new HashMap<>(ConnectionOptions.values().length);
for(ConnectionOptions option:ConnectionOptions.values()){
this.add(new JLabel(option.labelCaption));
JTextField textField = new JTextField();
//setup textField;
this.add(textField);
components.put(option, textField);
}
JButton button = new JButton("OK");
button.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(final MouseEvent pE) {
super.mouseClicked(pE);
//TODO validate TextFields
Collection<String> inputs = new Stack<>();
for(Entry<?,JTextField> e : components.entrySet()){
String text = e.getValue().getText();
if(text==null || text.trim().isEmpty()){
//TODO improve input validation
System.out.println("Input text is empty for: "+e.getKey());
} else {
inputs.add(e.getKey() + ": " + text);
}
}
listener.onOkButtonClicked(inputs.toArray(new String[inputs.size()]));
}
});
this.add(button);
button = new JButton("cancel");
button.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(final MouseEvent pE) {
super.mouseClicked(pE);
listener.onCancelButtonClicked();
}
});
this.add(button);
}
public void setConnectionPanelListener(final ConnectionPanelListener l){
listener = l;
}
private enum ConnectionOptions{
IP_ADDRESS("IP-Address:"), PORT("Port:"), WHATEVER_ATTRIBUTE_YOU_NEED("Extras:");
private String labelCaption;
private ConnectionOptions(final String caption) {
labelCaption = caption;
}
}
}
As you can see, AddConnectionPanel expects a Listener to register for the case, that "OK" or "CANCEL" are clicked. So your adjusted implementation could be like:
private void addCnctButtonActionPerformed(java.awt.event.ActionEvent evt) {
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
AddConnectionPanel panel = new AddConnectionPanel();
panel.setConnectionPanelListener(new ConnectionPanelListener(){
#Override
void onOkButtonClicked(String... options){ TODO: fill sPanel using the given Strings }
#Override
void onCancelButtonClicked(){ TODO }
});
topFrame.setContentPane(panel);
topFrame.invalidate();
topFrame.validate();
}

Closing a JDialog by hitting the "enter" on keyboard

I want to close my JDialog by hitting the "enter" key on my keyboard. how can I do that? thank you!
NOTE:
I want to do this, without any button involved.
THank you!
One way:
You could give it a close JButton
whose ActionListener has code that closes the dialog,
And make that button the default button for the dialog's rootpane.
e.g.,
myDialog.getRootPane().setDefaultButton(exitButton);
Option two:
Use Key Bindings to bind the enter key to exit code in an AbstractAction.
e.g.,
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class DemoDialog {
public static void main(String[] args) {
JFrame frame = new JFrame("Frame");
frame.add(Box.createRigidArea(new Dimension(400, 300)));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
final JDialog dialog = new JDialog(frame, "Dialog", true);
// set binding
int condition = JPanel.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = ((JPanel) dialog.getContentPane()).getInputMap(condition);
ActionMap actionMap = ((JPanel) dialog.getContentPane()).getActionMap();
String enter = "enter";
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), enter);
actionMap.put(enter, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});
dialog.add(Box.createRigidArea(new Dimension(200, 200)));
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
}
I would like to say first that 'Hovercraft Full Of Eels' solution is more elegant than this one and more closely in the spirit of the JDialog and Swing API. However, to offer an alternative here is a basic example of using a KeyListener on your JDialog that will do as you need without adding a button;
public class Test {
public static void main(String[] args) {
JDialog jd = new JDialog();
// Add and define the KeyListener here!
jd.addKeyListener(new KeyListener(){
#Override
public void keyTyped(KeyEvent e) {
// Nothing
}
#Override
public void keyPressed(KeyEvent e) {
// Nothing
}
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
JDialog d = (JDialog)e.getSource();
d.dispose();
}
}
});
// End key listener code
jd.setVisible(true);
}
}
The important/relevant code is between the two main comments. This is a compilable example, so you can copy paste this into a new file and run it to view the effects.

opening only one window on actionPerformed

I've looked through topics on how to open only one window when a button is clicked but none of the solutions there helped, perhaps because my code was structured a bit differently.
So I have a main window class extending JFrame and one of the buttons is supposed to open a new window when clicked. I have defined the widgets/panels etc for the new window in a separate class. At the moment, every time I click on the button a new window is opened. I want to make it so that if a window is already opened then it would switch to that window once the button is clicked again.
Here is a bit of my code:
public class MainWindow extends JFrame{
/*
* create widgets and panels
*/
Button.addActionListener(new ActionListener() { // the button that opens
//a new window
#Override
public void actionPerformed(ActionEvent e) {
Window2 ww = new Window2(); //creating the new window here
}
});
}
NB. The Window2 class is also extending JFrame, if that's of any help.
Thanks
pull out ojbect creation from actionPerformed method beacuse each time you click button it's create new object. below can help you :-
Make a Window2 class singalton for more detail about singalton click here.
2 . add null check as below :-
....
Window2 ww = null; // static or instence variable
......
#Override
public void actionPerformed(ActionEvent e) {
if(ww==null)
{
ww = new Window2();
ww.someMethod();
}
else
{
ww.someMethod();
}
}
});
Here is a full working example:
Window2.java
public class Window2 extends JFrame {
private static final long serialVersionUID = 7843480295403205677L;
}
MainWindow.java
public class MainWindow extends JFrame {
private static final long serialVersionUID = -9170930657273608379L;
public static void main(String[] args) {
MainWindow mw = new MainWindow();
mw.go();
}
private void go() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
private void createAndShowGUI() {
JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
private Window2 ww = null;
#Override
public void actionPerformed(ActionEvent e) {
if (ww==null) {
ww = new Window2(); //creating the new window here
ww.setDefaultCloseOperation(HIDE_ON_CLOSE);
ww.setTitle("Window2 created on " + new Date());
ww.setSize(500, 200);
}
pack();
ww.setVisible(true);
}
});
setLayout(new BorderLayout());
add(button);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
}
What you can try is make two windows and put the actionPeformed method in the main class so that when the button is pressed it displays the second window

Change JLabel text from another class

I have the following class that draws a Label. (I have only given part of the code here). Everyhting works fine, the label gets displayed.
Now, i have another class called Caller Class. I have a method in that where i will use to change the value of this label. how can i do that
public class MyClass{
private JLabel label;
MyClass(){
run();
}
public void editTheLabelsValue (String text) {
label.setText(text);
frame.repaint();
}
run(){
.... // there were more code here, i removed it as it's not relevant to the problem
label = new JLabel("Whooo");
label.setBounds(0, 0, 50, 100);
frame.getContentPane().add(label);
.....
}
later on, i will be using the following class to change the text of the above label. How can i do this.
public class Caller {
void methodA(){
MyClass mc = new MyClass();
mc.editTheLabelsValue("Hello");
}
}
1.) When the methodA() is executed, the text Hello is not getting displayed on the Label field. it still remains as Whooo. How can i correct this. I want the label text to be Hello once that method has been executed.
The immeditate problem I can see is to appears that you are either using a null layout or your don't understand how layout managers work.
The following code updates the label from the main class in a sub class via a setText method call. This method is called every second
public class PaintMyLabel {
private int counter = 0;
public static void main(String[] args) {
new PaintMyLabel();
}
public PaintMyLabel() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final MasterPane master = new MasterPane();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(master);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Timer timer = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
counter++;
master.setText("Now updated " + counter + " times");
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
});
}
public class MasterPane extends JPanel {
private JLabel label;
public MasterPane() {
label = new JLabel("Original text");
setLayout(new GridBagLayout());
add(label);
}
public void setText(String text) {
label.setText(text);
}
}
}
If you're using a null layout, then stop it. Just don't. There are only a very small number of times you would ever use a null layout and I suspect this isn't one of them.

Adding a clickable, action-firing JMenuItem directly to a JMenuBar?

Is there a way to add a JMenuItem (or similar button-type object) to a JMenuBar?
Adding a JMenuItem doesn't play well with the layout of a JMenuBar, and buttons look too button-like.
Should we be tweaking the button to look like a JMenuItem or tweaking the JMenuBar to display the JMenuItem correctly? Or something else altogether?
The following code implements camickr's solution, although I would have come up with the same thing after seeing the default way JMenuItems are rendered in a JMenuBar. It looks reasonably authentic and responds to clicks, but not to the mnemonic.
I tried giving the JMenuItems accelerators (see code) and that works but that looks really weird.
public class TheDude19 extends JFrame {
private class Action1 extends AbstractAction {
private Action1() {
super("Action1");
putValue(MNEMONIC_KEY, (int) '1');
// putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK));
}
public void actionPerformed(ActionEvent arg0) {
System.out.println("Action 1!");
}
}
private class Action2 extends AbstractAction {
private Action2() {
super("Action2");
putValue(MNEMONIC_KEY, (int) '2');
}
public void actionPerformed(ActionEvent arg0) {
System.out.println("Action 2!");
}
}
private class NarrowMenuItem extends JMenuItem {
public NarrowMenuItem(Action a) {
super(a);
}
public Dimension getMaximumSize() {
return new Dimension(super.getPreferredSize().width, super.getMaximumSize().height);
}
}
public TheDude19() {
JMenuItem menu1 = new NarrowMenuItem(new Action1());
JMenuItem menu2 = new NarrowMenuItem(new Action2());
JMenuBar mb = new JMenuBar();
mb.add(menu1);
mb.add(menu2);
add(mb, BorderLayout.NORTH);
setSize(400, 300);
}
public static void main(String[] args) {
(new TheDude19()).setVisible(true);
}
}
JMenuItem doesn't play well with the
layout of a JMenuBar
A menubar use a BoxLayout which will try to strech the component to its maximum size. Try using:
menuItem.setMaximumSize( menuItem.getPreferredSize() );
If you need more help post your SSCCE showing the problem.
Just tweak the JButton.
button= new JButton("MenuItem");
button.setOpaque(true);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
button.setFocusable(false);
button.addActionListener(new buttonHandler());
menuBar.add(button);
setContentAreaFilled makes it see-through,
setBorderPainted gets rid of the border,
setFocusable gets rid of the tiny border around the text.
Maybe you're forgetting your JMenu. You need to put the JMenuItem in a JMenu, then you add the JMenu to the JMenuBar.
To build a menu bar you need to do something like the following:
JMenuBar myBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem newFileMenuItem = new JMenuItem("New");
newFileMenuItem.addActionListener(new ActionListerner() { ... Define Action Handler here... });
fileMenu.add(newFileMenuItem);
myBar.add(fileMenu);
I had something like this happen recently I had a JMenuBar that only had 2 JMenuItems in (so note that I haven't tried this in a mixed JMenu and JMenuItem environment.
At first I ended up changing the Layout to a FlowLayout with a left alignment, but that left too much space in-between the components. I messed around with trying to do various things, but was very unsatisfied. What I ended up doing was just using a JMenu, but overriding some of it's behaviors so that it pretended to be a JMenuItem. Like so:
JMenuBar mainMenuBar = new JMenuBar();
final JMenu quitMenuItem = new JMenu("Quit");
quitMenuItem.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent e) {
System.exit(0);
}
public void menuDeselected(MenuEvent e) {}
public void menuCanceled(MenuEvent e) {}
});
quitMenuItem.setPopupMenuVisible(false);
final JMenu aboutMenuItem = new JMenu("About");
aboutMenuItem.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent e) {
JOptionPane.showMessageDialog(MainFrame.this, "Assignment 3 With Swing UI. Author: T.Byrne", "About", JOptionPane.INFORMATION_MESSAGE);
aboutMenuItem.setSelected(false);//otherwise it will still be selected after the dialog box.
}
public void menuDeselected(MenuEvent e) {}
public void menuCanceled(MenuEvent e) {}
});
aboutMenuItem.setPopupMenuVisible(false);
mainMenuBar.add(quitMenuItem);
mainMenuBar.add(aboutMenuItem);
this.setJMenuBar(mainMenuBar);
Yup. Or do it the easy way
mb.add(new JMenuItem(closeAction) {
public Dimension getMaximumSize() {
return new Dimension(
super.getPreferredSize().width,
super.getMaximumSize().height);
}
});
It creates a class file, but meh.
To get the button to look like a JMenu just add a rollover effect and remove the border of the button (See code below for example)
Required imports
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.Dimension;
import java.awt.Color;
Code
JButton tstButton = new JButton(); //Button
tstButton.setText("Test"); //Button Text
tstButton.setOpaque(false); //These remove the button filling and border
tstButton.setContentAreaFilled(false);
tstButton.setBorder(null);
tstButton.setFocusable(false);
tstButton.setRolloverEnabled(true); //Allows the button to detect when mouse is over it
tstButton.getModel().addChangeListener(new ChangeListener()
{
#Override
public void stateChanged(ChangeEvent e)
{
ButtonModel model = (ButtonModel) e.getSource();
if(model.isRollover())
{
tstButton.setBackground(Color.RED); //Changes the colour of the button
tstButton.setOpaque(true);
}
else
{
tstButton.setBackground(null);
tstButton.setOpaque(false);
}
}
});
Dimension dBt = new Dimension(75,25); //Sets the size of the button in the JMenuBar
tstButton.setMinimumSize(dBt);
tstButton.setPreferredSize(dBt);
tstButton.setMaximumSize(dBt);
tstButton.setMnemonic('T'); //Allows you to press Alt+T on your keyboard to press the button
tstButton.addActionListener(new ActionListener() //Adds action listener so it can do something
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Button pressed");
}
});
menuBar.add(tstButton); //Adds the button to the JMenuBar
Edit
There is a much improved way to do this
JButton tstButton = new JButton();
tstButton.setVisible(false);
tstButton.addActionListener(new CustomActionListener());
menuBar.add(tstButton);
JMenu menuButton = new JMenu();
addHotKey(menuButton, "shift C", 'm', "Menu Button","pressed");
menuButton.addMouseListener(new CustomMouseListener());
menuButton.addMenuKeyListener(new CustomKeyListener());
menuBar.add(menuButton);
public void addHotKey(JMenu J, String s, char c, String S, String key)
{
Action buttonAction = new AbstractAction(S)
{
#Override
public void actionPerformed(ActionEvent evt)
{
clcikComponent(tstButton);
}
};
J.setAction(buttonAction);
buttonAction.putValue(Action.MNEMONIC_KEY, KeyEvent.getExtendedKeyCodeForChar(c));
J.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(s), key);
J.getActionMap().put(key, buttonAction);
}
class CustomMouseListener implements MouseListener
{
public void mouseClicked(MouseEvent e)
{
clcikComponent(m_Invisible);
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class CustomKeyListener implements MenuKeyListener
{
#Override
public void menuKeyTyped(MenuKeyEvent e)
{
char c = e.getKeyChar();
if(c==KeyEvent.VK_ENTER)
{
if(m_code.isSelected())
{
clcikComponent(m_Invisible);
}
}
}
#Override
public void menuKeyPressed(MenuKeyEvent e){}
#Override
public void menuKeyReleased(MenuKeyEvent e){}
}
public void clcikComponent(JButton comp)
{
comp.doClick();
}
class CustomActionListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e)
{
//Makes Button Perform Action
}
}
Getting the UI and the code both to look good took a while. We ended up attaching a mouse adapter to the JMenu's underlying component:
JMenu selectData = new JMenu("Select data...");
selectData.getComponent().addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
// Our code to open a window for choosing data series here...
// [...]
}
});
menuBar.add(selectData);
I guess we'll add a KeyAdapter as well, but haven't yet.

Categories