How do I resize my JMenuItem to fit a JButton? - java

Here is the code I am using:
JMenu menu = new JMenu("Menu");
JMenuItem item = new JMenuItem("Add");
item.add(new JButton("SOMETHING A BIT WORDY"));
menu.add(item);
// ...
JMenuBar menuBar = new JMenuBar();
menuBar.addMenu(menu);
JFrame frame = new JFrame();
frame.setJMenuBar(menuBar);
// ...
frame.pack();
frame.setVisible();
However, the button (inside of the menu item) appears small and only contains the text "..." which is used when the button's size cannot fit the intended text. Is there any way to make my JMenuItem "grow" to fit my JButton (or JTextArea, or JLabel, or whatever the Component may be)?

I totally agree with others who posted their comments, it may be an odd mix to put an button in the menuitem, if you insist to this, setPreferredSize can solve your problem.
I used a bad hard code implementation, if anyone know how to set the width dynamically, please guide me too. :P thx in advance.
JMenu menu = new JMenu("Menu");
JMenuItem item = new JMenuItem("Add");
item.add(new JButton("SOMETHING A BIT WORDY"));
item.setLayout(new FlowLayout()); // set FlowLayout for item
item.setPreferredSize(new Dimension(200, 100)); // hard code implementation :-(
menu.add(item);
// ...
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
JFrame frame = new JFrame();
frame.setJMenuBar(menuBar);
// ...
frame.pack();
frame.setVisible(true);

Related

How to fix the display of elements using Swing in Java

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);

Is there a way to move a jMenuBar to somewhere else than the top of my jFrame?

I'm creating an undecorated jFrame, and I wanted to add a jMenuBar to this jFrame to allow me to use drop-down menus for various things.
But, the jMenuBar always defaults to the very top of my jFrame, and it being undecorated, it pushes down a jPanel I had set there with the "Minimize" and "Close" buttons, and trying to move it anywhere else is unsuccessful.
I've tried using a jToolBar, and while it allows for more customization in terms of placement, I can't find a way to add drop-down menus onto the buttons there.
Is there a way to move a jMenuBar somewhere else than the top or to add a drop-down menu to a jToolBar?
You should have a look at Layout Managers.
Here is a simple example to achieve what you want, using a BorderLayout:
public static void main(String[] args) {
JFrame jf = new JFrame();
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
JLabel label = new JLabel("North");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("A south menu");
menu.add(new JMenuItem("A south item"));
menuBar.add(menu);
container.add(label, BorderLayout.NORTH);
container.add(menuBar, BorderLayout.SOUTH);
jf.add(container);
jf.setPreferredSize(new Dimension(200,100));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.pack();
}
result:

I want JMenu to show the items when the program starts

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();
}
});

Why isn't my 'Menu' bar displaying in my JFrame 'Window' Object?

Here I've created the JFrame object, 'window'. And set it to 'true'.
import java.awt.*;
public static void main(String[] args) {
JFrame window = new JFrame("GUI Test");
window.setSize(250, 100);
window.setLocation(100, 100);
window.setVisible(true);
Here I've added menu items to the 'menu'. And set visibility as 'true'.
JMenuItem home = new JMenuItem("Home");
JMenuItem about = new JMenuItem("About");
JMenuItem tag = new JMenuItem("Tag");
JMenu menu = new JMenu("menu");
menu.add(home);
menu.add(about);
menu.add(tag);
menu.setVisible(true);
}
I'm not getting any errors. Then what am I missing? Why doesn't my menu display in the 'window' object?
You need to add your JMenus to a JMenuBar and set to to the JFrame using JFrame#setJMenuBar
See How to use Menus for more details
Frirst creat JMenubar and add the JMenu into that.
JMenuBar menubar = new JMenuBar();
Add the JMenu into JMenuBar and JMenuar into JFrame to display in the window.
menubar.add(menu);
window.setJMenuBar(menubar);
You need to add JMenuBar with window,
JMenuBar menuBar= new JMenuBar();
.....
menuBar.add(menu);//Add menu with JMenuBar.
window.setJMenuBar(menuBar);
Add the Jmenu into in you JFrame
window.add(menu);
and also set the layout to JFrame
window.setLayout(new Flawlayout()); // you can try other layout too
And best way is
How to use Menus

Can't see a MenuBar

the code that I wrote does not show the menubar, any idea what I should do?
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//What happens on close.
this.setSize(1100, 750);//Initial size of the frame.
menuBar = new MenuBar();
east = new EastPanel();
central = new CentralPanel();
south = new SouthPanel();
//Add the menu bar to the frame.
this.setJMenuBar(menuBar.getComposition());
//Frame's other components.
this.add(central.getCentralPanel());
//this.add(msBoard.getMessagesBoard(), BorderLayout.SOUTH);
this.add(east.getEastPanel(), BorderLayout.EAST);
this.add(south.getSouthPanel(), BorderLayout.SOUTH);
//this.add(menuBar);
//Load the card images.
//cards = new CardImages();
//cards.loadCards();
//Initialize cardsPerPlayer list.
//cardsPerPlayer = new ArrayList<ImagePanel>();
this.setVisible(true);
where getComposition is
public JMenuBar getComposition(){
return this.Composition;
}
and Composition is private JMenuBar
Adding MenuBar is not enough. You should attach it to the current JFrame object.
Also make sure you're adding some menu items into it.
Example:
frame.setJMenuBar(theJMenuBar);
Propably because You didint add anything to this menubar.
In eclipse when ive added menu bar (using builder) without any items i didint see it but after i add menu to menubar it will works.

Categories

Resources