Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

java - JOptionPane without button

I need to present a information message that needs to be in the screen for 5 seconds, during this time, user can't close the dialog. The specification says clearly that the dialog shouldn't have any button. Is there a way I can use JoptionPane.showMessageDialog in a way that the dialog have no button?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about this way using showOptionDialog, maybe not showMessageDialog, but the same thing when we have no buttons or place to enter text (downfall is it can be closed by user):

enter image description here

  JOptionPane.showOptionDialog(null, "Hello","Empty?", JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);

UPDATE

Here is another way, it uses JOptionPane and JDialog (even better as it is uncloseable by user):

enter image description here

final JOptionPane optionPane = new JOptionPane("Hello world", JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);

final JDialog dialog = new JDialog();
dialog.setTitle("Message");
dialog.setModal(true);

dialog.setContentPane(optionPane);

dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.pack();

//create timer to dispose of dialog after 5 seconds
Timer timer = new Timer(5000, new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        dialog.dispose();
    }
});
timer.setRepeats(false);//the timer should only go off once

//start timer to close JDialog as dialog modal we must start the timer before its visible
timer.start();

dialog.setVisible(true);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...