Solved

How can I make a window popup here with swing?

Posted on 2014-11-26
6
178 Views
Last Modified: 2014-12-06
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);
    }
}
0
Comment
Question by:Computer Guy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
krakatoa earned 500 total points
ID: 40468610
Do you think you could start using the code delimiters instead of bald text here? There are some important experts who won't even look at your question if it's unformatted and therefore difficult to read.

But from what I can see, it could be that you are aiming at something like :

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;
 import javax.swing.*;

 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");
				 JTextField nickName = new JTextField(10); 
        JTextField ipAddress = new JTextField(10); 

 
        JPasswordField password = new JPasswordField(10); 

 
        JComponent[] inputs = new JComponent[] { 
                 new JLabel("Name"), nickName, new JLabel("Something else"), 
                ipAddress, new JLabel("Password"), password 
            }; 

 
        JOptionPane jop = new JOptionPane(inputs, JOptionPane.PLAIN_MESSAGE); //puts up OK button only 
 
 
         JDialog dialog = jop.createDialog( 
                 "New Client Creation"); 
         dialog.setVisible(true); 

             }
         });
     }
     public static void main(String[] args) {
         MenuExp me = new MenuExp();
         me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         me.setVisible(true);
     }
 } 

Open in new window

0
 
LVL 3

Author Comment

by:Computer Guy
ID: 40468737
Woops!  So in other words use JOptionPane?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40468744
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.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 3

Author Comment

by:Computer Guy
ID: 40469002
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.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40469125
I don't know about Netbeans, sorry. I don't use IDEs. ;(
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40470069
>> 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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question