Solved

How can I make a window popup here with swing?

Posted on 2014-11-26
6
176 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 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:

810 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