1

So I've been playing around with GUIs and created a relatively nice welcome-frame for my user.
Said frame has two text options, where each text option should, in theory, open a new window.

One of the text frames does exactly what it's suppose to, opens a new window, as desired, the other, however, does not.

The following is my code:

package gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Frame1Welcome extends JFrame {

private JLabel intro1 = new JLabel(
        "Welcome to Twitter Farmer.");
private JLabel intro2 = new JLabel(
        "In order to start farming up those tweets, first decide if you want to farm for a specific amount of time, or farm a certain amount of tweets.");
private JLabel intro3 = new JLabel(
        "By specifying the amount of time you wish to farm for, Twitter Farmer will gather as many tweets as it can in said time.");
private JLabel intro4 = new JLabel(
        "If you know how many you want however, Twitter Farmer will instead gather up the exact desired amount.");
private JLabel intro5 = new JLabel(
        "Please enter the time in seconds, or the amount of tweets as whole number, and press the 'ENTER'-key.");

private JLabel tweets = new JLabel("Tweets:");
private JLabel time = new JLabel("Time:");

private JTextField twitterText = new JTextField(10);
private JTextField timeText = new JTextField(10);

private String inputText; // REMOVE ME

public Frame1Welcome(String title) {
    super(title);

    launchGUI.setMyDefaults(this, 800, 300);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(0, 0, 10, 0);

    JPanel northernPanel = new JPanel(new GridBagLayout());
    this.add(northernPanel, BorderLayout.NORTH);

    gbc.gridy = 0;
    northernPanel.add(intro1, gbc);
    gbc.gridy = 1;
    northernPanel.add(intro2, gbc);
    gbc.gridy = 2;
    northernPanel.add(intro3, gbc);
    gbc.gridy = 3;
    northernPanel.add(intro4, gbc);
    gbc.gridy = 4;
    northernPanel.add(intro5, gbc);

    JPanel southernPanel = new JPanel(new GridBagLayout());
    this.add(southernPanel, BorderLayout.CENTER);

    gbc.gridx = 0;
    southernPanel.add(tweets, gbc);
    gbc.gridx = 1;
    southernPanel.add(twitterText, gbc);

    gbc.gridy = 1;

    gbc.gridx = 0;
    southernPanel.add(time, gbc);
    gbc.gridx = 1;
    southernPanel.add(timeText, gbc);

    twitterText.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            inputText = twitterText.getText(); // REMOVE ME
            dispose();

            System.out.println("TWEETS:" + inputText); // REMOVE ME

            try {
                new Frame3Tweets("Twitter Farmer - Tweet-farm by desired amount", Integer.parseInt(timeText.getText()));
                } catch (NumberFormatException error) {
                    //ADD INVALID NUMBER WINDOW 
                }

        }
    });

    timeText.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            inputText = timeText.getText(); // REMOVE ME
            dispose();

            System.out.println("TIME:" + inputText); // REMOVE ME

            try {
            new Frame2Time("Twitter Farmer - Tweet-farm over desired time", Integer.parseInt(timeText.getText()));
            } catch (NumberFormatException error) {
                //ADD INVALID NUMBER WINDOW 
            }

        }
    });

}

}

Firstly, all comment are simply to remind me of things to do later in the code.

Secondly, assume that launchGUI.setMyDefaults(this, 800, 300);is just my method to set the size and the visibility and all that, and it works for that for other windows.

Thirdly, assume that the new Frame2Time and new Frame3Tweets are both functional.

My question is thus:

Why does timeText.addActionListener create a new window, and twitterText.addActionListener does not?

I've tried to have twitterText.addActionListener make other windows too after a number has been given by the user, without any such window popping up.

Could anyone show me what I've done wrong, and preferably, how to fix it?

Maroun
  • 94,125
  • 30
  • 188
  • 241
ViRALiC
  • 1,419
  • 4
  • 18
  • 46
  • 3
    That's a bad practice to leave `catch` blocks empty. You're probably getting an *exception* but you don't know you are, since you're catching it and do nothing with it. Not even displaying it. – Maroun Feb 18 '14 at 06:15
  • Does the main frame close when either field is actioned? – MadProgrammer Feb 18 '14 at 06:16
  • 1
    Ah, you know, there is no better way to learn than to teach yourself. I took your advice @MarounMaroun and added an error.printStackTrace() which gave me all the answers. I'll answer my own question in a bit! Thank you! – ViRALiC Feb 18 '14 at 06:20
  • 1
    See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Feb 18 '14 at 07:08

1 Answers1

1

By taking Maroun Maroun's advice, and adding an error.printStackTrace() I was able to deduct my issue:

try {
   new Frame3Tweets("Twitter Farmer - Tweet-farm by desired amount", Integer.parseInt(timeText.getText()));
} catch (NumberFormatException error) {
   error.printStackTrace(); 
}

The method was taking timeText.getText() as an argument, which was empty and thus forced everything to crash and burn.

It should have been taking tweetText.getText() instead, which had a value.

So kids, follow Maroun Maroun's advice, and don't leave your catch blocks blank!

Maroun
  • 94,125
  • 30
  • 188
  • 241
ViRALiC
  • 1,419
  • 4
  • 18
  • 46