1

Been researching around and looking at many answers but have had no luck.

public class GUIinput extends JFrame{

//Num is the JTextfield variable used for later.
public JTextField num;
public JButton button;

public void test(){
           //JPanel
    JPanel panel = new JPanel();

    //Welcome JLabel..Contains the welcome message.
    JLabel welcome = new JLabel("Welcome to my Grade Calculator program!");
    panel.add(welcome);

    //Instructions J Label on how to work the program.
    JLabel instructions = new JLabel("Enter your Mark and Weight to find your average grade!");
    panel.add(instructions);

    //JLabel---How many different assignments do the user want to evaluate?
    JLabel number = new JLabel("<html><br>Firstly, please enter how many assignments/exams you want to evaluate?</html>");
    panel.add(number);

    //JTextField assigner.
    num = new JTextField(18);
    panel.add(num);


    //Enter button
    button = new JButton("Enter");
    panel.add(button);


    //JFrame maker.
    setTitle("Grade Calculator");
    setSize(575,250);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    add(panel);

    //This action listener opens the new class Gradeanalysis(). The next window.
    button.addActionListener(new Gradeanalysis());

    //Action listener for the Textfield in case the user presses the enter key.
    num.addActionListener(new Gradeanalysis());

   }


public static void main(String[] args) {
    new GUIinput().test();


    }
}

This class is for the first window of the program. The program ask the user to enter a value which gets sent to another class. (Grade analysis).

 public class Gradeanalysis extends GUIinput implements ActionListener{


@Override
    public void actionPerformed (ActionEvent e){
        int messageType = JOptionPane.PLAIN_MESSAGE;

 /*ERROR IS HERE---*/JOptionPane.showMessageDialog(null,num.getText(),"fasfa",messageType);
    JFrame frame = new JFrame("Grade Calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(createMainPanel());
    frame.pack();
    frame.setVisible(true);



}

Now I cut a lot of code out which is not needed. So what the program should do is get the user input from the first window and when the user clicks the enter button the input should be displayed on a MessageDialog screen. Not the new JFrame.

Now I understand the problem is because of that line with the asterisks espically the "num.getText()" section.

If i add "num = new JTextField();" above that line the MessageDialog screen will be blank. I want it to display the input from the previous screen.

Have no clue how to go about this. Appreciate all help, thanks.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
bob9123
  • 725
  • 1
  • 9
  • 31
  • 1
    Do you know what `new` does? – user253751 Jul 16 '15 at 01:22
  • 1
    You appear to me a very big fan of __inheritance__. If that is the case, start reading about __Composition__, and it's benefits over __Inheritance__. You extending classes in your code for no reasons. You not actually enhancing their behaviour, and moreover why `Gradeanalysis` class is extending `GUIinput`? What former class has to do with the latter? When you have `GUIinput` acting as `JFrame` then why create a new `JFrame` in `actionPerformed ( ... )`, consider `JDialog` for such situation – nIcE cOw Jul 16 '15 at 01:33
  • @immibis How will new help me out? Making a new object will just make num null/0 right? – bob9123 Jul 16 '15 at 15:09
  • @nIcEcOw My idea is that I get the variable (num) from GUIinput class. This is why I used extend. I need to get the input from the user which is in num onto the Gradeanalysis class. – bob9123 Jul 16 '15 at 15:12
  • @bob9123: Please if you can, just explain in simple english, the requirement. Like what you typed in a `JTextField`, on a click of a `JButton`, you simply want that text inside `JTextField` to be visible inside the `JOptionPane` which is a part of another class, where `actionPerformed (... )` is defined. Is that right? – nIcE cOw Jul 16 '15 at 16:33
  • Don't worry I have finally fixed that specific issue. I appreciate your help. – bob9123 Jul 16 '15 at 16:37

2 Answers2

3

Your problem lies here:

//This action listener opens the new class Gradeanalysis(). The next window.
button.addActionListener(new Gradeanalysis());

//Action listener for the Textfield in case the user presses the enter key.
num.addActionListener(new Gradeanalysis());

your adding a new Gradeanalysis which has no pointers to the textfields, and have not initialised their fields. you should change your action listener so that its in a separate class, and pass in the fields it affects

ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int messageType = JOptionPane.PLAIN_MESSAGE;

                JOptionPane.showMessageDialog(null, num.getText(), "fasfa", messageType);
                JFrame frame = new JFrame("Grade Calculator");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JPanel());//dont have create main panel method
                frame.pack();
                frame.setVisible(true);
            }
        };
        button.addActionListener(listener);

        num.addActionListener(listener);
Ash
  • 101
  • 4
  • A new class for just the pointer? So it should now be button.addActionListener(new newclassname()); – bob9123 Jul 16 '15 at 01:09
  • I think a better solution would be to assign the action listener to a variable or field before adding it to the components – Zymus Jul 16 '15 at 01:12
  • Ive added an example of an anonymous inner class that should work, though it would be better if it was separate – Ash Jul 16 '15 at 01:17
  • Thanks for the reply however this is will not work with my program. What I want to do is get the input from the GUIinput class and have it as a variable in the Gradeanalysis class. I'm assuming with your code that all of the above is only done in GUIinput class. This will be an issue because whatever is done in the GUIinput changes the way Gradeanaylsis class looks. – bob9123 Jul 16 '15 at 15:06
1

From the given code it is not clear if Gradeanalysis can see your num. You could make a new constructor for this class so that num goes in as a parameter. Rework your Gradeanalysis class like this

public class Gradeanalysis extends GUIinput implements ActionListener{

JTextField num;

public Gradeanalysis(JTextField num)
{
    this.num = num;
}
//Implement the actionPerformed here as u have..
}

Then call Gradeanalysis from your code for example like this

button.addActionListener(new Gradeanalysis(num));

Now I will note that the action performed might not work as you expect for the TextField and that a swing application should only have one JFrame, make the others JDialogs or something.

Community
  • 1
  • 1
milez
  • 2,201
  • 12
  • 31
  • Thanks for your answer but this still did not get the input from previous screen. Don't think it can still see the updated num. – bob9123 Jul 16 '15 at 14:41
  • 1
    Yeah there is a mistake in my code, still no parameter on Gradeanalysis. Did u use new Gradeanalysis(num) ? – milez Jul 16 '15 at 15:14
  • Yeah I did put in num as without it the program will not run. It runs when put in but the input is not displayed on the screen. Been researching how to fix this for so long but keep failing haha :/ – bob9123 Jul 16 '15 at 15:16
  • 1
    Then I think you'll have to post more code and the error message :\ – milez Jul 16 '15 at 15:20
  • There is no error message though, just does not display the input on the screen – bob9123 Jul 16 '15 at 15:24