Search code examples
javaswinguser-interfacejtextfieldgettext

jTextField and Jpanel color output


Create a program where you can enter a colour name into a textfield then use that colour to set the panel background colour with the colour specified in the text field Im having trouble getting the output

import javax.swing.*;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingUtilities;

public class textfield implements  ActionListener{

    JTextField Input;
    JPanel color;
    JButton Button;

    public JPanel createContentPane (){

        // We create a bottom JPanel to place everything on.
        JPanel totalGUI = new JPanel();
        totalGUI.setLayout(null);

        // Creation of a Panel to contain the JLabels
        Input = new JTextField ("color", 35);
        Input.setBackground (Color.white);
        Input.setLocation(50,50);
        Input.setSize(100, 30);
        Input.addActionListener (this);
        totalGUI.add(Input);

        color = new JPanel();
        color.setBackground (Color.white);
        color.setLocation(200, 50);
        color.setSize(200, 100);
        totalGUI.add(color);


        totalGUI.setOpaque(true);
        return totalGUI;
    }

    public void actionPerformed(ActionEvent e) {

        String text = Input.getText();
        { if (text == "green")
            color.setBackground(color.green);
        }
    }

    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Assignment");

        textfield demo = new textfield();
        frame.setContentPane(demo.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 300);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Solution

  • There are a few problems.

    1. You should compare equality of strings with equals method and not with ==.

    2. Change color.green to Color.GREEN

      public void actionPerformed(ActionEvent e) {
      
          String text = Input.getText();
          if (text.equals("green"))
          {
              color.setBackground(Color.GREEN);
          }
      }
      

    For more than one color, you can use if-else or switch case

    Using if-else:

       public void actionPerformed(ActionEvent e) {
    
            String text = Input.getText();
            if (text.equals("green"))
            {
                color.setBackground(Color.GREEN);
            }
            else if(text.equals("red"))
            {
                color.setBackground(Color.RED);
            }
            else if(text.equals("yellow"))
            {
                color.setBackground(Color.YELLOW);
            }
            else
            {
                color.setBackground(Color.BLUE);
            }
    
        }
    

    Using switch case:

    public void actionPerformed(ActionEvent e) {
    
        String text = Input.getText();
    
        switch (text) {
            case "green":
                color.setBackground(Color.GREEN);
                break;
            case "red":
                color.setBackground(Color.RED);
                break;
            case "yellow":
                color.setBackground(Color.YELLOW);
                break;
            case "blue":
                color.setBackground(Color.BLUE);
                break;
    
        }
    
    }