Search code examples
javaoopmethodsjtextfieldsettext

Create Text in JText Field that user can not edit in java


I am creating a game similar to the star wars game sabacc. I am trying to create a Jtextfield that has three card suites already on the screen. The user will press a button and depending on the button they press the card suit will change to a different suit. If they get three of the same suites they win. I am having trouble getting text onto the screen though. As of right now I keep getting an error saying non static method can not be referenced by a static content.

Here is my code for the main application :

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

public class CardApp extends JFrame implements ActionListener {

    private JButton oneButton, 
                    twoButton, 
                    threeButton;  
    private int width = 25;
    private int height = 15;



    public CardApp() {
        //JPanel boardPanel = new JPanel(new GridLayout(height,width));
        JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
        JTextField TextField = new JTextField(30); 

        Hand settingTheText = new Hand();

        TextField.setText(settingTheText.ListOfCards());

        oneButton = new JButton("1");
        twoButton = new JButton("2");
        threeButton = new JButton("3");


        // Listen for events on each button
        oneButton.addActionListener(this);
        twoButton.addActionListener(this);
        threeButton.addActionListener(this);

        // Add each to the panel of buttons
        buttonPanel.add(oneButton); 
        buttonPanel.add(twoButton); 
        buttonPanel.add(threeButton); 
       // Add everything to a main panel attached to the content pane
        JPanel mainPanel = new JPanel(new BorderLayout());
        getContentPane().add(mainPanel);
        mainPanel.add(TextField, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        setTitle("Sabacc Example by Angela Rucci");
        setSize(375, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    public void actionPerformed(ActionEvent e) {
                int pressed = 0;
                if (e.getSource() == oneButton){
                        pressed = 1;}
                if (e.getSource() == twoButton){
                        pressed = 2;}
                if (e.getSource() == threeButton){
                        pressed = 3;}
                 Hand handObject = new Hand();

///This IS WHERE IM GETTING MY ERROR!//

                String screenText = handObject.ListOfCards();
                TextField.setText(screenText);
    }


public static void main(String[] args) {

    CardApp c = new CardApp();


}

}

This is the other file where i am getting my list of suits

package cardapp;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Hand {
String [] Suits = {"C", "H", "S", "D"};
String [] probability = {"C","H","R","D"};
Random randomInt = new Random ();
String RandomSuit;
String RandomShuffle;
String ThreeSuits;
String LeftSuit;
String MiddleSuit;
String RightSuit;
int pressed = 0;



       public int Discards(int pressedNumber){

              return pressed;

             }



       public void Randomizer (){

           int RandomSuitNumber = randomInt.nextInt(4);//this is generator a random number

           //------------------Decide what hand to randomize --------------------------//
           if (pressed==1){
                  LeftSuit= Suits[RandomSuitNumber];
                  }

              if (pressed==2){
                 MiddleSuit=Suits[RandomSuitNumber];
                  }

              if (pressed==3){
                  RightSuit=Suits[RandomSuitNumber];
                    }
          //----------------20% chance of new random set------------------------------------//
            int ProabilityRandomNum = randomInt.nextInt(5);//this will create a random number for probability array
            RandomShuffle= probability[ProabilityRandomNum];//this will pick a random letter in proability array


          //------------If proability array equals R then change all of the suits----------//  
            if (RandomShuffle.equals("R")){
                JOptionPane.showMessageDialog(null, "Randomized Hand!");
                int leftNumber = randomInt.nextInt(4);
                int middleNumber = randomInt.nextInt(4);
                int rightNumber = randomInt.nextInt(4);
                LeftSuit= Suits[leftNumber];
                MiddleSuit= Suits[middleNumber];
                RightSuit= Suits[rightNumber];}

            ThreeSuits = (LeftSuit + MiddleSuit + RightSuit); 
       }


       public String ListOfCards (){
              return ThreeSuits;
             }




         public void GameOver(){
                  if (LeftSuit == MiddleSuit && MiddleSuit == RightSuit &&    


    RightSuit== LeftSuit){
                      JOptionPane.showMessageDialog(null, "WINNER!!");
                     }
             }
    }

Solution

  • The variables are local to the method. the JTextField TextField is visible to the CardApp() only. if you want it to be available to the whole class, put it as a private class member :

    public class CardApp extends JFrame implements ActionListener {
    
        private JButton oneButton, 
                        twoButton, 
                        threeButton;  
        private int width = 25;
        private private int height = 15;
        // available to all methods
        // better naming convention was JTextfield tf = new JTextField(30);
        // even stackoverflow thinks its a class name :)
        // see the color highlighting
        private JTextField TextField = new JTextField(30);
    
        public CardApp() {
            //JPanel boardPanel = new JPanel(new GridLayout(height,width));
            JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
            //JTextField TextField = new JTextField(30); 
    
            Hand settingTheText = new Hand();
    
            TextField.setText(settingTheText.ListOfCards());
         }
         //
         // code continues here ...
         //
    }