I've created a menubar and added a menu with several items. When I do doClick() on the JMenu(archiveMenu) it highlights the button for the menu but it doesnt show any of the items that are added to it. I've tried doing doClick() before the adding of actionListeners to the items and setVisible(true) but nothing works.
I think it has something to do with the fact that they have seperate actionListeners but I'm so far in to the program that changing to a single actionListener would lead to a huge amount of work. I appreciate any help, thanks!
It looks like this:
// MENU BAR
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu archiveMenu = new JMenu("Archive");
menuBar.add(archiveMenu);
JMenuItem newItem = new JMenuItem("New Map");
JMenuItem loadItem = new JMenuItem("Load places");
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem exitItem = new JMenuItem("Exit");
newItem.addActionListener(new NewMapLis());
loadItem.addActionListener(new LoadLis());
saveItem.addActionListener(new SaveLis());
exitItem.addActionListener(new ExitLis());
archiveMenu.add(newItem);
archiveMenu.add(loadItem);
archiveMenu.add(saveItem);
archiveMenu.add(exitItem);
archiveMenu.doClick();
What is happening is that when doClick() is called the window is not fully loaded yet.
you need to perform it after loading.
for example, you could call it when the frame is opened:
myFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowOpened(WindowEvent e) {
archiveMenu.doClick();
}
});
Related
As in title, I struggle to get my JMenu to resize when programatically adding components. In my application I need JMenu with JCheckBoxes. Whenever I tick one of them, JSlider should appear just below. Below is the code that makes that happen. The problem I have is that when there's lot of sliders visible, items in JMenu get clumped - see image below.
How can I force redraw/resize/expansion of JMenu to perserve original checkboxes/sliders height?
Also note - JMenu stays visible at all times when selecting checkboxes. It closes only when I click outside of it. But after such 'restart' menu grows and problem is no longer present.
Many thanks in advance!
public class Window extends JFrame implements ActionListener {
private JPanel panel;
private JMenuBar menuBar;
private JMenu menu;
private JSlider slider1;
private JCheckBoxMenuItem checkBox1;
private JCheckBoxMenuItem checkBox2;
private JCheckBoxMenuItem checkBox3;
public Window() {
super("Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setMinimumSize(new Dimension(300,300));
panel = new JPanel();
panel.setMinimumSize(new Dimension(300,300));
menuBar = new JMenuBar();
menu = new JMenu("Options");
checkBox1 = new JCheckBoxMenuItem("option 1");
checkBox2 = new JCheckBoxMenuItem("option 2");
checkBox3 = new JCheckBoxMenuItem("option 3");
checkBox1.addActionListener(this);
//prevent JMenu from closing after selecting CheckBox
checkBox1.setUI(new BasicCheckBoxMenuItemUI() {
#Override
protected void doClick(MenuSelectionManager msm) {
checkBox1.doClick(0);
}
});
slider1 = new JSlider();
slider1.setVisible(false);
menu.add(checkBox1);
menu.add(slider1);
menu.add(checkBox2);
menu.add(checkBox3);
menuBar.add(menu);
setJMenuBar(menuBar);
add(panel);
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(checkBox1)) {
slider1.setVisible(checkBox1.isSelected());
}
}
}
Here's screen from my main application:
Hiding and reshowing the popup menu will cause it to be resized:
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(checkBox1)) {
slider1.setVisible(checkBox1.isSelected());
menu.setPopupMenuVisible(false);
menu.setPopupMenuVisible(true);
}
}
Call revalidate() it is supposed to do the trick, call it on the newly added components. https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Component.html#revalidate()
But i would say a use case like this is not what menus are meant for? why not use a dialog for dynamic components?
For background https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Component.html#revalidate() call it o the newly added component and see https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Component.html#invalidate() and https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Container.html#isValidateRoot()
I'm working on an application using Swing in Java, however, I have a weird problem in the display, my code works perfectly fine, but the output is weird. When I run the program. The JFrame looks empty or something is missing (JMenuBar, JMenuItem, etc are invisible), then I maximize the screen, and all other stuff becomes visible, then I minimize the screen and it looks visible. I'm pretty sure that the code works fine, it's just a display problem. Can anyone help so that the first display looks fine?
Here is the code
JFrame frame = new JFrame("Menu");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu help = new JMenu("Help");
menubar.add(help);
JMenuItem about = new JMenuItem("About");
help.add(about);
class exitaction implements ActionListener {
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
}
exit.addActionListener(new exitaction());
}
Put the code below at the end. It'll work fine.
The method setVisible is an action, just like show() before JDK 1.5.
frame.setVisible(true);
My program has a menu bar with one menu one it called "File." Inside the file menu there are 4 options: "New", "Pause", "Unpause", and "Exit". All five of these have mnemonics assigned however only the one for File works the way I had hoped.
The four others work, but they only work if I activate the mnemonic for File first. i.e. To activate "New" I need to press Alt+F, the Alt+N. I didn't think mnemonics were suppose to work that way, but I could be mistaken.
This is the code I currently have, maybe someone can point out what I'm doing wrong.
//MENU BAR
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem newGame;
private JMenuItem pauseGame;
private JMenuItem unpauseGame;
private JMenuItem exitGame;
//CREATE THE FILE MENU
public void buildMenuBar(){
//INITIAILIZE
menuBar = new JMenuBar();
//BUILD FILE MENU
buildFileMenu();
//ADD TO MENU BAR
menuBar.add(fileMenu);
//SET
setJMenuBar(menuBar);
}
public void buildFileMenu(){
//INITIALIZE
fileMenu = new JMenu("File");
newGame = new JMenuItem("New");
pauseGame = new JMenuItem("Pause");
unpauseGame = new JMenuItem("Unpause");
exitGame = new JMenuItem("Exit");
//MNEMONICS
fileMenu.setMnemonic(KeyEvent.VK_F);
newGame.setMnemonic(KeyEvent.VK_N);
pauseGame.setMnemonic(KeyEvent.VK_P);
unpauseGame.setMnemonic(KeyEvent.VK_U);
exitGame.setMnemonic(KeyEvent.VK_X);
//LISTENERS
newGame.addActionListener(new MenuListener());
exitGame.addActionListener(new MenuListener());
//ADD TO FILEMENU
fileMenu.add(newGame);
fileMenu.add(pauseGame);
fileMenu.add(unpauseGame);
fileMenu.add(exitGame);
}
So I'm going to answer my own question. I've learned that mnemonics like I was trying to use only work when the menu is active. That's why they worked for the "File" option but not the "New Game" option unless the file option was already open. It was working right, just not how I understood it to work.
I'm practicing creating a GUI program in Java using swing and awt imports. Most everything in my main class is working, except for getting a drop-down menu with a tab named file at the top of the GUI. I have two snippets of code, one in the JPanel class and the other in the public main() class. I ultimately want to get a file menu with save and save as options at the top. Don't need to invoke anything or add listeners, just to make them visible on the program itself. I'm using JMenuBar menuBar along with JMenu fileMenu to create it. What am I doing wrong? Snippets below:
JMenuBar menuBar = new JMenuBar();
JMenuItem saveItem, saveAllItem;
JMenuItem menuItem = new JMenuItem("Save");
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
saveItem = fileMenu.add("Save");
saveAllItem = fileMenu.add("Save All");
panel.add(menuItem);
setVisible(true);
There is no any need of adding JMenuBar object to JPanel because it is only linked with the JFrame. You need to pass the JMenuBar object to JFrame method setJMenuBar() in order to set the menu bar in the window.You can create a drop down menu for JFrame by using this code:
JMenuBar menuBar = new JMenuBar();
JMenuItem saveItem, saveAllItem;
// Menu
JMenu fileMenu = new JMenu("File");
// Menu Item (Drop down menus)
saveItem = new JMenuItem("Save");
saveAllItem = new JMenuItem("Save All");
// Adding menu items to menu
fileMenu.add(saveItem);
fileMenu.add(saveAllItem);
// adding menu to menu bar
menuBar.add(fileMenu);
// setting menubar at top of the window.
// if you create a object of JFrame in class then code to set JMenuBar to JFrame will be:
// jframe.setJMenuBar(menuBar);
// if class is extending JFrame then it will be like this:
setJMenuBar(menuBar);
The code is here:
I don't think the System.exit(); is working properly.
Am I using the wrong method or what?
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnRWindow = new JMenu("RWindow");
menuBar.add(mnRWindow);
JMenuItem mntmMore = new JMenuItem("More");
mnRWindow.add(mntmMore);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.exit(DISPOSE_ON_CLOSE);
}
});
mnRWindow.add(mntmExit);
Don't use a MouseListener with a JMenuItem.
A JMenuItem is designed to be used with an ActionListener. Read the section from the swing tutorial on How to Use Menus for more information and working examples.
Also, DISPOSE_ON_CLOSE is NOT a value you should be using for the System.exit(...) method. That variable should only be used with the setDefaultCloseOperation(...) method of the frame. Just use a "0" for the value.