Search code examples
javajtextfield

JTextField.getText() returning null value and radiobuttons.isSelected() always returns false


2 different bugs, and I have no idea what is causing either of them. They both seem to be in the actionPerformed methods. These are 3 different files, sorry for the bad formatting, I can never get it to work properly.

package A8;

import javax.swing.*;

import java.awt.*;

public class a8main {

public static final int RES_X = 600;
public static final int RES_Y = 125;

public static JFrame window;
public static JPanel panel = new JPanel();
public static RatePanel ratePanel = new RatePanel();
public static MinutesPanel minutesPanel = new MinutesPanel();

public static void main(String[]args){
    createWindow();
    populateWindow();
}

public static void createWindow(){
    window = new JFrame();
    window.setTitle("A8 Long Distance Communications");
    window.setSize(RES_X, RES_Y);
    window.setEnabled(true);
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void populateWindow(){
    ratePanel.populatePanel(panel);
    minutesPanel.populatePanel(panel);
    window.add(panel);
}

}


package A8;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MinutesPanel implements ActionListener{

JTextField field = new JTextField();
JLabel minuteLabel = new JLabel();

int callTime;
double chosenRate;

public void populatePanel(JPanel Panel){
    createPanelObjects();
    Panel.add(minuteLabel);
    Panel.add(field);
}

public void createPanelObjects(){
    chosenRate = RatePanel.chosenRate;
    field.setColumns(10);
    minuteLabel = new JLabel("Enter length of call in minutes");
    field.addActionListener(new MinutesPanel());
}
public void displayCallPrice(){
    JOptionPane dialogBox = new JOptionPane();
    double cost = RatePanel.chosenRate * callTime;
    dialogBox.showMessageDialog(null, "The cost of your call is $" + cost);

}

@Override
public void actionPerformed(ActionEvent e) {
    String callStr;
    callStr = field.getText();  //problem is here.
    //callStr = "3";
    System.out.print(callStr);
    callTime = Integer.parseInt(callStr);
    System.out.print(callTime);
System.out.print(RatePanel.chosenRate);

displayCallPrice();

}

}


package A8;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class RatePanel implements ActionListener{

    public static final double DAY_RATE = 0.07;
    public static final double EVENING_RATE = 0.12;
    public static final double OFFPEAK_RATE = 0.05;
    public static double chosenRate = DAY_RATE; //Because button defaults to day rate it is init to that rate.

    JLabel categoryLabel = new JLabel();
    JRadioButton dayButton = new JRadioButton();
    JRadioButton eveningButton = new JRadioButton();
    JRadioButton offpeakButton = new JRadioButton();
    ButtonGroup buttons = new ButtonGroup();

    public void populatePanel(JPanel Panel){
        createPanelObjects();
        Panel.add(categoryLabel);
        Panel.add(dayButton);
        Panel.add(eveningButton);
        Panel.add(offpeakButton);
    }

    public void createPanelObjects(){
        categoryLabel = new JLabel("Select a rate category");
        dayButton = new JRadioButton("Day rate: $" + DAY_RATE);
        eveningButton = new JRadioButton("Evening rate: $" + EVENING_RATE);
        offpeakButton = new JRadioButton("Offpeak rate: $" + OFFPEAK_RATE);
        dayButton.setSelected(true);    //Used to make it impossible to not select a rate, prevents bugs.
        buttons.add(dayButton);
        buttons.add(eveningButton);
        buttons.add(offpeakButton);
        dayButton.addActionListener(new RatePanel());
        eveningButton.addActionListener(new RatePanel());
        offpeakButton.addActionListener(new RatePanel());

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(dayButton.isSelected()){
            chosenRate = DAY_RATE;
            System.out.println(DAY_RATE);
        }
        if(e.getSource() == eveningButton){
            chosenRate = EVENING_RATE;
            System.out.println(EVENING_RATE);
        }
        if(e.getSource() == offpeakButton){
            chosenRate = OFFPEAK_RATE;
            System.out.println(OFFPEAK_RATE);
        }
            System.out.println(e.getSource() == eveningButton);
            System.out.println(dayButton.isSelected());
    }


}

Solution

  • You should not create a new instance of your ActionListener for every component.

    Since your panels implement ActionListener, you are creating new instances of them every time you are calling addActionListener().

    Instead of this:

    field.addActionListener(new MinutesPanel());
    
    // ...
    
    eveningButton.addActionListener(new RatePanel());
    offpeakButton.addActionListener(new RatePanel());
    

    You should do this:

    field.addActionListener(this);
    
    // ...
    
    eveningButton.addActionListener(this);
    offpeakButton.addActionListener(this);