Results 1 to 9 of 9
  1. #1
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default Components as containers

    In a program i am designing i need to be able to add a jcomponent to a Jlabel. I have set the jlabel to opaque and I am able to change the background color. In addition, i am able to add a jlabel to another jlabel. However, i am unable to add any other jcomponents, such as jbutton or jtextfield, to a jlabel. Is their any way i can accomplish this or is there any way i can display a jcomponent on top of a jlabel?

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

    Default Re: Components as containers

    I would advise against doing this as JLabel is probably not the best JComponent to use as a container. Why not instead use a JPanel?

  3. #3
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default Re: Components as containers

    i need to be able to display text. a jtextarea would work too but I would imagine it would pose the same problems

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

    Default Re: Components as containers

    Probably your best bet is to create a small compilable and runnable program that demonstrates your problem, one that we could run on our own machines unaltered, and that we could fiddle with to better understand your problem and help you come up with a solution. This beast is called an SSCCE. Please check out the link to find out more about it and how it can help you and us.

  5. #5
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default Re: Components as containers

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class test {
     public test()
     {
    	 JFrame frame = new JFrame();
    	 frame.setLocationRelativeTo(null);
    	 frame.setSize(600, 500);
    	 frame.setVisible(true);
    	 frame.setDefaultCloseOperation(3);
    	 
    	 JPanel panel1 = new JPanel();
    	 frame.add(panel1);
    	  
    	 panel1.setOpaque(true);
    	 panel1.setBackground(Color.GRAY);
    	 panel1.setLayout(new BorderLayout());
    	 panel1.setBorder(BorderFactory.createEmptyBorder(70, 20, 70, 20));
    	 JLabel label = new JLabel();
    	 panel1.add(label, BorderLayout.CENTER);
    	 label.setBorder(BorderFactory.createTitledBorder("Topic"));
    	 label.setBackground(Color.white);
    	 label.setOpaque(true);
    	 label.setLayout(new GridLayout(8, 1));
    	 
    	 //problem below
    	 JTextField field = new JTextField(30);
    	 label.add(field);
     }
     public static void main(String args[])
     {
    	 test test = new test();
     }
    }
    Last edited by Fubarable; 09-12-2011 at 12:23 AM. Reason: code tags added

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

    Default Re: Components as containers

    I have added code tags to your post so the code is readable. Can you tell us what this code is supposed to do that it's not doing, or what it is doing that it's not supposed to do, and any other information that would help us to understand it and your problem better?

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

    Default Re: Components as containers

    Note that when I run your code, I'm able to see the JTextField and enter text into it.

  8. #8
    wired-in=p is offline Member
    Join Date
    Jul 2011
    Posts
    38
    Rep Power
    0

    Default Re: Components as containers

    When i run the code i am unable to see the jtextfield. Does this have something to do with the platform I am running the code from?

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default Re: Components as containers

    I am also able to see the text field which is strange because it shouldn't work.

    Generally when you add a component to a visible GUI you need to revalidate() the parent component.

    Reorder your code so that you create and display the frame AFTER you have created and added all your components to the panel.

    I also agree with Fubarable that you should use a JPanel as a container, instead of a JLabel. I sometimes use a JLabel as a container when I want to use an Image as the background, but otherwise I use a JPanel.

Similar Threads

  1. Replies: 14
    Last Post: 08-22-2011, 12:14 AM
  2. transparant containers
    By Dipke in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 02:41 PM
  3. Replies: 2
    Last Post: 12-19-2010, 02:24 PM
  4. First Java-game: Containers
    By eastviolence in forum New To Java
    Replies: 0
    Last Post: 04-04-2008, 06:09 PM
  5. Swing containers
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-07-2007, 12:06 PM

Tags for this Thread

Posting Permissions

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