I am trying to get my JMenuBar to display in the GUI, however it just appears as a 1-pixel line at the top.
This is my code...
public LibraryView() {
setBounds(100,100,640,480);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JMenuBar b1 = new JMenuBar();
JMenu m1 = new JMenu("Test");
JMenuItem i1 = new JMenuItem("Item1");
this.setJMenuBar(b1);
}
Could someone please help me to understand what is wrong.
You need to add the JMenu and JMenuItem to the JMenuBar. You also need to pack() and setVisible(true); at the end of the method, just before the GUI is shown...
public LibraryView() {
setBounds(100,100,640,480);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JMenuBar b1 = new JMenuBar();
JMenu m1 = new JMenu("Test");
JMenuItem i1 = new JMenuItem("Item1");
m1.add(i1); // ADDED
b1.add(m1); // ADDED
this.setJMenuBar(b1);
pack(); // ADDED
setVisible(true); // MOVED
}
You should call setVisible(true) only after adding all components to the top level window. Also don't forget to use layout managers, to let these managers and your component's preferredSize set the sizes of components, and don't forget to call pack().
call repaint() right before you call set visible(true) this NEVER FAILS.
setJMenuBar(menuBar);
menuBar.add(jMenuItem);
repaint(); //then
setVisible(true); //Assured NEVER FAILS
Related
I have a JMenu A within a JMenuBar aligned to the right of the screen. Within this menu are several JMenuItems, along with another JMenu B. Since menu A is right aligned, I need menu B to open to the left. In order to do this I found the code
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);.
The issue however, is that this messes with the text alignment of menu 2, which I would like to stay exactly like the other menu items.
I have also tried manually aligning menu 2 using SwngConstants.leftAlign, however this is too severe:
How can I make the menu expand to the left, while retaining the default text alignment? Thanks in advance! The code I used to produce the above images is seen below:
import java.awt.*;
import javax.swing.*;
public class Test{
public Test(){
JFrame frame = new JFrame();
JMenuBar menuBar = new JMenuBar();
JMenu menu1 = new JMenu("Menu 1");
JMenu menu2 = new JMenu("Menu 2");
JMenuItem menuitem1 = new JMenuItem("Menu Item 1");
JMenuItem menuitem2 = new JMenuItem("Menu Item 2");
JMenuItem menuitem3 = new JMenuItem("Menu Item 3");
JMenuItem menuitem4 = new JMenuItem("Menu Item 4");
menuBar.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
menu1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
menu2.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
menu2.setHorizontalAlignment(SwingConstants.LEFT);
menuBar.add(menu1);
menu1.add(menuitem1);
menu1.add(menuitem2);
menu1.add(menu2);
menu2.add(menuitem3);
menu2.add(menuitem4);
frame.setJMenuBar(menuBar);
frame.getContentPane().setBackground(Color.WHITE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(270,170));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
UIManager.getLookAndFeelDefaults().put("Label.font", new Font("Arial", Font.PLAIN, 12));
UIManager.getLookAndFeelDefaults().put("Button.font", new Font("Arial", Font.PLAIN, 12));
UIManager.getLookAndFeelDefaults().put("ComboBox.font", new Font("Arial", Font.PLAIN, 12));
UIManager.getLookAndFeelDefaults().put("JTextField.font", new Font("Arial", Font.PLAIN, 12));
new Test();
} catch(Exception e){
JOptionPane.showConfirmDialog(null, e.getMessage());
}
}
}
UPDATE: If I remove the lines of code aligning the menus right to left and move the frame to the edge of the display screen, then the menus act as desired (i.e. the menu will not expand off the monitor). Perhaps there is a way to tell the menu not to expand off the JFrame rather than the monitor?
UPDATE: Thank you #StanislavL for the idea. Overriding menu 2 with the following code does the trick, it also gets rid of that unsightly overlap between the two menus. Note that menu 2 no longer needs ComponentOrientation.RIGHT_TO_LEFT.
JMenu menu2 = new JMenu("Menu 2"){
protected Point getPopupMenuOrigin(){
return new Point(-this.getPopupMenu().getPreferredSize().width, 0);
}
};
You can try to override
public ComponentOrientation getComponentOrientation()
method of JMenu to return your alignmnet
or JMenu has method you can override
protected Point getPopupMenuOrigin()
Not getting any errors however the program only displays the word "menu" at the top in the program. It doesn't display the 3 JMenu items: "home", "about" and "explore".
JPanel p5 = new JPanel(new GridBagLayout());
p5.setVisible(true);
fw.add(p5, BorderLayout.PAGE_START);
JMenu menu = new JMenu("Menu");
menu.setVisible(true);
menu.add("home");
menu.add("about");
menu.add("explore");
JMenuBar menubar = new JMenuBar();
fw.setJMenuBar(menubar); //ADDED THIS LINE. STILL DOESN'T WORK.
menubar.setVisible(true);
menubar.add(menu);
p5.add(menu);
I've added JMenu to JMenuBar (everything JMenu, JMenubar and JPanel is set to visible). Also I added JPanel (p5) to "first window (fw) and added menu to p5. I'm not sure why my menu items are not being displayed.
UPDATE: MCVE (Minimal Complete and Verifiable Example) as requested.
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
public class TestingClass extends JFrame {
public static void main(String[] args) {
FirstWindow fw = new FirstWindow();
fw.setSize(400, 600);
fw.setDefaultCloseOperation(EXIT_ON_CLOSE);
fw.setVisible(true);
JPanel p5 = new JPanel(new GridBagLayout());
p5.setVisible(true);
fw.add(p5);
JMenu menu = new JMenu("Menu");
menu.setVisible(true);
menu.add("home");
menu.add("about");
menu.add("explore");
JMenuBar menubar = new JMenuBar();
fw.setJMenuBar(menubar); // THE UPDATED LINE OF CODE.
menubar.setVisible(true);
menubar.add(menu);
p5.add(menu);
}
}
As you run the program, you will see the words "menu" displayed. The items: "home, about and explore" from JMenu are not displayed. Does anybody know what I'm doing wrong?
An MCVE of a run-time problem should compile cleanly. That shows 3 compilation errors. One is a missing import (easily fixable), but the other two relate to the missing FirstWindow.
Nevertheless, once a few tweaks were made, the problem becomes clear. A component can only appear in one place. By adding it to the panel as well (commented out below), it cannot appear in the menu.
import java.awt.*;
import javax.swing.*;
public class TestingClass extends JFrame {
public static void main(String[] args) {
JFrame fw = new JFrame();
fw.setSize(400, 200); // for screenshot
fw.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p5 = new JPanel(new GridBagLayout());
p5.setVisible(true);
fw.add(p5);
JMenu menu = new JMenu("Menu");
//menu.setVisible(true);
menu.add("home");
menu.add("about");
menu.add("explore");
JMenuBar menubar = new JMenuBar();
fw.setJMenuBar(menubar); // THE UPDATED LINE OF CODE.
//menubar.setVisible(true);
menubar.add(menu);
//p5.add(menu); // WTF?
fw.setVisible(true); //should be done after all components are added
}
}
You need to add the menuBar to the frame:
frame.setJMenuBar( menuBar );
Also, you don't need do make Swing components visible since they are visible by default (except for top level contains, like JFrame which you do need to set visible).
You need to call setVisible() after adding component! So first add all components. Add the highest level component to the JFrame(JPanel in your case) and the only call setVisible() for the JFrame. No need to call on every component.
JMenu menu = new JMenu("Menu");
menu.add("home");
menu.add("about");
menu.add("explore");
//rest of the components
//add panelto the frame
frame.getContentPane.add(p5);
//set menubar for the frame
frame.setJMenuBar( menuBar );
//set visibility for frame to true
frame.setVisible(true);
I'm training with Java and Swing, in Windows the GUI is shown correctly, instead on MAC there are several problems.
The JMenuBar is shown in the window, while should be shown in the MAC bar [finder].
Code of JMenuBar:
JMenuBar menuBar = new JMenuBar();
preferenceItem.addMouseListener(this);
printItem.addMouseListener(this);
menuBar.add(fileMenu);
fileMenu.add(openItem);
fileMenu.add(printItem);
fileMenu.add(exportItem);
menuBar.add(optionMenu);
optionMenu.add(preferenceItem);
setJMenuBar(menuBar);
Than another problem is how two panels are shown. As you can see in the previous image backup button, which is in the second panel is shown above the first panel.
public OptionDialog(){
super();
setTitle(NOME_APPLICAZIONE);
Dimension dimensioni = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((int)(dimensioni.getWidth()/2-getWidth()/2), (int)(dimensioni.getHeight()/2-getHeight()/2));
this.setVisible(true);
this.add(pnlDialog, BorderLayout.NORTH);
this.add(pnlDialogBackup, BorderLayout.SOUTH);
pnlDialog.setBorder(general);
GridLayout grid = new GridLayout(7,2,20,5);
pnlDialog.setLayout(grid);
pnlDialog.add(lblLingua);
pnlDialog.add(Language);
pnlDialog.add(lblCarattere);
pnlDialog.add(Char);
pnlDialog.add(lblOrdinamento);
pnlDialog.add(Order);
pnlDialog.add(lblScadenza);
pnlDialog.add(Scadenza);
pnlDialog.add(lblNotifica);
pnlDialog.add(Notifica);
pnlDialog.add(lblTempo);
pnlDialog.add(Time);
pnlDialog.add(Applica,BorderLayout.PAGE_END);
pnlDialogBackup.setBorder(backup);
pnlDialogBackup.add(Backup);
Notifica.addItemListener(this);
lblTempo.setVisible(false);
Time.setVisible(false);
setSize(new Dimension(300, 300));
setResizable(false);
}
Im trying to get it so that when i click on a menu option, the windows changes from the 'welcome text' to 4 buttons which the user can then click on. However, when i click on the simulation button, nothing happens.. the window doesnt change at all.. Ive summarized my code to the basic stuff by the way. anyone see anything i cant?
JFrame GUI = new JFrame("Graphical User Interface");
public gui()
{
JMenuBar menubar = new JMenuBar();
JMenu Simulation = new JMenu("Simulation");
theLabel = new JLabel("Welcome to the Main Menu. ",JLabel.CENTER);
GUI.add(theLabel);
menubar.add(Simulation);
Simulation.add(Simulationmenu);
Simulationmenu.addActionListener(this);
GUI.setJMenuBar(menubar);
GUI.setLocation(500,250);
GUI.setSize(300, 200);
GUI.setVisible(true);
}
public void actionPerformed(ActionEvent E){
if(E.getSource() == Simulationmenu){
// Buttons in the menu i want to output once clicked on 'simulation'
thePanel = new JPanel(new GridLayout(4,0));
Run = new JButton("Run");
Pause = new JButton("Pause");
Reset = new JButton("Reset");
DisplayMaps = new JButton("Display Maps?");
// Add the components to the panel:
thePanel.add("West", Run);
thePanel.add("Center", Pause);
thePanel.add("East", Reset);
thePanel.add("West", DisplayMaps);
// Add the panel to the contentPane of the frame:
GUI.add(thePanel);
// add this object as listener to the two buttons:
Run.addActionListener(this);
Seems your problem in next, you add a new panel(thePanel) to your JFrame(GUI) when it is showing, but in this case you must to call revalidate() method of JFrame(GUI).
Add GUI.revalidate() after GUI.add(thePanel);, it helps you.
I'm trying to modify a GUI. It is hosting a GLCanvas displaying JOGL content.
Here is the code to set it up:
private void setupWindow() {
this.frame = new JFrame(WINDOW_TITLE);
frame.setSize(width, height);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(listener);
menu.add(exitItem);
frame.setJMenuBar(menuBar);
}
Currently, the canvas takes up the entire space in the window, aside from the menu bar. I'd like to make space for other controls in the window, like buttons and list boxes. How can I do it?
I tried inserting the following, but it didn't work:
private void setupWindow() {
this.frame = new JFrame(WINDOW_TITLE);
frame.setSize(width, height);
// ** inserted the following:
JPanel canvasPanel = new JPanel(new BorderLayout());
canvasPanel.add(canvas);
canvasPanel.setSize(30, 40);
canvasPanel.setVisible(true);
// **
frame.add(canvasPanel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(listener);
menu.add(exitItem);
frame.setJMenuBar(menuBar);
}
This doesn't modify the appearance of the window at all.
What should I be doing here? I'm not too familiar with Java GUIs.
Update: Changing the constructor's argument from BorderLayout to FlowLayout causes the GLCanvas to disappear.
Per Anon's answer, you really need to better understand the layout managers. Your question is too open ended.
In case this helps though:
JPanel canvasPanel = new JPanel(new BorderLayout());
frame.add(canvasPanel, BorderLayout.CENTER);
canvasPanel.add(canvas);
Box leftBtnsBox = Box.createVerticalBox();
frame.add(leftBtns, BorderLayout.WEST);
leftBtns.add(new JButton("Button 1"));
leftBtns.add(new JButton("Button 2"));
This code sets a BordeLayout explicitly, but I think the default layout manager for panels (and content panes) is BorderLayout. This code will put two buttons to the top left of the canvas panel arranged vertically. Because you are using these layout managers, your second code example's setSize had no effect.
I suggest checking out this guide to layout managers - it will give you enough info to see what you should try.