JLabel[] x=new JLabel[10];
      int i=0;
      for(component comps : a.getComponent_list())
      {
        x[i]=comps.getType().getImg();
        x[i].setBounds(comps.getPosition().x, comps.getPosition().y, comps.getType().getWidth(), comps.getType().getHeight());
        System.out.println("Coming Here,"+comps.getId()+","+comps.getPosition().x);
        this.add(x[i]);
        i++;
      }

      repaint();

I want to have all the labels in their position. after this function.
but only the last added label is shown. What can I do?

Recommended Answers

All 23 Replies

Are you sure all the positions are different? Maybe print them out to confirm.

Yes, I'm sure. That's why i've printed comps.getPosition().x
All the positions are being printed but only last added label is shown.
Please help me. Badly stuck at here.

Did you explicitly set a null layout manager for the container "this"?

Yes. I've set null layout for "this" JComponent.

Hmmmm... curious.
Can you create a small stand-alone version that demonstartes the problem so I can try it here?

I'm giving the required function.

the "this" JComponent constructor :

public Pad_Draw(counts a)
{
      setDoubleBuffered(false);
      setLayout(null);
      Pad_counts=a;
};

The paintComponent of "this" :

  public void paintComponent( Graphics g )
  {
      graphics2D = (Graphics2D)g;
      graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      graphics2D.setPaint(Color.white);
      graphics2D.fillRect(0, 0, getSize().width, getSize().height);
      graphics2D.setColor(Color.black);
  }

the calling function:

  public void add_componentType( final componentType cmpType, final counts a )
  {
      this.reset();
      //to add a new component gate
      MouseListener add_component= new MouseAdapter(){
          @Override
          public void mouseClicked(MouseEvent e)
          {
              component current=new component(cmpType,e.getPoint(),a);
              update(a);
          }
      };
      this.addMouseListener(add_component);
      //System.out.println("component");
  }

the called function and the problem lies here :

  private void update(counts a)
  {
      JLabel[] x=new JLabel[10];
      int i=0;
      for(component comps : a.getComponent_list())
      {
        x[i]=comps.getType().getImg();
        x[i].setBounds(comps.getPosition().x, comps.getPosition().y, comps.getType().getWidth(), comps.getType().getHeight());
        //x.setBounds(a.getPosition().x,a.getPosition().y,a.getType().getWidth(),a.getType().getHeight());
        System.out.println("Coming Here,"+comps.getId()+","+comps.getPosition().x);
        this.add(x[i]);
        i++;
      }
      //System.out.println("Coming Here,");
      //Pad_counts=a;
      repaint();
      //this.update(graphics2D);
      //this.validate();
  }

I'm sure that the array has enough size. Please help me now.

Exactly which Jthing does Pad-Draw extend?
What does reset() do? Is it OK that you do that every time you add a new component?

Pad_Draw extend JComponent
and reset() is just setting the default mouse cursor.

  /**
   * reset to default so that we can add other component
   */
  public void reset(  )
  {
      this.default_mouse();
  }

Without being able to see and run the whole program I'm kinda out of ideas now... sorry

Can you please give me any idea on how to add array of JLabels to a null layout with JComponent extension with an example. I'll be greatful to you.

I've been trying to debug it. and I've seen that all the JLabels in the array are being added. but the previously added JLabel is not staying in the Pad_Draw and it has been disappearing and the newly added JLabel is showing.

Can you make a small simple program that compiles, executes and shows the problem? We need a program to test with.

It's so much dependant that I can't make a small sample program of this.
Can you give any idea how to add "ImageIcon"ed JLabels so that it stays at the JComponent. It's just nt staying.

Your code has something in it that is causing your problem.
You'd use the add() method to add a component to a container. I use the add() method all the time and do not have your problem.
If you want someone else to help you find the problem, You need to create a small simple program that compiles, executes and shows the problem.

x[i]=comps.getType().getImg();

This line has the error. As it's getting the image only. For the JLabel, I should use a Icon and code should be like this :

x[i]=comps.getType().getImg().getIcon();

This line has the error

What error? Please post the full text.

Break the statement up into one method call per statement and print out the results of each statement.

Actually "comps.getType().getImg()" returns a JLabel. Now when I'm doing x[i]=comps.getType().getImg() it's just not happening what I want i.e it's not equating comps.getType().getImg() to x[i].
So I create a new JLabel by writting

x[i]=new JLabel(comps.getType().getImg().getIcon());

So by getting this new JLabel. I can easily use this.add(x[i]) after seeting the bounds..

For debugging: Break the statement up into one method call per statement and print out the results of each statement. Copy the output and paste here.

Sorry, I didn't get you NormR1.

You have a compound statement with many method calls all in that one statement. That is hard to debug.
Separate out the method calls to only one per statement and print out what is returned by each method call.

I've already changed the code and variables slightly. I can share the current code. Here it's:

      JLabel new_label;
      component current=new component(cmpType,p,a);
      new_label=new JLabel(current.getType().getImg().getIcon());
      System.out.println(current);
      System.out.println(current.getType());
      System.out.println(current.getType().getImg());
      System.out.println(current.getType().getImg().getIcon());

and the output is :

circuits.component@2e4996ea
circuits.componentType@a266faa
javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=25165833,maximumSize=,minimumSize=,preferredSize=,defaultIcon=componentType,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]
componentType

What does the getImg() method return? It looks like a JLabel, not an Image.
Does the JLabel have the icon you want? Print out more info about the JLabel to verify that it is the correct one.
The location and the size of the label look like it is not being displayed.

The output from the test code you just posted is useless for finding the problem in your program. You need to print out the data for the variables in your program.

Actually my problem is solved!
getImg() does return a JLabel and getImg().getIcon() returns the ImageIcon it contains. and I create a new JLabel with that ImageIcon. that only solves my problem.

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.