Results 1 to 4 of 4
  1. #1
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default JTextfield question

    I am taking a graduate seminar that includes some Java programming. I am working on an assignment that has 2 text field boxes and a check box. The end goal is to save the data entered into a txt file. To approach this assignment, I am taking it piece by piece where I build upon the prior step. I decided my first step was to build the interface with the 2 text field boxes and a check box. I initially tried using the BorderLayout.XXX feature to arrange the objects but discovered setBounds being easier for me to control where I want things. Long story short, I am able to make the interface the way I want it to but for some reason the second text field box is not showing (just the label). Can someone tell me where I went wrong? Thanks

    public class Homework2a_GK extends JFrame {

    private JPanel panel;
    private JButton button;
    private JCheckBox check;
    private JTextField bookTitle;
    private JTextField author;
    private JLabel bookLabel;
    private JLabel authorLabel;

    public Homework2a_GK(String str) {
    super(str);

    }
    public static void main(String[] args) {

    Homework2a_GK myGUI = new Homework2a_GK("Save book records");
    myGUI.createAndShowGUI();
    }
    private void createAndShowGUI() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = getContentPane();
    contentPane.setLayout(new BorderLayout());

    JFrame frame = new JFrame("Save book records");

    JButton button = new JButton("Save");
    button.setBounds(230,125, 75,20);
    this.add(button);

    JCheckBox check = new JCheckBox("Available");
    check.setBounds(230,100,100,20);
    this.add(check);

    bookLabel = new JLabel("Book title:");
    bookLabel.setBounds(10,10,100,20);
    this.add(bookLabel);
    bookTitle = new JTextField();
    bookTitle.setBounds(120,10,350,20);
    this.add(bookTitle);


    authorLabel = new JLabel("Author:");
    authorLabel.setBounds(10,50,100,20);
    this.add(authorLabel);
    author = new JTextField();
    author.setBounds(120,50,350,20);
    this.add(author);


    setSize(500, 200);


    setVisible(true);

    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If you're going to use absolute positioning of components in a container, then the container just use the "null" layout, not a BorderLayout. So this:

    Java Code:
          //!! contentPane.setLayout(new BorderLayout());
          contentPane.setLayout(null);
    
          // JFrame frame = new JFrame("Save book records"); //!! This does nothing

    will likely fix your short-term problems.

    Having said that, let me put in a plug for using layout managers and for avoiding null layout in general. Following my recommendation will give your Swing GUI greater flexibility, upgradeability, and a better looking GUI on different platforms. Remember that you're not constrained to use one container with one layout, but rather you can nest JPanels each using its own layout to achieve as complex a gui as desired.

    Edit: for instance, right now your GUI shows two JLable / JTextField pairs, but what if later you want to update it so that it is able to retrieve more information, perhaps requiring 6 label/field pairs? With your current code, you'll have to calculate the bounds of all the new components and modify yourself the bounds of all the components below the new ones as well as the GUI size. If on the other hand your placed your label/fields in a separate JPanel that uses say GridBagLayout, and then add this JPanel into the GUI that uses say BoxLayout, the layout managers would take care of all the changes for you.
    Last edited by Fubarable; 04-08-2011 at 05:15 PM.

  3. #3
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default

    Interesting..so I can have multiple panels and use the layout manager to arrange the panels and not the objects. With this in mind, I can have one panel in the NORTH to house the 2 text field boxes, and one panel in the south to house the checkbox and save button. If I use this method, would I be able to control the size of the button so that it doesn't occupy the entire size of the panel?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by xyknight View Post
    Interesting..so I can have multiple panels and use the layout manager to arrange the panels and not the objects. With this in mind, I can have one panel in the NORTH to house the 2 text field boxes, and one panel in the south to house the checkbox and save button. If I use this method, would I be able to control the size of the button so that it doesn't occupy the entire size of the panel?
    Try it! The button will fill the container if certain layouts are used. If you don't want it to fill, you may wish to place it in its own JPanel (which uses FlowLayout by default) and then add that JPanel to your GUI. The best thing to do though is to read the tutorial on the layout managers and then experiment with them.

    Much luck!

Similar Threads

  1. JTextField Question
    By Rocketz in forum New To Java
    Replies: 1
    Last Post: 03-09-2011, 01:38 AM
  2. JTextField Question
    By Kyle227 in forum New To Java
    Replies: 5
    Last Post: 05-09-2010, 08:44 AM
  3. JTextField question
    By Chasingxsuns in forum New To Java
    Replies: 5
    Last Post: 07-14-2009, 03:39 AM
  4. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  5. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 05:25 PM

Posting Permissions

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