Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

help on closing dialog..

  Asked By: Jeff    Date: Mar 22    Category: Java    Views: 1187
  

i have my classes all working fine,i click on the frame and the
dialog box is show,the dialog box should close after i click ok,but
the first time i click it it is still there..the dialog box only
close the second time i click it.what is wrong?

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Kanchan Ap     Answered On: Mar 22

If not too much trouble could I see your code too?
What you describe is something I never encounter
previously?

 
Answer #2    Answered By: Haya Yoshida     Answered On: Mar 22

this is my dialog  class

import java.awt.event.*;
import java.awt.*;


public class DialogBox
{

public static void main(String args[])
{
Frame f;
new dialog(f = new Frame(),"Unlock Screen",true);
f.dispose();

}
}

class dialog extends Dialog implements ActionListener
{

Button ok = new Button("OK");

dialog(Frame parent, String title, boolean modal)
{
super(parent,title,modal);

ok.addActionListener(this);

add(BorderLayout.NORTH,new Label("Do you want to
unlock?"));
add(BorderLayout.SOUTH,ok);

/* Size the frame  */
pack();

/* Center the dialog */
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width - frameDim.width)
/ 2,(screenDim.height - frameDim.height) / 2);

/* show  the dialog */
setVisible(true);
}

public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == ok)
setVisible(false);//dialog box  is dispose

//show progressbar when button is click
DetectingBar bar = new DetectingBar();
bar.show();
bar.pack();
}
}

this is my detecting class(which is use in dialog class)

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

public class DetectingBar extends JFrame {
public final static int ONE_SECOND = 1000;

private JProgressBar progressBar;
private Timer timer;
private LongTask task;


public DetectingBar() {
super("Detecting");
setSize(40,20);
task = new LongTask();

progressBar = new JProgressBar(0, task.getLengthOfTask());
progressBar.setValue(0);
progressBar.setStringPainted(true);

JPanel panel = new JPanel();
panel.add(progressBar);

JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.NORTH);
contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20,
20, 20));
setContentPane(contentPane);

/* Center the dialog */
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frameDim = getBounds();
setLocation((screenDim.width -
frameDim.width)
/ 2,(screenDim.height -
frameDim.height) / 2);

//Create a timer.
timer = new Timer(ONE_SECOND, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
progressBar.setValue(task.getCurrent());

if (task.done())
{
Toolkit.getDefaultToolkit().beep();
timer.stop();

progressBar.setValue(progressBar.getMinimum());

dispose();
}
}
});
task.go();
timer.start();
}

and lastly this is my main class(dialog and detecting class will
appear)

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


public class ScreenSaver extends JFrame implements
MouseListener,KeyListener
{
//public final static int ONE_SECOND = 1000;
//Timer timer;

public ScreenSaver()
{
//Get the window size// Create the frame
String title = "Screensaver";
JFrame frame = new JFrame(title);

Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();

// Show the frame according to the user's window size
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
frame.setSize(screenWidth, screenHeight);

//Set background screen to black
Container screen = getContentPane();
screen.setBackground(Color.black);
frame.getContentPane().add(screen);
frame.addMouseListener(this);
frame.addKeyListener(this);

//Set the frame visible
frame.setVisible(true);

//Diable the resizeable button
frame.setResizable(false);


// Set to ignore the cross button
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);




}



public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseClicked(MouseEvent e)
{
Frame f = new Frame();
String title = "Unlock Screen";
boolean y = true;
dialog d = new dialog(f,title,y);
d.setVisible(true);

}

public void mousePressed(MouseEvent e){}

public void mouseReleased(MouseEvent e){}

public void keyPressed(KeyEvent e)
{
Frame f = new Frame();
String title = "Unlock Screen";
boolean y = true;
dialog d = new dialog(f,title,y);
char x;
if((x = e.getKeyChar()) >= '0' && x <= '9')
{

d.setVisible(true);
}
}

public void keyTyped(KeyEvent e){}

public void keyReleased(KeyEvent e){}

public static void main(String[] args)
{

ScreenSaver screensaver = new ScreenSaver();
}

}

 
Answer #3    Answered By: Geneva Morris     Answered On: Mar 22

I do not recall there is done() in Timer class. Why
don't you want user close  the dialog  in default way?
If you force user to use OK button to close the
operation, then you have more lucks in using
MessageDialog(). But it would defeat the purpose of
your program.

The way I see it, you probably have problem in
threading. Java is the multithreading language,
powerful but very difficult to debug. Since you apply
Timer, you are save yourself from alot of headache.
But Timer is also in the neighborhood of thread.

Trace your data byte see where's the hell it travels
in your program, main thread, or GUI thread.

 
Didn't find what you were looking for? Find more on help on closing dialog.. Or get search suggestion and latest updates.




Tagged: