Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

JLabel overlapping

Thread view: 
Mr Smith - 14 Feb 2005 14:04 GMT
Hello,

i would like to add JLabels to a custom JPanel. But even with the "null layout", JLabel are not drawn at all.

I i remove the line which sets the layout to null, all the JLabels are displayed in "grid" after a resize of the app.

Code to add the JLabel:

                   System.err.println("DEBUG: width = " + drawPanel.getWidth() + ", height = " + drawPanel.getHeight());
                   
                   for (int i = 0; i < jTablePoints.getRowCount(); i++) {
                       int x = drawPanel.getX(i);
                       int y = drawPanel.getY(i);
                       System.err.print("DEBUG: x = " + x + ", y = " + y);
                       String label = jTablePoints.getValueAt(i, sharedData.labelColumn).toString();
                       JLabel jLabel = new JLabel(label);
                       jLabel.setBackground(Color.GREEN);
                       jLabel.setBorder(javax.swing.BorderFactory.createLineBorder(Color.BLACK,1));
                       jLabel.addMouseListener(new JLabel_Listener());
                       jLabel.addMouseMotionListener(new JLabel_Listener());
                       drawPanel.add(jLabel);
                       jLabel.setLocation(x, y);
                       labelSet.add(jLabel);
                       repaint();
                       System.err.println("=> x = " + jLabel.getX() + ", y = " + jLabel.getY());
                   }
=>

DEBUG: width = 588, height = 359
DEBUG: x = 297, y = 157=> x = 297, y = 157
DEBUG: x = 318, y = 179=> x = 318, y = 179
DEBUG: x = 341, y = 155=> x = 341, y = 155

Why nothing is drawn?

Is it possible to let JLabel overlaps between themselves in a null layout?

TIA
Mr Smith - 14 Feb 2005 14:58 GMT
SOLVED

>> [...]
> Why nothing is drawn?

I was using only setLocation(), without asking a "preferred size". Now i use setBounds() and the JLabels are drawn(ed?).

> Is it possible to let JLabel overlaps between themselves in a null layout?

Seems to, yes.
John McGrath - 14 Feb 2005 20:15 GMT
> i would like to add JLabels to a custom JPanel. But even with the "null
> layout", JLabel are not drawn at all.

They are being drawn, but they have a size of [0,0], so they are difficult
to see.

> I i remove the line which sets the layout to null, all the JLabels are
> displayed in "grid" after a resize of the app.

That is because the layout manager sets the size (and position) for the
components in the container that it lays out.

> I was using only setLocation(), without asking a "preferred size". Now i
> use setBounds() and the JLabels are drawn(ed?).

Calling setBounds() is equivalent to calling setLocation() and setSize(),
so now the size is being set.

However, this is really not the best way to lay out a container in Java.
You seem to be dynamically computing the positions -- I presume that is
what the drawPanel object does -- which is a good thing.  But in Java,
sizing and positioning of child components is normally done by a layout
manager.

If you just placed those computations in a LayoutManager, your component
would probably work better.  The LayoutManager will be automatically
called when conditions change that could affect the layout.  It would also
be more flexible, since code that uses the component could replace the
layout if it needed to.

I also wonder whether those computations could be done with one of the
standard LayoutManagers.  There are quite a few to choose from and some of
them are quite powerful.  There is no reason to write a lot of positioning
and sizing code if it has already been done.

Signature

Regards,

John McGrath



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.