How do I open new windows from menubars? - java

This is my code so far:
public static void main(String[] args){
JFrame frame = new JFrame("Breakout!");
frame.setLocation(300, 300);
//Making the menubar
JMenuBar mb = new JMenuBar();
frame.setJMenuBar(mb);
JMenu fileMenu = new JMenu("File");
mb.add(fileMenu);
JMenuItem newAction = new JMenuItem("About");
fileMenu.add(newAction);
newAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("open new window here");
}
});
BreakoutCourt c = new BreakoutCourt();
frame.add(c);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I am making a Breakout game. I want to make an About window that displays information about the game (like, how to play it and so on). How would I go about doing that? Thank you for the help! I'm very new to Java Swing, so yeah.

You could do a simple one with the message type of JOptionPane:
JOptionPane.showMessageDialog(frame, "Breakout! v1.0");
(You'll need to make the frame final to do this, since it's being accessed from within the anonymous action listener).
For more control over what is displayed, and whether it should be modal or not, you could look at JDialog. There's an overview of using dialogs in Swing here: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html.

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

How to get a JFrame in Swing

I'm working on a simple app and I need to access JFrame created when initializing a program from different places where it's not always possible to access it directly (e.g. contentPane is empty so SwingUtilities.getWindowAncestor(Component) won't work). Is there any other way to do this? For example, this is a class resposinble for initialization:
public final class Application{
public static void start(){
//Empty screen with menu
SwingUtilities.invokeAndWait(() -> {
JFrame frame = new JFrame("Main frame");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Start");
menuBar.add(menu);
frame.setJMenuBar(menuBar);
});
}
}
And I need to use it from some other places, e.g.
JPanel panel = new JPanel();
//initialize the panel
//push it to the content pane of Main frame
So, in order to share the main frame I tend to wrap it in a singleton class and then provide a static method. Maybe there is another way of doing that?
You can store it in a static reference:
public final class Application{
static JFrame mainFrame; //static attribute
public static void start(){
//Empty screen with menu
SwingUtilities.invokeAndWait(() -> {
JFrame frame = new JFrame("Main frame");
mainFrame = frame; //Storing in static attribute
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Start");
menuBar.add(menu);
frame.setJMenuBar(menuBar);
});
}
}
An call it as follows:
JPanel panel = new JPanel();
//initialize the panel
Application.mainFrame.getContentPane().add(panel);
This is a ugly solution maybe you can review Design Patterns for a better solution:
http://www.tutorialspoint.com/design_pattern/

How do I change the direction of JMenus in a JMenuBar

When I create a menu in java GUI by using JMenuBar it puts all JMenus from Left-to-right direction like this:
I want to change it to Right-to-left like this:
I want do this in an English OS, so suggestions of an Arabic or Right-to-Left solution aren't what I'm looking for.
You can use Component.applyComponentOrientation to change the orientation of the JMenuBar:
import javax.swing.*;
import java.awt.*;
public class R_L_MenuBar_Demo
{
public static void main(String[] args){
SwingUtilities.invokeLater(() -> createAndShowGUI());
}
private static void createAndShowGUI()
{
JMenuBar mb = new JMenuBar();
JMenuItem item_1 = new JMenuItem("First Item");
JMenu menu_2 = new JMenu("Second Menu");
JMenuItem item_3 = new JMenuItem("First Item in Second");
menu_2.add(item_3);
mb.add(item_1);
mb.add(menu_2);
//switch the orientation of the menubar to right to left
JButton btn_r_to_l = new JButton("Switch menubar to r_to_l");
btn_r_to_l.addActionListener(e -> {
mb.invalidate();
mb.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
mb.validate();
});
//switch the orientation of the menubar to left to right
JButton btn_l_to_r = new JButton("Switch menubar to l_to_r");
btn_l_to_r.addActionListener(e -> {
mb.invalidate();
mb.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
mb.validate();
});
JFrame frame = new JFrame("R_L_MenuBar");
frame.setLayout(new FlowLayout());
frame.add(btn_r_to_l);
frame.add(btn_l_to_r);
frame.setJMenuBar(mb);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200 , 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
This will look like this:
The Default look (left to right)
And after switching to right-to-left:

JMenu consumes focuslost event in Windows7 LAF Java7

If a popup menu is still open when another component is clicked, then the component does not get the event, because it's probably consumed by the popup. This happens for all JPopupmenus in general.
This happens only in Java 7 with windows LAF (Windows7). Is there a workaround? Is it a known bug?
import javax.swing.*;
import java.awt.event.*;
public class Test
{
public static void main(String[] s)
throws Exception
{
String lookAnfFeelClassName = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(lookAnfFeelClassName);
JMenu menu = new JMenu("TEST Menu");
JMenuItem menuItem = new JMenuItem("Menu Item 1");
JMenuBar menuBar = new JMenuBar();
menu.add(menuItem);
menuBar.add(menu);
final JButton b = new JButton("Test");
b.setBounds(5, 50, 60, 20);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//If the Menu is open when I press the button, the putton is not pressed
//so I have to press it again.
JOptionPane.showMessageDialog(b, "Button Pressed");
}
}
);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(150, 150);
frame.setJMenuBar(menuBar);
frame.getContentPane().setLayout(null);
frame.getContentPane().add(b);
frame.setVisible(true);
}
}
Here is the magic line that fixes the problem:
UIManager.put("PopupMenu.consumeEventOnClose", Boolean.FALSE);
I found this after looking into the source code for the BasicPopupMenuUI class. Apparently this behaviour is a deliberate design choice according to the following comments in the code, but it sure feels like a bug to me.
// Ask UIManager about should we consume event that closes
// popup. This made to match native apps behaviour.
By the way, it happens in Java 5 and 6 too.

