🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
2 messages
2 total messages Started by Alan Shiers Wed, 08 Dec 2004 16:41
dropping JLabel onto JPanel
#752
Author: Alan Shiers
Date: Wed, 08 Dec 2004 16:41
42 lines
2749 bytes
Hi there,

I'm working on a Drag 'n Drop project. I simply want to drag items from a
class that extends JList to another class that extends JPanel. In my
project, the class that extends JList is named: MutableList.  The class that
extends JPanel is named:
TargetPanel.  It is supposed to be a simple one-way COPY procedure. I have
it set up so that MutableList has single selection and displays objects that
represent various types of items: radiobutton, checkbox, listbox, textbox,
etc.  Each of these are rendered using a class that implements
ListCellRenderer; which extends JLabel and renders an image of the various
items into MutableList.

This much is working so far. All my code compiles fine.  The problem I am
facing now is at runtime during the actual drop procedure.  I have no
problem selecting an item from the MutableList and begin dragging it over to
the TargetPanel.  The TargetPanel class has a method called
addElement(Component c).  Here is what it looks like:

public void addElement(Component c)
{
    add(c);
    Component[] comp = this.getComponents();
    if(comp[0] instanceof MyComponent)
      System.out.println("comp is instanceof MyComponent");
    repaint();
}

Note the MyComponent object mentioned in the method. I test to ensure that
TargetPanel did indeed receive the drop with the instanceof operator. When I
run this code, I do indeed get the println statement executed, so I know its
there. The MyComponent object extends JLabel and is a new instance
representing a copy of the original item in MutableList.
Now, note that I then call repaint().  This is an attempt in making this
instance of MyComponent visibly appear in TargetPanel. Well, surprise,
surprise!  It doesn't!

What am I doing wrong?  How do I make MyComponent appear in TargetPanel?

Please advise,

Alan
Re: dropping JLabel onto JPanel
#753
Author: Alan Shiers
Date: Wed, 08 Dec 2004 17:03
9 lines
1149 bytes
Ok, you can ignore this last post.  I just found the answer to my problem.
John McGrath answered the same problem I was having in another post subject
titled: adding components to JPanel in JScrollPane.

The answer was to use revalidate() instead of repaint().

That certainly worked in this instance as well.  Thanks John.

Alan
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads