?
Solved

halt the JDialog until i click ok button

Posted on 2005-04-25
10
Medium Priority
?
283 Views
Last Modified: 2010-03-31
hi,
i made a JDialog, and it return value to owner JFrame. i want the dialog wait unitl i click a "OK" button and then return data to the own Jframe, how to do that?

best regards
0
Comment
Question by:Barca
  • 6
  • 4
10 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13861745
Modal dialogs are suppose to block until they are not visible (or dispose).
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13861760
So, after you made the dialog visible you can assume that the code will not continue until you press ok in the dialog (which should trigger by your code a visible false or dispose action).
0
 
LVL 3

Author Comment

by:Barca
ID: 13861843
pls take a look on my code, i really have no idea how to fix it...
it will automatically return data and execute, and the button in JDialog is not work..
><


-------------
in JFrame

.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent event ){
                if(mode ==0){
                    cdda.addcd(addcd.getcd(LibrarySystem.this));
                }
            }
        }
        );
-------------

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

public class addcd extends JDialog{
    private JButton okButton,cancelButton;
    private JLabel lbltitle,lblplaytime,lblcomment,lblartist,lblnumberoftracks;
    private JTextField txttitle,txtplaytime,txtartist,txtnumberoftracks;
    private JTextArea tacomment;
    private CD tempcd = new CD("String title",56,"String comment", "String artist",12);
    private boolean flag = false;
    /** Creates a new instance of addcd */
    private addcd(JFrame frame) {
        super(frame);
        Container container = getContentPane();
        container.setLayout(new GridLayout( 6, 2 ));
        lbltitle = new JLabel("Title");
        lblplaytime = new JLabel("Play Time");
        lblcomment = new JLabel("Comment");
        lblartist = new JLabel("Artist");
        lblnumberoftracks = new JLabel("Number OF Tracks");
        txttitle = new JTextField();
        txtplaytime = new JTextField();
        txtartist = new JTextField();
        txtnumberoftracks = new JTextField();
        tacomment = new JTextArea();
        okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent event ){
                if(inputvalid() == true){
                    tempcd = new CD(txttitle.getText(),Integer.parseInt(txtplaytime.getText()), tacomment.getText(), txtartist.getText(),Integer.parseInt(txtnumberoftracks.getText()));
                    dispose();
                }
            }
        }
        );
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent event ){
                    dispose();
                }
        }
        );
        container.add(lbltitle);
        container.add(txttitle);
        container.add(lblplaytime);
        container.add(txtplaytime);
        container.add(lblartist);
        container.add(txtartist);
        container.add(lblnumberoftracks);
        container.add(txtnumberoftracks);
        container.add(lblcomment);
        container.add(tacomment);
        container.add(okButton);
        container.add(cancelButton);
        setLocation(10, 10);
        setSize(400, 300);
        setVisible(true);
        setModal(true);
    }
    public boolean inputvalid(){
        if(!(txttitle).equals("") && !(txtplaytime).equals("") && !(txtartist).equals("") && !(txtnumberoftracks).equals("")){
            flag = true;
        }
        return this.flag;
    }
    public static CD getcd(JFrame frame){
        addcd addcd1 = new addcd(frame);
        return addcd1.tempcd;
    }
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 15

Expert Comment

by:aozarov
ID: 13861953
try changing super(frame) to super(frame, true);
and there is no need to set it directly setModal(true);

Don't you see the dialog at all?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13861977
Also are you sure  mode is equal to zero when you click on your JFrame button?
>> if(mode ==0){
                    cdda.addcd(addcd.getcd(LibrarySystem.this));
                }
0
 
LVL 3

Author Comment

by:Barca
ID: 13861979
if i click the cancel button or the cross button in the Dialog, it also return data...
0
 
LVL 3

Author Comment

by:Barca
ID: 13861987
Also are you sure  mode is equal to zero when you click on your JFrame button?
>> if(mode ==0){
                    cdda.addcd(addcd.getcd(LibrarySystem.this));
                }

SURE
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13862055
>> if i click the cancel button or the cross button in the Dialog, it also return data...
Ok, so now you changed super(frame) to super(frame, true) and the modality works correctly, right?

you can do (if you want to return null in this case and not the default):

cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent event ){
                     tempcd  = null;
                    dispose();
                }
        }
        );
0
 
LVL 3

Author Comment

by:Barca
ID: 13862204
how about the cross button on the top right corner of the Dialog...
0
 
LVL 15

Accepted Solution

by:
aozarov earned 320 total points
ID: 13862327
Just declare tempcd null (instead of having default value) and only when you want to return a non-null value
in the OK listener do the initialization, no?
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

809 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