Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: GUI Question (Jlabe/JTextField)

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default GUI Question (Jlabe/JTextField)

    This is my first time working with JPanels, etc I am trying to make a panel with a bunch of JTextFields and a bunch of JLabels, I want to make the JLabels centered above the JTextFields.

    //Created textfields and labels
            JTextField jtxtFN = new JTextField("", 10);
            JTextField jtxtLN = new JTextField("", 10);
            JTextField jtxtWage = new JTextField("", 8);
            JTextField jtxtEmp = new JTextField("", 5);
            JLabel jlFN = new JLabel("First Name");
            JLabel jlLN = new JLabel("Last Name");
            JLabel jlWage = new JLabel("Wage");
            JLabel jlJob = new JLabel("Job Type");
            JLabel jlEmp = new JLabel("Employee ID");
            JLabel jlGend = new JLabel("Gender");
            //Adding text fields and labels
            p3.add(jlFN);
            p3.add(jtxtFN);
            p3.add(jlLN);
            p3.add(jtxtLN);
            p3.add(jlWage);
            p3.add(jtxtWage);
            p3.add(jlJob);
            //Combo Box
            JComboBox jcJob = new JComboBox();
            jcJob.addItem("Hourly");
            jcJob.addItem("Salaried");
            jcJob.addItem("Consultant");
            p3.add(jcJob);
            p3.add(jlEmp);
            p3.add(jtxtEmp);
            p3.add(jlGend);
     
            //Radio Buttons with Button group
            JRadioButton jbMale = new JRadioButton("Male");
            JRadioButton jbFemale = new JRadioButton("Female");
            JRadioButton jbUnkown = new JRadioButton("Unknown");
            ButtonGroup bg = new ButtonGroup();
            bg.add(jbMale);
            bg.add(jbFemale);
            bg.add(jbUnkown);
            p3.add(jbMale);
            p3.add(jbFemale);
            p3.add(jbUnkown);
    Last edited by aandcmedia; April 3rd, 2012 at 07:10 PM.


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    21
    My Mood
    Torn
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: GUI Question (Jlabe/JTextField)

    I suggest you downloading netbeans it has a visual editor, where you can drag and drop your GUI components onto a frame. Since you are new, netbeans would be ideal.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: GUI Question (Jlabe/JTextField)

    Quote Originally Posted by Ajan View Post
    I suggest you downloading netbeans it has a visual editor, where you can drag and drop your GUI components onto a frame. Since you are new, netbeans would be ideal.
    I have netbeans but I need to learn it this way first before moving onto that way. Any guidance would be much appreciated.

  4. #4

    Default Re: GUI Question (Jlabe/JTextField)

    Quote Originally Posted by aandcmedia View Post
    This is my first time working with JPanels, etc I am trying to make a panel with a bunch of JTextFields and a bunch of JLabels, I want to make the JLabels centered above the JTextFields.
    To arrange components within a JFrame/JPanel, you should use some kind of layout managers. For your situation, I suggest you dive into GroupLayout or GridBagLayout.

  5. #5
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,234
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: GUI Question (Jlabe/JTextField)

    Quote Originally Posted by Ajan View Post
    I suggest you downloading netbeans it has a visual editor, where you can drag and drop your GUI components onto a frame. Since you are new, netbeans would be ideal.
    Opinions vary, and mine is that I do not recommend this if you are a beginner. GUI builders are for more advanced users, or at least those familiar with the basics of Swing. In my experience those without any knowledge of Swing cannot customize, adapt, read, or even debug code written by a builder

    @aandcmedia, as mentioned using the appropriate LayoutManager: see A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: GUI Question (Jlabe/JTextField)

    I wish that was an option buy my Java class requires we write out the entire thing, my program is looking fine except for this one problem I seem to be facing. I will eventually figure it out, will get back to everyone when I do. After a bit of brain power and suggestions of boxes within boxes etc.

    public class Layout extends JFrame{
        public Layout (){
           //Create 1 master panel (p1), than 2 panels that will go inside of it
            JPanel p1 = new JPanel(new BorderLayout());
            JPanel p2 = new JPanel(new GridLayout(1,5));
            JPanel p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
            JPanel p4 = new JPanel(new FlowLayout(FlowLayout.CENTER));
     
            //Setting up font for centering
     
     
            //Populating the grid with buttons
            JButton jbtAdd = new JButton("Add Employee");
            JButton jbtChange = new JButton("Change Employee");
            JButton jbtClear = new JButton("Clear Fields");
            JButton jbtRead = new JButton("Read File");
            JButton jbtWrite = new JButton("Write File");
            p2.add(jbtAdd);
            p2.add(jbtChange);
            p2.add(jbtClear);
            p2.add(jbtRead);
            p2.add(jbtWrite);
     
            //Created textfields and labels
            JTextField jtxtFN = new JTextField("", 10);
            JTextField jtxtLN = new JTextField("", 10);
            JTextField jtxtWage = new JTextField("", 8);
            JTextField jtxtEmp = new JTextField("", 5);
            JLabel jlFN = new JLabel("First Name");
            JLabel jlLN = new JLabel("Last Name");
            JLabel jlWage = new JLabel("Wage");
            JLabel jlJob = new JLabel("Job Type");
            JLabel jlEmp = new JLabel("Employee ID");
            JLabel jlGend = new JLabel("Gender");
     
            //Center All Labels:
            jlFN.setHorizontalAlignment(JLabel.CENTER);
            jlLN.setHorizontalAlignment(JLabel.CENTER);
            jlWage.setHorizontalAlignment(JLabel.CENTER);
            jlJob.setHorizontalAlignment(JLabel.CENTER);
            jlEmp.setHorizontalAlignment(JLabel.CENTER);
            jlGend.setHorizontalAlignment(JLabel.CENTER);
            //Laying out the text boxes with labels to center
           JPanel f1 = new JPanel(new GridLayout(2,1));
           JPanel f2 = new JPanel(new GridLayout(2,1));
           JPanel f3 = new JPanel(new GridLayout(2,1));
           JPanel f4 = new JPanel(new GridLayout(2,1));
           JPanel f5 = new JPanel(new GridLayout(2,1));
           JPanel f6 = new JPanel(new GridLayout(2,1));
           JPanel f7 = new JPanel(new GridLayout(1,3));
     
           f1.add(jlFN);
           f1.add(jtxtFN);
           f2.add(jlLN);
           f2.add(jtxtLN);
           f3.add(jlWage);
           f3.add(jtxtWage);
     
     
            p3.add(f1);
            p3.add(f2);
            p3.add(f3);
     
     
     
            //Combo Box with proper labeling 
            f4.add(jlJob);
            JComboBox jcJob = new JComboBox();
            jcJob.addItem("Hourly");
            jcJob.addItem("Salaried");
            jcJob.addItem("Consultant");
            f4.add(jcJob);
            p3.add(f4);
     
            f5.add(jlEmp);
            f5.add(jtxtEmp);
            p3.add(f5);
     
            f6.add(jlGend);
     
            //Radio Buttons with Button group
            JRadioButton jbMale = new JRadioButton("Male");
            JRadioButton jbFemale = new JRadioButton("Female");
            JRadioButton jbUnkown = new JRadioButton("Unknown");
            ButtonGroup bg = new ButtonGroup();
            bg.add(jbMale);
            bg.add(jbFemale);
            bg.add(jbUnkown);
            f7.add(jbMale);
            f7.add(jbFemale);
            f7.add(jbUnkown);
            f6.add(f7);
            p3.add(f6);
     
            //Creating the list
            JLabel jlEmpList = new JLabel("Employee List");
            JList jlList = new JList();
            p4.add(jlEmpList);
            p4.add(jlList);
     
            add(p4, BorderLayout.WEST);
            add(p2, BorderLayout.SOUTH);
            add(p3, BorderLayout.CENTER);
     
     
        }

    After Scouring the internets I've found everything I've needed to make my GUI! Now its time to learn how to use handling!
    Last edited by aandcmedia; April 4th, 2012 at 06:34 PM.

Similar Threads

  1. Need help int JTextField
    By n00b in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 1st, 2011, 09:57 AM
  2. how to delete a JTextField?
    By A4Andy in forum AWT / Java Swing
    Replies: 10
    Last Post: August 31st, 2011, 11:33 AM
  3. Jtextfield Validation
    By nimishalex in forum AWT / Java Swing
    Replies: 8
    Last Post: December 11th, 2010, 01:42 AM
  4. Get a certain line in a JTextField
    By FlamingDrake in forum Java Theory & Questions
    Replies: 2
    Last Post: May 14th, 2010, 03:21 PM
  5. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 07:37 AM