Integer returning methods make components invisible in JPanel

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!

Answers


Change getHeight() and getWidth() method names because they are already exist in JPanel so you are overriding them.


You're not "adding" those methods, you're "Over-riding" them - the behavior already exists in the superclass (JPanel). At a glance, it looks like your implementation will return negative numbers, which causes weird behavior; the superclass has its own implementation, which was probably doing some reasonable default behavior.

If you read up on the documentation for JPanel, you can probably find out more about what that behavior was.


Need Your Help

setitem and getitem -- python

python class

I've created a python program that makes a vector. Now i want to set an item using a function__setitem__ and__getitem__. So for example, ifvector = Vec()andvector[3] = 26would change an empty vecto...

Android official bluetooth chat sample cannot connect

android

Has anybody meet such a problem? when using the bluetooth chat sample , it