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.

Swing JLayeredPane question

Thread view: 
Tobias - 16 Feb 2005 10:11 GMT
Hi all,

I'm coding a graphical interface for small screen touchscreen PC composed of
menus and submenus. As the look of it must be fancy, there is a background
image.

I'd like a status bar on top displaying informations (like is there a CD or
DVD in the tray, time/date and so on). This status bar is darkened but
still transparent, and must be present on all menus and screens.

So the main window is a JFrame with BorderLayout content pane. At the center
a card layout displays the different screens and at the north there is this
status bar :
public class StatusBarJPanel extends javax.swing.JPanel {
private JLabel jLabelDateTime;
private LabelDateTimeTimer timer;
private JLayeredPane layeredPane;

public StatusBarJPanel() {
 super();
 init();
}

private void init() {
 layeredPane = new JLayeredPane();
 layeredPane.setLayout(new BorderLayout());
 layeredPane.setOpaque(true);
 layeredPane.setBackground(new Color(0, 0, 0, 130));
 this.add(layeredPane, BorderLayout.NORTH);

 try {
  this.setPreferredSize(new java.awt.Dimension(640, 40));
  layeredPane.setPreferredSize(new java.awt.Dimension(640, 40));
 
  jLabelDateTime = new JLabel();
  timer = new LabelDateTimeTimer(jLabelDateTime);
  layeredPane.add(jLabelDateTime, BorderLayout.EAST);
  jLabelDateTime.setForeground(Color.WHITE);
  jLabelDateTime.setFont(new Font("Helvetica", Font.BOLD, 18));
  jLabelDateTime.addPropertyChangeListener(new PropertyChangeListener() {
   // to avoid odd background repainting after time change
   public void propertyChange(PropertyChangeEvent evt) {
    layeredPane.repaint();
    layeredPane.getParent().repaint();
   }
  });
 
  JLayeredPane iconPane = new JLayeredPane();
  iconPane.setLayout(new FlowLayout());
  layeredPane.add(iconPane, BorderLayout.WEST);
  ImageIcon dvd =
   new ImageIcon(
    getClass().getClassLoader().getResource(
     "skins/tst/dvd_mount.png"));
  JLabel dvdIcon = new JLabel(dvd);
  iconPane.add(dvdIcon);
  ImageIcon gprs =
   new ImageIcon(
    getClass().getClassLoader().getResource(
     "skins/tst/gprs.png"));
  JLabel gprsIcon = new JLabel(gprs);
  iconPane.add(gprsIcon);
 
 } catch (Exception e) {
  e.printStackTrace();
 }
}
}
(this is just a prototype to see if what we need is feasible, so quick and
dirty code...)

Two questions :
- Is there another simpler design to avoid LayeredPane + JPanel (this is to
repaint the background after some elements of the status bar change) ?
- There is a small border at the top of the window where the LayeredPane is
not visible, so there is a small stipe at the top of the window not colored
by the layered pane. I tried to add an empty border to the JPane, put the
LayeredFrame at the north, but the stripe is still there. Do you see a
workaround ?

Thank you for your answers.

Best regards,

Tobias
Thomas Weidenfeller - 16 Feb 2005 11:05 GMT
> Hi all,
>
> I'm coding a graphical interface for small screen touchscreen PC composed of
> menus and submenus. As the look of it must be fancy, there is a background
> image.
[...]

>      getClass().getClassLoader().getResource(
>       "skins/tst/gprs.png"));

     this.class.getResource("/skins/tst/gprs.png");

> Two questions :
> - Is there another simpler design to avoid LayeredPane + JPanel (this is to
> repaint the background after some elements of the status bar change) ?

Subclass JPanel and override the paintComponent() method (and override
isOpaque() to always return true, but that is a longer story). In
paintComponent() paint the background image. Use instances of this
subclassed JPanel as your container.

If you need the background image stretch not only over a particular
JPanel, but over the entire size of a window, you need to subclass the
particular JFrame or JDialog class instead of JPanel and do a similar
change.

> - There is a small border at the top of the window where the LayeredPane is
> not visible, so there is a small stipe at the top of the window not colored
> by the layered pane. I tried to add an empty border to the JPane, put the
> LayeredFrame at the north, but the stripe is still there. Do you see a
> workaround ?

You need to find out who owns that space. The enclosing container or the
enclosed component. Depending on this you have to figure out why it is
there. E.g. check the insets.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Tobias - 16 Feb 2005 11:32 GMT
Thanks for your answer.

>> Two questions :
>> - Is there another simpler design to avoid LayeredPane + JPanel (this is
[quoted text clipped - 9 lines]
> particular JFrame or JDialog class instead of JPanel and do a similar
> change.

If I do this, the look and feel styles that I could use with the whole app
wouldn't have effects on the background, or am I getting something wrong ?
Because the UI paintComponent would be overridden by this custom one,
would'nt it ?

In fact, what we are looking for is the possibility to change the background
image by defining another LnF. But the status bar always need to be black
and transparent above the background image in the content pane. And I want
to avoid the use of the glass pane...

Tobias


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.