3

I have a short code, i would like to dispose a dialog, but don't want the user to be able to close it, it just doesnt work, help me please.

    import javax.swing.*;
    public class Processing extends JOptionPane{

    JDialog jd;
    public Processing(){
        super(null, JOptionPane.DEFAULT_OPTION, 
                  JOptionPane.INFORMATION_MESSAGE, null, new Object[]{});
        Icon processPic = new ImageIcon("C:\\Users\\Judit\\Desktop\\Mesekocka3D"
                + "\\externals\\pics\\szamitas.gif");

        setMessage(processPic);
        jd = createDialog("Számítás");

        jd.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        jd.setVisible(true);
        jd.dispose();
    }   
}

this is how my code looks like now, I use Jdialog instead of Joptionpane, should i write more characters to make the website accept my edit?

import javax.swing.*;



public class Mifasz
{
 private static class Processing extends JDialog{

public Processing(){
    Icon processPic = new ImageIcon("C:\\Users\\Judit\\Desktop\\Mesekocka3D"
            + "\\externals\\pics\\szamitas.gif");
    JLabel label = new JLabel(processPic);
    add(label);
    setTitle("Számítás");
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);        
    setSize(400, 70);
    setLocation(330, 300);
    setModal(true);
    setVisible(true);
    dispose();
}       
}

  public static void main(String[] args)
  {
    Processing pc = new Processing();
  }

}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ferenc Dajka
  • 1,052
  • 3
  • 17
  • 45
  • why did you mixing `JDialog` with `JOptionPane` (this is same family of `JComponents`), what do you really want to do, (by ignoring `don't want the user to be able to close it`), one of them is useless, use JDialog or JOptionPane :-) – mKorbel Apr 19 '12 at 20:02
  • I'd like to show a gif for the user for a short time, it's like a loadingscreen, I don't want him to do anything with the app till, so I'm using joptionpane, but I would also like to disable the X button, so I'm using jdialog, becouse I didn't find an option to disable the optionpane's X button. – Ferenc Dajka Apr 19 '12 at 20:05

4 Answers4

2

I'd like to show a gif for the user for a short time, it's like a loadingscreen, I don't want him to do anything with the app till, so I'm using joptionpane, but I would also like to disable the X button, so I'm using jdialog, becouse I didn't find an option to disable the optionpane's X button.

1) you can use undecorated JDialog#setUndecorated(true);

2) put gif as Icon to the JLabel

3) use Swing Timer for timing, ooutput from Swing Timer could be a Swing Action and inside actionPerformed put JDialog#setVisible(fasle);

EDIT

i'd like to block it, and it need to be used as many times the user clicks on a JCombobox item

1) create JDialog only once times, you'll reuse that only

2) run Swing Timer#repeat(false) with expecting delay, in actionPerformed put JDialog#setVisible(fasle);

3) on event from selected Item (JComboBox) change Icon in the JLabel myLabel#setIcon(myAnotherGIF)

4) inside invokeLater wrap JDialog#setVisible(true);

5) any another steps aren't required

only in the special cases

6) you have to call

myIcon.getImage().flush();
myLabel.setIcon(myIcon);

in the case that you have issue with Icon's repainting in the JLabel

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • @Ferenc Dajka see my answer here, my question how many times you want to display this JDialod with Image, in the case that only once time during aplications lifetime, then you don't need to reply somehow... – mKorbel Apr 19 '12 at 20:16
  • JDialog doesn't block the screen like joptionpane, i'd like to block it, and it need to be used as many times the user clicks on a JCombobox item – Ferenc Dajka Apr 19 '12 at 20:18
  • @Ferenc Dajka `JDialog#setModal(true);` or there are ModalityTypes, but maybe not important ... – mKorbel Apr 19 '12 at 20:20
  • @Ferenc Dajka see edit in my post about rest from your last comment – mKorbel Apr 19 '12 at 20:41
  • @Ferenc Dajka I think that is designated for Image, ImageIcon, Icon, – mKorbel Apr 19 '12 at 20:55
  • okay it works now, when I've added the label to the dialog first it gave me an exception, maybe i mistyped something, thanks for your help and also for your steadyness ;) – Ferenc Dajka Apr 19 '12 at 20:57
  • okay now my original problem was that dispose doesn't work, and it still doesn't work, i really can't get it why – Ferenc Dajka Apr 19 '12 at 22:24
1

You need to call

jd.setVisible(false);

and then

jd.dispose();
Rahul Borkar
  • 2,742
  • 3
  • 24
  • 38
0

What you want to do is a splash screen, see this.

Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
0

Okay, so I've found a very easy solution: I just disable the mainframe, while processing. How to make a JFrame Modal in Swing java

Community
  • 1
  • 1
Ferenc Dajka
  • 1,052
  • 3
  • 17
  • 45