Solve Java GUI Switch Problem: Create JLabels & JTextFields

In summary: JTextField) mPanel.findFirstElement(JTextField.class); num2 = (JTextField) mPanel.findFirstElement(JTextField.class); num3 = (JTextField) mPanel.findFirstElement(JTextField.class); num4 = (JTextField) mPanel.findFirstElement(JTextField.class); equals = (J
  • #1
smilesofmiles
19
0
This is actually Java!

My current problem: I need my switch statement to show only the given information for each selected shape.

For example when I click "circle" on the drop down menu, I want my switch to ask for the radius only while showing my compute button and answer.
When I click "triangle" on the drop down menu, I want my switch statement to ask for the base and height ect. ect.

I eventually want to be able to click the compute button to display that selected shape. The numbers entered from the user are dummy values. My compute button would just link to a drawASquareThing(), drawACircleThing(), drawATriangleThing() method.

I need my switch statement to work before I can achieve this though hahaha.

THANK YOU FOR ANY AND ALL HELP.Edits: I added more things to my code but I still can't figure out how to make it exit the switch statements to show each drop down option separately.
I still need advice. :(
I also tried to use return statements but they didn't help.My App

Code:
/*
A GUI that uses a JComboBox to prompt 
the user for the type of shape they would like to create (circle, square, or triangle), 
and then uses JLabel and JTextField objects to prompt them for measurements that are appropriate 
for that shape (i.e. radius for circle, side length for square, base & height for triangle).  
Your GUI should only include appropriate prompts (e.g. don't ask for radius if the user selects 
a triangle). You may assume that the triangle is a right triangle (Links to an external site.).
2) A display of the shape described by the user including a graphic of the shape,
 labelled measurements, and the calculated area.  Note: Shapes do not need to be drawn to scale.
*/
public class App {

    public static void main(String[] args) {
        MainFrame mFrame = new MainFrame();
    }
    
}

My MainFrame.java
Code:
import javax.swing.JFrame;

public class MainFrame extends JFrame {
    MainFrame()
    {
        super("Let's make shapes"); 
        MainPanel mPanel = new MainPanel(); // Special area that adds different components
        getContentPane().add(mPanel); // Contains the panel
        setSize(500,400);
       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Sets behavior of the exit button! :-) Close window / close program.
        
        setVisible(true); // Shows us the program :-)

    }
}

My MainPanel.java

Code:
import java.awt.event.ActionEvent;
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;

public class MainPanel extends JPanel implements ActionListener{ 
    

            JTextField num1;
            JTextField num2;
            JTextField num3;
            JTextField num4;
            JLabel equals;
            JTextField answer;
            JLabel labelTxt;
            JComboBox shapeList;
            JButton drawAThing;
            JLabel radiusLabel;
            JLabel base;
            JLabel lengthLabel;
            JLabel height;
            JButton drawACircle;
            JButton drawASquare;
            JButton drawATriangle;
            
    MainPanel(){
        
        String [] shapes = {"circle", "square", "triangle"};
        shapeList = new JComboBox(shapes);
        shapeList.setSelectedIndex(2);
        shapeList.addActionListener(this); 
        add(shapeList); 
       
        
    }
    
            @Override
    public void actionPerformed(ActionEvent e) {
        Object o = e.getSource();
        
        if (o == shapeList){
            JComboBox cb = (JComboBox)e.getSource();
            String msg = (String)cb.getSelectedItem();
            
           
            switch(msg){
            case "circle": 
                radiusLabel = new JLabel();
                radiusLabel.setText("Radius: ");
                add(radiusLabel);
                  
                num1 = new JTextField(12);
                add(num1);
               
                
                drawACircle = new JButton("Draw a circle ");
                drawACircle.addActionListener(this);
                add(drawACircle);
                
                if (o == drawACircle){
                        
                        drawACircleThing();
  
                    }
                
                remove(drawACircle);
        
                this.revalidate();
                this.repaint();
                
                break;
                
            case "square":
                    lengthLabel = new JLabel();
                    lengthLabel.setText("Side Length: ");
                    add(lengthLabel);
                     
                    num2 = new JTextField(12);
                    add(num2);
                    
                    
                    drawASquare = new JButton("Draw a square ");
                    drawASquare.addActionListener(this);
                    add(drawASquare);
                    
                    if (o == drawASquare){
                        
                    drawASquareThing();
                    remove(drawASquare);
        
                    this.revalidate();
                    this.repaint();
                    }
                    this.revalidate();
               break;
               
            case "triangle":
                    base = new JLabel();
                    base.setText("Base: ");
                    add(base);
                     
                    num3 = new JTextField(12);
                    add(num3);
                    
                    height = new JLabel();
                    base.setText("height: ");
                    add(height);
                     
                    num4 = new JTextField(12);
                    add(num4);
                    
                    
                    drawATriangle = new JButton("Draw a triangle ");
                    drawATriangle.addActionListener(this);
                    add(drawATriangle);
                    
                    if (o == drawATriangle){
                        
                        drawATriangleThing();
  
                    remove(drawATriangle);
        
                    this.revalidate();
                    this.repaint();
                    
                    }
                    this.revalidate();
                    break;
                    
                    
                 default:
                    break;
                     
        }
    }
    }
    
            @Override
public void paintComponent(Graphics g) 
{
    g.setColor(Color.BLUE ); 
    g.fillRect(50, 50, 200, 200);
    g.fillOval(100,100,200,200);

}
private void drawASquareThing()
{
	Graphics g = getGraphics();
	g.setColor(Color.yellow);
	g.fillRect(150,100,330,200);    
        g.drawString("My Triangle", 50, 10); 
        
}
private void drawATriangleThing()
{
	Graphics g = getGraphics();
	g.setColor(Color.yellow);
	g.fillRect(150,100,330,200);    
        g.drawString("My Triangle", 50, 10); 
        
}

public void drawACircleThing()
{
    Graphics g = getGraphics();
    g.setColor(Color.ORANGE);
    
    int [] xPoints = {50, 50, 100};
    int [] yPoints = {50, 100, 100};
    
    g.drawPolygon(xPoints, yPoints, 3);
    g.fillPolygon(yPoints, xPoints, 3);
      
}}
Things I've tried:
Code:
//    Object o = e.getSource(); // Holds who clicked
//        
//         if (o == shapeList)
//        {
//            String action = e.getActionCommand();
//
//        if (action.equals("circle")) {
//
//            radiusLabel = new JLabel();
//                    radiusLabel.setText("Radius: ");
//                    add(radiusLabel);
//                  
//                    num1 = new JTextField(12);
//                    add(num1);
//                  
//        }

//            int shapeCase = shapeList.getSelectedIndex();
//           
//            switch (shapeCase){
//                 case 1:
//                    radiusLabel = new JLabel();
//                    radiusLabel.setText("Radius: ");
//                    add(radiusLabel);
//                  
//                    num1 = new JTextField(12);
//                    add(num1);
//                  
//                 break;
//                 case 2:
//                    JLabel lengthLabel = new JLabel();
//                    lengthLabel.setText("Side Length: ");
//                    add(lengthLabel);
//                     
//                    num2 = new JTextField(12);
//                    add(num2);
//                break;
//                 default:
//                     break;
//            }
            //shapeSelected.setText((String) shapeList.getSelectedItem()); //You can cast this!
        }
