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
    13,432
    Blog Entries
    7
    Rep Power
    20

    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
    cenosillicaphobia: the fear for an empty beer glass

  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 online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    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 Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our 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
    13,432
    Blog Entries
    7
    Rep Power
    20

    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
    cenosillicaphobia: the fear for an empty beer glass

  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
    13,432
    Blog Entries
    7
    Rep Power
    20

    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. limit characters in textfield in swing
    By heman in forum AWT / Swing
    Replies: 4
    Last Post: 11-06-2012, 11:53 AM
  2. Replies: 1
    Last Post: 12-11-2011, 11:59 AM
  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
  •