-2

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();


    }

}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson May 22 '14 at 23:25

2 Answers2

1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Well i only added 3 of them because i was adding them one by one to set the location. The reason i am not using a layout manager is because i will have textfields in very odd places in my program as it is a calculator of sorts. Even with a layoutmanager i still get the exact same issue. – TheGoatHerder May 22 '14 at 18:40
  • Actually, i fixed it, turns out my jpanels were interfering with one another – TheGoatHerder May 22 '14 at 19:20
  • `Actually, i fixed it, turns out my jpanels were interfering with one another` - because you are using null layout. That NOT a fix. Use Swig the way it was designed to used. That is use layout managers! – camickr May 22 '14 at 19:26
  • Yeah, i threw out the null and used a gridbag layout instead. Sorry, i am very new at this. – TheGoatHerder May 23 '14 at 20:31
  • @TheGoatHerder, then you should accept the answer, so people know the problem has been solved. – camickr May 23 '14 at 20:39
0
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;


public class DiffuserCalc {
    //create the class data fields
    private double qts;
    private double qes;
    private double vas;
    private JFrame programBounds = new JFrame();
    private JLabel label1= new JLabel("Qts");
    private JLabel label2= new JLabel("Qes");
    private JLabel label3= new JLabel("Fs");
    private JLabel label4= new JLabel("BL");
    private JLabel label5= new JLabel("Xmax");
    private JLabel label6= new JLabel("Fs");
    private JLabel label7= new JLabel("Vas");
    private JLabel label8= new JLabel("Diameter");
    private JLabel label9= new JLabel("Pmax (RMS)"); 
    private JTextField qtsParam = new JTextField("Value");
    private JTextField qesParam = new JTextField("Value");
    private JTextField fsParam = new JTextField("");
    private JTextField bLParam = new JTextField("");
    private JTextField xmaxParam = new JTextField("");

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

    DiffuserCalc()
    {
        programBounds.setTitle("Box Designer");
        JPanel parameterMenu = new JPanel();
        JPanel labelPanel = new JPanel();
        JPanel fieldInputs = new JPanel();

        // LAYOUT
        programBounds.setLayout(new BorderLayout());
        parameterMenu.setLayout(new BorderLayout());
        fieldInputs.setLayout(new FlowLayout(FlowLayout.LEFT));
        fieldInputs.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        labelPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        labelPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);


        programBounds.add(parameterMenu, BorderLayout.NORTH);
        parameterMenu.add(labelPanel, BorderLayout.NORTH);
        parameterMenu.add(fieldInputs, BorderLayout.SOUTH);
//        programBounds.add(fieldInputs);
        programBounds.setSize(1280,720);

        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);

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

        parameterMenu.setBorder(BlackBorder);

        qtsParam.setColumns(3);
        fieldInputs.add(qtsParam);
        qesParam.setColumns(3);
        fieldInputs.add(qesParam);
        fsParam.setColumns(2);
        fieldInputs.add(fsParam);
        bLParam.setColumns(2);
        fieldInputs.add(bLParam);
        xmaxParam.setColumns(2);
        fieldInputs.add(xmaxParam);

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

        DiffuserCalc MainProgram = new DiffuserCalc();


    }
}

So I rewrote the class for you to be more according to the Java style standard. Next not using a Layout manager is asking for trouble. And even though your requirements are like you say it's still better to put the effort to use a Layout manager as you'll keep running into problems like these. Read more about layout managers here. Furthermore don't call setVisible on JPanels that you added to a frame. When you call setVisible on the JFrame it will call setVisible on all of it child components.

Next call the method setColumns on the JTextField instead of initializing it with spaces for more consistent and predictable behaviour.

Joel Witteveen
  • 387
  • 1
  • 9
  • Okay, cool, i had no idea about any of that. I know not using layout manager is bad but unfortunately it it the only way i was taught in my class. Thank you for the response though. I fixed it by usiing GridBagLayout for my components – TheGoatHerder May 23 '14 at 20:33
  • `Furthermore don't call setVisible on JPanels` correct that is not necessary since all Swing components (other than top level containers) are visible by default. `When you call setVisible on the JFrame it will call setVisible on all of it child components.` - no it wont. Swing components are visible by default. – camickr May 23 '14 at 20:41