Integer returning methods make components invisible in JPanel

Viewed 198

I have a very weird problem. I am writing an interface for a paint-like program and created a class that extends JLabel. I have added the base components and needed variables as shown below:

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

public class denemePanel extends JPanel {

    int radius;
    int height;
    int width;
    String color;
    JButton drawButRect;
    JButton drawButCirc;
    JButton drawButRand;
    JButton paintBut;
    JLabel heightL;
    JLabel widthL;
    JLabel radiusL;
    JLabel colorL;
    JTextField colorF;
    JTextField heightF;
    JTextField widthF;
    JTextField radiusF;
    ShapeContainer shapes;
    boolean drawRect;
    boolean drawCirc;
    boolean drawRand;
    boolean isColor;
    ButtonListener buttonListener;

    public denemePanel( ShapeContainer shapes) {

        height = -1;
        radius = -1;
        width = -1;
        color = "";
        drawRect = false;
        drawCirc = false;
        drawRand = false;
        isColor = false;
        buttonListener = new ButtonListener();
        setLayout( new GridLayout( 0,7, 5, 5));
        setPreferredSize( new Dimension( 700, 150));
        shapes = this.shapes;
        drawButRect = new JButton( "Draw!");
        drawButCirc = new JButton( "Draw!");
        drawButRand = new JButton( "Draw any!");
        paintBut = new JButton( "Paint!");
        drawButRect.addActionListener( buttonListener);
        drawButCirc.addActionListener( buttonListener);
        drawButRand.addActionListener( buttonListener);
        paintBut.addActionListener( buttonListener);
        heightL = new JLabel( "Height:");
        widthL = new JLabel( "Width:");
        radiusL = new JLabel( "Radius:");
        colorL = new JLabel( "Color:");
        heightF = new JTextField( "0");
        colorF = new JTextField( "yellow/red");
        widthF = new JTextField( "0");
        radiusF = new JTextField( "0");
        add( heightL);
        add( heightF);
        add( radiusL);
        add( radiusF);
        add( new JLabel( ""));
        add( colorL);
        add( colorF);
        add( widthL);
        add( widthF);
        add( new JLabel( ""));
        add( new JLabel( ""));
        add( new JLabel( ""));
        add( new JLabel( ""));
        add( new JLabel( ""));
        add( new JLabel( ""));
        add( drawButRect);
        add( new JLabel( ""));
        add( drawButCirc);
        add( drawButRand);
        add( new JLabel( ""));
        add( paintBut);
        setVisible( true);
    }

    //Methods
    public int getRadius() {
        return radius;
    }
    public int getHeight() {
        return height;
    }
    public int getWidth() {
        return width;
    }
    public String getColor() {
        return color;
    }
    //Methods end

    //-------------------------------------ACTION LISTENER-----------------------------------------------
    public class ButtonListener implements ActionListener {

        public void actionPerformed( ActionEvent e) {

            if( e.getSource() == drawButRect) {
                height = Integer.parseInt( heightF.getText());
                width = Integer.parseInt( widthF.getText());
                if( height > 0 && width > 0) {
                drawRect = true;
                drawCirc = false;
                drawRand = false;
                isColor = false;
                }
                else {
                    JOptionPane.showMessageDialog(null, "Not a valid input!");
                    drawRect = false;
                    drawCirc = false;
                    drawRand = false;
                    isColor = false;
                }
            }

            else if( e.getSource() == drawButCirc) {
                radius = Integer.parseInt( radiusF.getText());
                if( radius > 0) {
                    drawRect = false;
                    drawCirc = true;
                    drawRand = false;
                    isColor = false;
                }
                else {
                    JOptionPane.showMessageDialog(null, "Not a valid input!");
                    drawRect = false;
                    drawCirc = false;
                    drawRand = false;
                    isColor = false;  
                }
            }

            else if( e.getSource() == drawButRand) {
                drawRect = false;
                drawCirc = false;
                drawRand = true;
                isColor = false;
            }

            else if( e.getSource() == paintBut) {
                color = colorF.getText().toLowerCase();
                if( color.equals( "red") || color.equals( "yellow")) {
                    drawRect = false;
                    drawCirc = false;
                    drawRand = false;
                    isColor = true;
                }
                else {
                    JOptionPane.showMessageDialog(null, "Not valid color -just red or yellow!");
                    drawRect = false;
                    drawCirc = false;
                    drawRand = false;
                    isColor = false;
                }
            }
        }
    }
    //-----------------------------------------------------------------------------------------------------------
}

Before adding the "methods" part, my code was working like this:

After adding the methods (just methods), it became like this (no components are visible but that is it, they are actually working! So, if I press a button that I know it is there, it works like it supposed to):

By doing some commenting and uncommenting, I have discovered that String method has nothing to do with it (Uncommenting it doesn't solve the problem and making just that uncommented doesn't cause one.). But the integer returning methods causes this problem both individually and together.

//Methods
public int getRadius() {
    return radius;
}
public int getHeight() {
    return height;
}
public int getWidth() {
    return width;
}
public String getColor() {
    return color;
}
//Methods end

To solve this, I added setVisible() to Panel but nothing has changed. Thank you all in advance!

2 Answers
Related