1

When the user hits "close" in one JFrame frame, I want the JFrame credits to be displayed showing my name and stuff for 2.5 seconds before the program exits. Now, credits is showing, but empty without the textArea and the Button - couldnt find whats wrong.

Heres my code: For the closing operation of frame

frame.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                        int result = JOptionPane.showConfirmDialog(null, "Sind Sie sicher?", "Schließen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                        if (result == JOptionPane.YES_OPTION){
                            credits.setVisible(true);
                            try {
                                Thread.sleep(2500);
                            } catch(InterruptedException ex) {
                                Thread.currentThread().interrupt();
                            }
                                System.exit(0);
                        } else {
                            //do nothing 
                        }
                    }
                });

For credits (just the class initializing the frame):

public class CreditsFrame extends JFrame {

Positioner pos = new Positioner();

private JPanel contentPane;
ImageIcon frameIcon = new ImageIcon("files/images/frameicon.png");
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                CreditsFrame frame = new CreditsFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public CreditsFrame() {
    setIconImage(frameIcon.getImage());
    setAlwaysOnTop(true);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(pos.posX(pos.screenX, 441), pos.posY(pos.screenY, 210), 441, 210);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    JTextArea txtarea = new JTextArea();
    txtarea.setBounds(10, 11, 415, 125);
    txtarea.setEditable(false);
    txtarea.setBackground(Color.WHITE);
    txtarea.setWrapStyleWord(true);
    txtarea.setLineWrap(true);
    txtarea.append("created by & more here");
    contentPane.setLayout(null);
    contentPane.add(txtarea);

    JButton btnOk = new JButton("Ok");
    btnOk.setBounds(154, 147, 89, 23);
    btnOk.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            dispose();
        }
    });
    contentPane.add(btnOk);
}

}

Any suggestions for a quick fix? Thanks.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Zi1mann
  • 334
  • 1
  • 2
  • 15

1 Answers1

2

Any suggestions for a quick fix?

Yes, don't call Thread.sleep(...) on the Swing event thread unless you want to put your entire GUI to sleep. Instead use a Swing Timer to handle the delay. Basically, the timer's ActionListener will be called after the milliseconds delay has passed.

e.g.,

frame.addWindowListener(new java.awt.event.WindowAdapter() {
    @Override
    public void windowClosing(java.awt.event.WindowEvent windowEvent) {
        int result = JOptionPane.showConfirmDialog(null, "Sind Sie sicher?", "Schließen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (result == JOptionPane.YES_OPTION){
            credits.setVisible(true);
            new Timer(2500, new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.exit();
                }
            }).start();
        } else {
            //do nothing 
        }
    }
});

Also, look at The Use of Multiple JFrames, Good/Bad Practice?

Other issues that will bite you in the future:

  • You look to be using null layouts and setBounds. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms different from the original one.
  • Never set a JTextAreas bounds as this will make it completely fail if it is placed within a JScrollPane and more text than can be displayed is added -- the scroll bars will seem not to work since you've artificially constrained the size of the text component.
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I know that absolute layouts are very inconsistent, but the frames are not resizable so at least this wont mess with them. Thanks for your proposed solution, worked with some tweaks. – Zi1mann Oct 30 '15 at 17:06
  • @Zi1mann: don't say I didn't warn you. Just wait til you create a complex GUI, and then discover that you need to add one more JRadioButton in the middle of it. If you used nested JPanels with layout managers, then this will be a very easy fix. If you used null layout, then you must manually tweak the positions of every component -- and hope that it will work on all platforms. Luck. – Hovercraft Full Of Eels Oct 30 '15 at 17:12