Results 1 to 6 of 6
  1. #1
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Components not displaying on JFrame

    my menu bar displays but my jpanel and the components in it are not displaying when i first compile the program. I have to compile it mutiple times to get it to display. i am using the setBounds method


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


    public class IncomeTax extends JFrame {
    //Instance Variable Declaration
    private JMenuItem exit;
    private JMenuItem help;
    private JMenuItem about;
    private JTextField name;
    private JTextField salary;
    private JTextField numOfChildren;
    private JComboBox comBox;
    private JButton submit;


    //Constructor that initializes JFrame
    public IncomeTax ()
    {
    setTitle ("Income Tax Application");
    setSize (400,300);
    setVisible (true);
    setDefaultCloseOperation (EXIT_ON_CLOSE);
    setResizable (false);
    setLocation(300,200);
    setMenuBar ();
    setContent ();
    }//end constructor

    //Sets MenuBar on JFrame
    public void setMenuBar ()
    {
    JMenuBar mBar = new JMenuBar ();
    JMenu file = new JMenu ("File ");
    JMenu option = new JMenu ("Options");

    setJMenuBar(mBar);
    mBar.add(file);
    mBar.add(option);
    file.add(exit = new JMenuItem ("Exit"));
    option.add(help = new JMenuItem ("Help"));
    option.add(about = new JMenuItem ("About"));

    help.setFont(new Font("Segoe UI",Font.PLAIN,12));
    about.setFont(new Font("Segoe UI",Font.PLAIN,12));
    exit.setFont (new Font("Segoe UI",Font.PLAIN,12));
    file.setFont(new Font("Segoe UI",Font.PLAIN,12));
    option.setFont(new Font("Segoe UI",Font.PLAIN,12));

    EventHandler handlerObj = new EventHandler ();
    help.addActionListener (handlerObj);
    about.addActionListener (handlerObj);
    exit.addActionListener (handlerObj);
    }//end setMenuBar

    public void setContent ()
    {
    String[] stat ={"Married","Single"};
    name = new JTextField (12);
    salary = new JTextField (7);
    numOfChildren = new JTextField (2);
    submit = new JButton ("Submit");

    JLabel label1 = new JLabel ("Name: ");
    JLabel label2 = new JLabel ("Gross Salary: ");
    JLabel label3 = new JLabel ("# of Children: ");

    label1.setFont(new Font("Segoe UI",Font.PLAIN,12));
    label2.setFont(new Font("Segoe UI",Font.PLAIN,12));
    label3.setFont(new Font("Segoe UI",Font.PLAIN,12));

    Dimension label1Size = label1.getPreferredSize();
    Dimension label2Size = label2.getPreferredSize();
    Dimension label3Size = label3.getPreferredSize ();
    Dimension nameSize = name.getPreferredSize ();
    Dimension salarySize = salary.getPreferredSize ();
    Dimension childSize = numOfChildren.getPreferredSize ();
    Dimension btSize = submit.getPreferredSize ();

    label1.setBounds (20,20,label1Size.width,label1Size.height);
    label2.setBounds (20,45,label2Size.width,label2Size.height);
    label3.setBounds (20,70,label3Size.width,label3Size.height);
    name.setBounds(70,20,nameSize.width,nameSize.heigh t);
    salary.setBounds(90,45,salarySize.width,salarySize .height);
    numOfChildren.setBounds (100,70,childSize.width,childSize.height);
    submit.setBounds (20,110,btSize.width,btSize.height);

    JPanel panel = new JPanel ();
    panel.setBounds(50,20,270,170);
    panel.setBorder(BorderFactory.createTitledBorder(" Info"));

    comBox = new JComboBox (stat);
    comBox.setBounds (160,70,75,30);
    comBox.setEditable(false);
    comBox.setSelectedItem("Single");

    setLayout(null);
    add(panel);

    panel.setLayout(null);
    panel.add(label1);
    panel.add(name);
    panel.add(label2);
    panel.add(salary);
    panel.add(label3);
    panel.add(numOfChildren);
    panel.add(comBox);
    panel.add(submit);

    EventHandler handlerObj = new EventHandler ();
    submit.addActionListener(handlerObj);
    }//end setContent

    public static void main (String [] args)
    {
    IncomeTax window = new IncomeTax ();
    }//end main


    private class EventHandler implements ActionListener
    {
    public void actionPerformed (ActionEvent e)
    {
    if (e.getSource () == help || e.getSource () == about || e.getSource () == exit || e.getSource() == submit )
    {
    JOptionPane.showMessageDialog (null,"Not Coded");
    }
    }//end actionPerformed
    }//end EventHandler

    }//end IncomeTax class

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I'm pretty sure you have to make it visible at the very end, after adding all of the components.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    I have to compile it mutiple times to get it to display.
    There shouldn't be any difference between the output from the first compile and the output from the last compile, UNLESS you change the source between each compile. A compiler reads the source file and creates a .class file.
    You do not get any displays in that process, unless you have errors in the source.

  4. #4
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    Put setVisible(true) at the very end of the code body you used to write the class that returns the frame. So a rough pseudo-code would be:
    Java Code:
    pulblic SomeFrame()
    {
    JFrame jf = new JFrame();
    JPanel jp = new Jpanel();
    JLabel someLabel = new JLabel("Label");
    jp.add(someLabel);
    jf.add(jp);
    
    //this would be the last line of the code; there would be additional code for you to fill in above.  
    jf.setVisible(true);
    }
    Your compiler by the way shouldn't change anything unless you made a change.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Even better, move the code that makes the gui visible right out of the constructor and call it on the event dispatch thread. There are numerous examples throughout Oracle's Tutorial.

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I suggested making it visible at the end of everything. No response still...

    pbrockway2 is right though, if the OP ever returns to this thread the OP should indeed listen to this advice.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Access Child Components of JFrame?
    By PrinceSendai in forum New To Java
    Replies: 5
    Last Post: 06-22-2011, 03:18 PM
  2. Get more components from JFrame Form.
    By ZeCute in forum AWT / Swing
    Replies: 0
    Last Post: 05-09-2011, 10:46 AM
  3. JLayeredPane not displaying components
    By abc in forum AWT / Swing
    Replies: 3
    Last Post: 01-06-2011, 03:40 PM
  4. Getting and Displaying Swing Components From DataBase
    By vivekkumar in forum AWT / Swing
    Replies: 1
    Last Post: 07-27-2010, 03:32 PM
  5. Displaying Chinese characters on Swing components
    By vaskarbasak in forum AWT / Swing
    Replies: 3
    Last Post: 06-26-2008, 08:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •