Search code examples
javaswingjtextfield

JTextField Does not show up initially, only when clicking on it?


So this problem is really giving me a headache because for some reason last night when i was working on it, my code ran perfectly and my textfields would show up without a problem...

Go to bed, wake up, time to work on it again aaaaaand bam. Now my JtextFields only show up when i highlight them or click them or something...I was wondering what could be wrong?

My code is really just messy and crappy at this point while i figure out a better way to design my program...

I thought it was just eclipse but netbeans is giving me the same issue.

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.awt.*;
import javax.swing.border.*;


public class DiffuserCalc {
    //create the class data fields
    private double Qts;
    private double Qes;
    private double Vas;
    JFrame ProgramBounds = new JFrame();
    JLabel label1= new JLabel("Qts");
    JLabel label2= new JLabel("Qes");
    JLabel label3= new JLabel("Fs");
    JLabel label4= new JLabel("BL");
    JLabel label5= new JLabel("Xmax");
    JLabel label6= new JLabel("Fs");
    JLabel label7= new JLabel("Vas");
    JLabel label8= new JLabel("Diameter");
    JLabel label9= new JLabel("Pmax (RMS)"); 
    JTextField QtsParam = new JTextField("Value");
    JTextField QesParam = new JTextField("Value");
    JTextField FsParam = new JTextField("   ");
    JTextField BLParam = new JTextField("   ");
    JTextField XmaxParam = new JTextField("   ");

    Font myFont = new Font("Tahoma", Font.BOLD, 20);

    DiffuserCalc()
    {
        ProgramBounds.setTitle("Box Designer");
        JPanel ParameterMenu = new JPanel();
        JPanel FieldInputs = new JPanel();


        ParameterMenu.setBounds(30, 0, 1180, 120);
        FieldInputs.setBounds(0,0, 1280, 720);
        ProgramBounds.add(ParameterMenu);
        ProgramBounds.add(FieldInputs);
        ProgramBounds.setSize(1280,720);



        // LAYOUT

        ParameterMenu.setLayout(new FlowLayout(FlowLayout.CENTER, 60, 10));

        FieldInputs.setLayout(null);

        Border lineBdr = BorderFactory.createLineBorder(Color.BLACK);
        Border BlackBorder = BorderFactory.createTitledBorder(lineBdr, "    T/S Parameters    ", TitledBorder.CENTER, TitledBorder.TOP, myFont, Color.black);

        //FIELD PROPERTIES

        label1.setFont(myFont);
        label2.setFont(myFont);
        label3.setFont(myFont);
        label4.setFont(myFont);
        label5.setFont(myFont);
        label6.setFont(myFont);
        label7.setFont(myFont);
        label8.setFont(myFont);
        label9.setFont(myFont);

        // PARAMETER BOUNDS
        int XLoc = 150;
        int YLoc = 70;
        QtsParam.setBounds(XLoc, YLoc, 40, 20);
        QesParam.setBounds(XLoc+95, YLoc, 40, 20);
        FsParam.setBounds(XLoc+190, YLoc, 40, 20);



        // ADD FIELDS
        ParameterMenu.add(label1);
        ParameterMenu.add(label2);
        ParameterMenu.add(label3);
        ParameterMenu.add(label4);
        ParameterMenu.add(label5);
        ParameterMenu.add(label6);
        ParameterMenu.add(label7);
        ParameterMenu.add(label8);
        ParameterMenu.add(label9);

        ParameterMenu.setBorder(BlackBorder);

        FieldInputs.add(QtsParam);
        FieldInputs.add(QesParam);
        FieldInputs.add(FsParam);
        FieldInputs.add(BLParam);
        FieldInputs.add(XmaxParam);




        // set everything proper
        QtsParam.requestFocus();
        ParameterMenu.setVisible(true);
        FieldInputs.setVisible(true);
        ProgramBounds.setVisible(true);
    }
    public double BoxDimension(int x, int y)
    {
        return x;
    }
    public static void main(String[] args) {

        DiffuserCalc MainProgram = new DiffuserCalc();


    }

}

Solution

  • Your code only sets the bounds for 3 text fields but you add 5 text fields to the panel.

    Don't use a null layout!!!

    Use a proper layout manager and then you don't have to worry about making mistakes like this.

    Also, follow Java naming conventions. Variable names do NOT start with an upper case character.