Solved

Opening a second window

Posted on 2006-06-13
4
266 Views
Last Modified: 2010-03-31
In java how do I open up a second window (or pop window) when the user presses a button.  For example, say my main window has a "select city" button, upon pressing the button a second window comes up which lists a list of cities.
0
Comment
Question by:urivera
4 Comments
 
LVL 5

Expert Comment

by:tbboyett
ID: 16898937
make sure that the dialogs are not model and just make the same call again if you want the same window to display again.  
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 16899361
write a class for a frame which lists the cities and in that have a method to initialize the frame and show it. In your main window add a actionlistener to that select cities button, so when ever the button is clicked call the method to show up the second window with list of cities.
0
 
LVL 1

Accepted Solution

by:
rongasa earned 125 total points
ID: 16900178
See this code, I think this is what you are looking for -You are not making clear if your problem is the creation of a new window/dialog box or the use of a button to make the window appear... -.
The code should help, but you will need to search it a little by your self in order to brink it up to your likings. Howeverthis is prety much how you should do it. I prefer to use swing (just a java library), so this code sniffet is in java swing. If you like you may use anything else you might got in mind, the idea is prety much the same.

Enjoy!!!

import java.awt.*;
import javax.swing.*;

....
// create the button
 JButton list_cities= new JButton();
.... // add it to your window...

// assign an action listener to the button...
        list_cities.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            cities_actionPerformed(e);
          }
        });

....
// When the button is pressed, the action listener is invoked and thus the dialogbox will appear.
void list_cities_actionPerformed(ActionEvent e) {
// Code that makes the second window. It will be inside the action listener of the button list_cities
   ...........
    Vector Vectorofcities= new Vector(20);
    Vectorofcities.add(London);
    Vectorofcities.add(Munich);
    Vectorofcities.add(Athens);

    JList cities = new JList(Vectorofcities);
    cities.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    cities.setToolTipText("Select a city.");

    JScrollPane jScrollPane1 = new JScrollPane();
    jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    jScrollPane1.setBorder(BorderFactory.createLoweredBevelBorder());
    jScrollPane1.setDebugGraphicsOptions(0);
    jScrollPane1.getViewport().add(cities, null);
    Frame frame = new Frame();
    int index = 0;
    int result = 0;
    String msg ="Please Select a city. where you wish to travel.";
    do {
      Object oj[] = {msg, "Notice that after selecting a city, a ticket will be bought",jScrollPane1};
      JDialog dialog;
      JOptionPane pane;
      result = 0;
      pane = new JOptionPane(oj, JOptionPane.PLAIN_MESSAGE,JOptionPane.OK_CANCEL_OPTION);
      dialog = pane.createDialog(frame, "DBS Restoration procedure!!!");
      dialog.show();
      try {
        result = ( (Integer) pane.getValue()).intValue();
      }
      catch (Exception ex) {
        result = JOptionPane.CANCEL_OPTION;
      }
      .........
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16900677
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 78
eclipse apache tomcat admin console 52 96
JavaFX TableView not displaying correctly 3 16
How to determine if a string is a valid SHA value 7 29
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

775 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