Java - Is it possible to add a JMenuBar to a JFrame's decoration window?

I'm wondering if I can add a JMenuBar to a JFrame or JRootPane's Decoration window, or otherwise the border that surrounds the content pane within. I see applications like Firefox or Photoshop having their menubar in the decoration window.
Is this possible? I've looked around google, but I haven't been able to find any results over this kind of thing. I'm hoping Java has this capability.
Not sure what you're looking for, but you can add JMenuBar to JFrame - JFrame.setJMenuBar(). Look at How to Use Menus tutorial for details.
Edit:
Below is an overly simplified example of undecorated frame with a menu, just to demo the idea.
You may want to turn to existing solutions - JIDE has ResizableFrame for this purpose. It is part of open source JIDE-oss. Substance L&F has support for title bar customization (see What happened to the Substance LaF?). You can also very efficiently utilize ComponentMover and ComponentResizer classes by #camickr, see Resizing Components article for more details.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class UndecoratedFrameDemo {
private static Point point = new Point();
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
point.x = e.getX();
point.y = e.getY();
}
});
frame.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point p = frame.getLocation();
frame.setLocation(p.x + e.getX() - point.x,
p.y + e.getY() - point.y);
}
});
frame.setSize(300, 300);
frame.setLocation(200, 200);
frame.setLayout(new BorderLayout());
frame.getContentPane().add(new JLabel("Drag to move", JLabel.CENTER),
BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
JMenuItem item = new JMenuItem("Exit");
item.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menu.add(item);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
}
Try this...
Adding JMenuBar to the JFrame
JMenuBar menuBar = new JMenuBar();
myFrame.setJMenuBar(menuBar);
Adding JMenuBar to the JPanel
JMenuBar menuBar = new JMenuBar();
myPanel.add(menuBar);
See this JMenuBar Tutorial link for more information on the topic.
I've done some digging and the short answer is ... no.
Basically the frame decoration is off loaded to the OS, so we don't have any access to it's content.
However, if you want to go to a lot of work, you can implement your own decoration. You need to take responsibility for resizing and moving though.
You might want to check out
http://java-swing-tips.blogspot.com.au/2010/05/custom-decorated-titlebar-jframe.html and http://www.paulbain.com/2009/10/13/howto-draggabe-jframe-without-decoration/ for ideas

Categories

Resources