[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Opening a second window

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
urivera
Asked:
urivera
1 Solution
 
tbboyettCommented:
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
 
mukundha_expertCommented:
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
 
rongasaCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now