Solved

How can I make a window popup here with swing?

Posted on 2014-11-26
6
172 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Increment alphanumeric sequence 6 80
array220 challenge 8 48
Python Assistance 7 36
What is the latest versions eclipse neon 2 122
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 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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 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