Search code examples
javaswingjtextfield

JTextField not visible until mouse over


I am trying to make a simple calculator program with Java. When I added a JTextField however it made all the button and the field itself invisible until I hover over it. If I comment out the text field everything goes back to normal and all the button are visible.

Here is my code:

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

public class Calculator extends JFrame {

// Numbers
JButton btn_zero;
JButton btn_one;
JButton btn_two;
JButton btn_three;
JButton btn_four;
JButton btn_five;
JButton btn_six;
JButton btn_seven;
JButton btn_eight;
JButton btn_nine;

// Operators
JButton btn_add;
JButton btn_subtract;
JButton btn_multiply;
JButton btn_divide;
JButton btn_equals;
JButton btn_decimal;
JButton btn_pm;
JButton btn_clear;

// Panel
JPanel buttonPanel;

// Dimensions
final int WIDTH = 340;
final int HEIGHT = 500;

public Calculator() {
    // Characteristics of frame
    super("Calculator");
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Insets frameInsets = getInsets();
    int frameWidth = WIDTH + (frameInsets.left + frameInsets.right);
    int frameHeight = HEIGHT + (frameInsets.top + frameInsets.bottom);
    setPreferredSize(new Dimension(frameWidth, frameHeight));
    //setLayout(null);
    pack();
    setVisible(true);


    // Add values to all buttons
    btn_zero = new JButton("0");

    btn_one = new JButton("1");
    btn_two = new JButton("2");
    btn_three = new JButton("3");
    btn_four = new JButton("4");
    btn_five = new JButton("5");
    btn_six = new JButton("6");
    btn_seven = new JButton("7");
    btn_eight = new JButton("8");
    btn_nine = new JButton("9");
    btn_add = new JButton("+");
    btn_subtract = new JButton("-");
    btn_multiply = new JButton("×");
    btn_divide = new JButton("÷");
    btn_equals = new JButton("=");
    btn_decimal = new JButton(".");
    btn_pm = new JButton("±");
    btn_clear = new JButton("C");

    // Adds the panel
    buttonPanel = new JPanel();
    buttonPanel.setSize(new Dimension(frameWidth, frameHeight));
    buttonPanel.setLayout(null);

    // Textfield
    JTextField AnswerBox = new JTextField ("");
    AnswerBox.setBounds(0, 0, 320, 70);
    buttonPanel.add(AnswerBox); 

    //Buttons
    btn_decimal.setBounds(70, 100, 50, 50);
    buttonPanel.add(btn_decimal);
    btn_pm.setBounds(130, 100, 50, 50);
    buttonPanel.add(btn_pm);
    btn_clear.setBounds(190, 100, 50, 50);
    buttonPanel.add(btn_clear);
    btn_add.setBounds(250, 100, 50, 50);
    buttonPanel.add(btn_add);
    btn_subtract.setBounds(250, 160, 50, 50);
    buttonPanel.add(btn_subtract);
    btn_multiply.setBounds(250, 220, 50, 50);
    buttonPanel.add(btn_multiply);
    btn_divide.setBounds(250, 280, 50, 50);
    buttonPanel.add(btn_divide);
    btn_equals.setBounds(10, 350, 290, 50);
    buttonPanel.add(btn_equals);
    btn_zero.setBounds(190, 160, 50, 170);
    buttonPanel.add(btn_zero);
    btn_one.setBounds(10, 160, 50, 50);
    buttonPanel.add(btn_one);
    btn_two.setBounds(70, 160 , 50, 50);
    buttonPanel.add(btn_two);
    btn_three.setBounds(130, 160, 50, 50);
    buttonPanel.add(btn_three);
    btn_four.setBounds(10, 220, 50, 50);
    buttonPanel.add(btn_four);
    btn_five.setBounds(70, 220, 50, 50);
    buttonPanel.add(btn_five);
    btn_six.setBounds(130, 220, 50, 50);
    buttonPanel.add(btn_six);
    btn_seven.setBounds(10, 280, 50, 50);
    buttonPanel.add(btn_seven);
    btn_eight.setBounds(70, 280, 50, 50);
    buttonPanel.add(btn_eight);
    btn_nine.setBounds(130, 280, 50, 50);
    buttonPanel.add(btn_nine);

    buttonPanel.setVisible(true);
    add(buttonPanel);

}
}

Also the code is also right now just showing the buttons of the calculator, and not actually doing anything, because I want to focus on fixing this bug.


Solution

  • Call revalidate() after you add all the widgets to recalculate the layout of the container (in your case a JFrame):

    buttonPanel.setVisible(true);
    add(buttonPanel);
    revalidate();