Solved

Opening a second window

Posted on 2006-06-13
4
248 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:mayankeagle
ID: 16900677
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

758 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