Avatar of Computer Guy
Computer Guy

asked on 

How can I make a window popup here with swing?

Hi,

I am trying to make an app similar to the attached, where I have a menu bar.

When I click on an item in the menu bar, in this case Add Client,  how can I make the add client window will appear with the contents of that window? Can someone please just list list a little example?

Thanks!

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuExp extends JFrame {
   
    public MenuExp() {
       
        setTitle("Menu Example");
        setSize(1024, 768);
       
        // Creates a menubar for a JFrame
        JMenuBar menuBar = new JMenuBar();
       
        // Add the menubar to the frame
        setJMenuBar(menuBar);
       
        // Define and add two drop down menu to the menubar
        JMenu fileMenu = new JMenu("File");
       // JMenu editMenu = new JMenu("Edit");
        menuBar.add(fileMenu);
        //menuBar.add(editMenu);
       
        // Create and add simple menu item to one of the drop down menu
        JMenuItem addClientAction = new JMenuItem("Add Client");
        JMenuItem editClientAction = new JMenuItem("Edit Client");
        JMenuItem exitAction = new JMenuItem("Exit");
       


        fileMenu.add(addClientAction);
        fileMenu.add(editClientAction);
        fileMenu.addSeparator();
        fileMenu.add(exitAction);
        // Add a listener to the New menu item. actionPerformed() method will
        // invoked, if user triggred this menu item
        addClientAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("You have clicked on the add new client");
            }
        });
    }
    public static void main(String[] args) {
        MenuExp me = new MenuExp();
        me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        me.setVisible(true);
    }
}
Java

Avatar of undefined
Last Comment
zzynx
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Computer Guy
Computer Guy

ASKER

Woops!  So in other words use JOptionPane?
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

JDialog in conjunction with JOptionPane gives you the facility to create custom interfaces yes.  Take a look at the API lit for JDialog for more detail.
Avatar of Computer Guy
Computer Guy

ASKER

Thanks!!!

Also, what is your opinion on using NetBeans for something like this?

I have toyed with it, but for some reason it seems as if the code style is A LOT different than doing it this way.
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

I don't know about Netbeans, sorry. I don't use IDEs. ;(
Avatar of zzynx
zzynx
Flag of Belgium image

>> Also, what is your opinion on using NetBeans for something like this?
I don't use NetBeans neither.
But I'd like to say that code generation is all a matter of taste.
You can write functionally identical code in a dozen of ways.
If NetBeans generates code that you can read and understand it's okay.
If it doesn't then don't use it and write your own.
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo