Solved

How can I make a window popup here with swing?

Posted on 2014-11-26
6
174 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now