I want to add a Lable in a JComponent when a button will be clicked. I have added a actionlistner on that button.
I've added this.add(label) in the JComponent. but the location, which is set by label.setLocation(...) is not maintained in the JComponent. If i use FlowLayout() in the JComponent then the labels are added from the top of the area of the JComponent. If I use BorderLayout then only one Label can be added upon a Button click.
So what kind of JComponent configuration allows me to add a JLabel at exactly the position mentioned by me?
I've also used "null" Layout, but then no Labels can be added.
So what should I do?

I'm adding some of my codes for understanding.
this the JComponent PadDraw()

public PadDraw()
    {        
        setDoubleBuffered(false);
        setLayout(new FlowLayout());
    }

below code is a method in the PadDraw JComponent which is invoked when a button is pressed :

private void add_instances()
    {
        for(instance_struct comps : counts.components)
        {
            comps.label.setLocation(comps.x_pos,comps.y_pos);
            this.add(comps.label);
        }
    }

Recommended Answers

All 3 Replies

You can add labels to a null layout - you need to call setBounds to specify both the position and size, and request a repaint afterwards. For ultimate control over layouts while retaining all the advantages of a layout manager use GridBagLayout. The learning curve is a bit steep, but it's worth the effort.

You can add labels to a null layout - you need to call setBounds to specify both the position and size, and request a repaint afterwards. For ultimate control over layouts while retaining all the advantages of a layout manager use GridBagLayout. The learning curve is a bit steep, but it's worth the effort.

Problem is solved. Thnx. but there can be many problems with null layout. So, I'm thinking about GridBagLayout. but do you know that in that layout ma i able to draw graphics class content like drawLine etc??
and is co-ordinates are manipulable like null layout in GridBag?

Using drawing statements is part of overriding paintComponent, and is independent of what kind of layout manager you use.
GridBagLayout, like all the other non-null layout managers avoids pixel coordinates and goes for rules about how you want the space divided up and how to fit/space/align things within those divisions. The point of this is that you can move your app to another computer where the fonts are different and it will preserve what's important about your layout while fitting it to the new text sizes. Similarly, when the user resizes the window it can allocate the increased/reduced space in the most appropriate way (eg resize a list box while keeping the buttons at the same relative position).
Frankly, if you are not going to move to a new machine, and you don't allow the user to resize the window, you may as well stay with null layout (but not in any other circumstances)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.