Solved

Opening a second window

Posted on 2006-06-13
4
257 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to start Neon 20 84
recursion example 16 111
sites similar to codingbat to improve coding hanson skills 3 40
Java DateChooser? 3 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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

914 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

15 Experts available now in Live!

Get 1:1 Help Now