Search code examples
javaswingjframejtextfield

Learning Swing and Jframe and stuff - also would like some tips and tricks on organization


I'm learning swing for my Java program. I'm very new at it and I'm slowly getting the hang of it.

My code here is very basic; I'm using an action listener to read the text that is entered into a JTextField, but my code is giving me an error that says "CreditGraphics.java:32: error: non-static variable this cannot be referenced from a static context text.addActionListener(this)", I'm not finding very many helpful solutions to this on the forums, at least no solution that suits my style of coding. Why is it giving me this error so I can avoid it for future reference?

Also, I'd love any organizational tips that you guys would like to give me.

I am trying to make a graphics application that can take in a credit card application through a JTextField and then run that through my already-made program that checks the validity of the card.

Thanks!

Here is my code:

    import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//if card is entered incorrectly reset textfield by doing textfield.setText(""); 

public class CreditGraphics implements ActionListener{

   public static String cardNum;
   public static JFrame frame;
   public static JPanel panel;
   public static JLabel label;
   public static JTextField text;

   public static void main(String[] args){
      frame = new JFrame("HI");
     panel = new JPanel();
     label = new JLabel("Welcome to MES Banking!");

  text = new JTextField();

  panel.add(label);
  panel.add(text);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  panel.setPreferredSize(new Dimension(500,500));
  frame.getContentPane().add(panel);
  frame.pack();
  frame.setVisible(true);

  text.addActionListener(this);

   }

   public void actionPerformed(ActionEvent e){
      cardNum = text.getText();
   }


}

Solution

  • Main Problem:

    this meaning the class, is not static, and it can't be since it's the main class. So it can't be accessed from a static context, which is the main method.

    Don't write all your code in the main method. You'll face this problem alot. Do your ui construction in the constructor, or in an init method. Then just call that method or constructor in the main

    Other Points:

    • Run your swing apps on the Event Dispatch Thread (EDT). See more at Initial Threads.(Also See below for example).

    • Personally, I wouldn't make the class implement ActionListener. I would an instance of a listener to the button. (See below)


    UPDATE

    Here's a refactor with the points above

    import java.awt.Dimension;
    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;
    import javax.swing.SwingUtilities;
    
    public class CreditGraphics {
    
        public String cardNum;
        public JFrame frame;
        public JPanel panel;
        public JLabel label;
        public JTextField text;
    
        public CreditGraphics() {
            frame = new JFrame("HI");
            panel = new JPanel();
            label = new JLabel("Welcome to MES Banking!");
    
            text = new JTextField();
    
            panel.add(label);
            panel.add(text);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            panel.setPreferredSize(new Dimension(500, 500));
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setVisible(true);
    
            text.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    cardNum = text.getText();
                }
            });
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new CreditGraphics();
                }
            });
        }
    }