//if (o == drawAThing) 
//{
//	drawSquare();
//}
}
}

I've also tried moving the break statements to different positions, casting my array list to a integer type and checking to see if my variables are case sensitive.
 
Last edited:
Technology news on Phys.org
  • #2
I also tried adding return statements but it still won't show the separate drop down options. I also took out my drawSquare() method to see if that would help but it didn't. I'm at a loss now.
 

FAQ: Solve Java GUI Switch Problem: Create JLabels & JTextFields

How do I create JLabels and JTextFields in Java GUI?

To create JLabels and JTextFields in Java GUI, you can use the JLabel and JTextField classes from the javax.swing package. You can create an instance of these classes and add them to your GUI using a layout manager.

How do I add functionality to my JLabels and JTextFields?

You can add functionality to your JLabels and JTextFields by adding event listeners to them. These event listeners can listen for user interactions such as button clicks or text input and perform the desired actions.

How do I switch between different JLabels and JTextFields in my Java GUI?

To switch between different JLabels and JTextFields in your Java GUI, you can use the CardLayout class from the java.awt package. This class allows you to switch between different components by showing or hiding them.

Can I customize the appearance of my JLabels and JTextFields?

Yes, you can customize the appearance of your JLabels and JTextFields in your Java GUI. You can use methods such as setForeground() and setFont() to change the color and font of your labels and text fields respectively.

How do I handle user input in my JTextFields?

To handle user input in your JTextFields, you can use the getText() method to retrieve the text entered by the user. You can then use this text to perform any necessary calculations or operations.

Similar threads

Replies
2
Views
1K
Replies
5
Views
2K
Replies
4
Views
4K
Replies
1
Views
2K
Replies
6
Views
2K
Replies
6
Views
2K
Replies
2
Views
3K
Replies
2
Views
4K
Replies
2
Views
4K
Back
Top