?
Solved

Opening a second window

Posted on 2006-06-13
4
Medium Priority
?
286 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
[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
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 500 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 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