Results 1 to 8 of 8
  1. #1
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default swing textfield not showing

    Hello.
    Ive been trying for 2 days to make an app and I'm stuck in the incipient phase, i.e. the UI. Swing is so difficult that I can't even display the components. Can anyone help me, please?
    For example, why isn't the JTextField there?
    The plan is: 1 text field + 2 buttons below on the left part of the frame and 1 text area on the right. That's why I created 2 panels. I want all components normal, not "maximized" within each of their own space (I want them spaced in between). Here is what I tried:

    Java Code:
     import java.awt.*;
        import java.util.*;
        import javax.swing.*;
        
        public class Main
        {
        public static void main ( String [ ] args )
        {
        JFrame f = new JFrame("Simple Crawler");
         f.setSize(new Dimension(840, 400));
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //JPanel p0 = (JPanel) f.getContentPane();
        JPanel p0 = new JPanel();
        JPanel p1 = new JPanel();
         
        JTextField tfHostName = new JTextField("default text", 20);
         tfHostName.setBounds(50, 50, 200, 25);
        
        JButton bRequest = new JButton("request");
        JButton bReset = new JButton("reset");
        
        f.add(p0);
         p0.add(tfHostName);
        f.add(p1);
        
        //f.pack();
        f.setResizable(false);
        f.setVisible(true);
        
        }
        }
    So the problems are:
    1. Why is not the field visible? (most important q)
    2. The difference between "JPanel p0 = (JPanel) f.getContentPane();" (which I don't know what it does exactly) and what I used instead: "JPanel p0 = new JPanel();" and "f.add(p0);"
    3. What layout managers to use for auto spacing if I'd make the frame resizable? I'd go with group layout.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,372
    Blog Entries
    7
    Rep Power
    25

    Default Re: swing textfield not showing

    Check what happens when you add( ... ) two components (here JPanels) to a JFrame without specifying their location or position in a LayoutManager.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default Re: swing textfield not showing

    So this is it -- not specifying position. Can you tell me then how can I specify the position and size of a component in a container in percent of the container dimensions?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,648
    Rep Power
    9

    Default Re: swing textfield not showing

    I suggest you check out the tutorials in my signature link. Go to the big index and check out the topic of layout managers. There are a lot of other useful examples in the tutorials as well. Worth going through all of them.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default Re: swing textfield not showing

    Thanks, I've already read through some of those tutorials but I'll keep on reading. It's just frustrating that I'm stuck at calligraphy for a couple of days when I have a novel to write.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,372
    Blog Entries
    7
    Rep Power
    25

    Default Re: swing textfield not showing

    The content pane has a BorderLayout by default; you can add one JPanel to BorderLayout.WEST and the other one to BorderLayout.EAST and see what happens ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  7. #7
    mireazma is offline Member
    Join Date
    Apr 2013
    Posts
    16
    Rep Power
    0

    Default Re: swing textfield not showing

    Thanks for the suggestion. I dropped the layout managers altogether and I decided to place them manually.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,372
    Blog Entries
    7
    Rep Power
    25

    Default Re: swing textfield not showing

    Quote Originally Posted by mireazma View Post
    Thanks for the suggestion. I dropped the layout managers altogether and I decided to place them manually.
    That's a very bad move; resize your frame or use another screen resolution and see what happens ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. limit characters in textfield in swing
    By heman in forum AWT / Swing
    Replies: 4
    Last Post: 11-06-2012, 12:53 PM
  2. Replies: 1
    Last Post: 12-11-2011, 12:59 PM
  3. New to SWING / a tad confused on showing things...
    By Fuginator in forum AWT / Swing
    Replies: 10
    Last Post: 05-15-2011, 10:48 PM
  4. How to make Swing JTextField look like AWT TextField?
    By LeonLanford in forum AWT / Swing
    Replies: 1
    Last Post: 09-04-2010, 04:06 PM
  5. Showing NetBeans-generated Swing forms
    By _SAS in forum AWT / Swing
    Replies: 0
    Last Post: 06-19-2010, 02:45 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
  •