Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2008

Tip: Looking for answers? Try searching our database.

Custom Dynamic Dialog

Thread view: 
dweesie - 11 Jan 2008 18:23 GMT
I'm trying to understand how to create and use dialogs. Could someone
explain how to build a swing dialog that would show up to 10 different
buttons, that can change dynamically based on some state?  Only by
clicking one of the buttons should close the dialog.  I don't know if
or how I should use a subclass of JDialog or what.

I would like to do something like:

void myButtonActionPerformed(ActionEvent e)
{
 myDialog.setState(currState);
 myDialog.setVisible(true);
 String myText = myDialog.getResult();
 // do something with myText
}
Knute Johnson - 11 Jan 2008 18:48 GMT
> I'm trying to understand how to create and use dialogs. Could someone
> explain how to build a swing dialog that would show up to 10 different
[quoted text clipped - 11 lines]
>   // do something with myText
> }

You don't have to subclass it because you are not going to add to it's
functionality.  Just create a JDialog and add your components.

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

public class test8 {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                final JDialog dialog =
                 new JDialog((Frame)null,"Title",true);
                dialog.setLayout(new GridLayout(3,0));
                JButton b = new JButton("Add a Button");
                b.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        dialog.add(new JButton("New Button"));
                        dialog.validate();
                    }
                });
                dialog.add(b);
                b = new JButton("Close");
                b.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        dialog.setVisible(false);
                    }
                });
                dialog.add(b);
                dialog.setSize(400,300);
                dialog.setVisible(true);
                dialog.dispose();  // always dispose your used windows
            }
        });
    }
}

Signature

Knute Johnson
email s/nospam/knute/

     ------->>>>>>http://www.NewsDem

dweesie - 11 Jan 2008 20:25 GMT
What's the normal way for the parent window to discover what button
was pressed after the dialog has been hidden?
RedGrittyBrick - 11 Jan 2008 23:28 GMT
> What's the normal way for the parent window to discover what button
> was pressed after the dialog has been hidden?

See http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html

One way is to have the ActionListener for each button set a variable and
either inspect that variable or set a field (instance variable) and
provide access to it's value through a getter method.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.