Solved

How can I make a window popup here with swing?

Posted on 2014-11-26
6
179 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

636 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