Jump to content

JPanel is shown empty when it is added to JFrames DRAG LAYER

- - - - -

  • Please log in to reply
4 replies to this topic

#1
vikramdarsi

vikramdarsi

    Newbie

  • Members
  • Pip
  • 2 posts
I have added three JLabel's, each containing a image, to a JPanel.
this JPanel is added to JFrame's DRAG LAYER dynamically, when user presses the key "Alt + RIGHT ARROW"
and will be removed when ALT key is released or arrow key is pressed when alt is holding down

ISSUE: when i pressed Alt + RIGHT ARROW, panel is shown empty, please explain me why it is behaving so?

Intention of writing the code is to achieve the functionality of Alt+Tab of any OS

Code you can find in the attachments


Also explain me which is correct way of doing,
prepare JPanel with the components, set its bounds, add it to JLayeredPane
or
add all the components to JLayeredPane by setting their bounds, and add JLayaredPane to the JPanel

Attached Files



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
JScrollPanes don't work like that. You can't .add(..) something to it. You must pass it to the constructor.
So lower the initialization code up until the panel is created, then do:

pane = new JScrollPane(panel);


Also, The setBounds(...) stuff will be ignored unless the layout is specifically set to null:
panel.setLayout(null);

_desktopPane.setLayout(null);

Quote

prepare JPanel with the components, set its bounds, add it to JLayeredPane
or
add all the components to JLayeredPane by setting their bounds, and add JLayaredPane to the JPanel

To be honest.I never use the setBounds(..), I always work with Layouts.

Quote

Why?
By using setBounds, you don't have a responsive layout. Meaning your gui components won't reorganize, or resize to fit new window sizes or anything.

When using a Layout, the Gui will resize itself to fit properly in the window.
And even when doing setResizable(false); so the window has a set size. The font type and size could possibly differ.

#3
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I agree with Wim that you should not use absolute position of your components. (unless you absolutely have to)
Read the first paragraph at the following page:
Doing Without a Layout Manager (Absolute Positioning) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

#4
vikramdarsi

vikramdarsi

    Newbie

  • Members
  • Pip
  • 2 posts
Hi Wim DC,
Thank you very much, after making that change it worked as expected. i did a silly mistake. But want to understand how

new JScrollPane(panel); and

pane = new JScrollPane();
pane.add(panel) are different?

Can you please throw some light on this or please point me to some documents

Once again thank you sir


--Vikram


wim DC said:

JScrollPanes don't work like that. You can't .add(..) something to it. You must pass it to the constructor.
So lower the initialization code up until the panel is created, then do:

pane = new JScrollPane(panel);


Also, The setBounds(...) stuff will be ignored unless the layout is specifically set to null:
panel.setLayout(null);

_desktopPane.setLayout(null);



To be honest.I never use the setBounds(..), I always work with Layouts.

By using setBounds, you don't have a responsive layout. Meaning your gui components won't reorganize, or resize to fit new window sizes or anything.

When using a Layout, the Gui will resize itself to fit properly in the window.
And even when doing setResizable(false); so the window has a set size. The font type and size could possibly differ.


#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
The JScrollPane does not use the .add() method. It only has that method because JScrollPane extends JComponent which extends Container (this class had the .add() method).
So that's why the JScrollPane has the .Add() method.

I don't exactly know why, but the JScrollPane class doesn't use the .Add() method to add stuff that needs to be contained in a scrollable window. It uses either the constructor, or you can also use the setViewPortView() method:
JScrollPane setViewPortView

Note that internally the JScrollPane does use the add() method itself to add the scrollbars, and also the "viewport". Like

add(viewport, VIEWPORT);

add(horizontalScrollBar, HORIZONTAL_SCROLLBAR);

add(verticalScrollBar, VERTICAL_SCROLLBAR);
Where those capitalised words are constants:

VIEWPORT = "VIEWPORT"

HORIZONTAL_SCROLLBAR = "HORIZONTAL_SCROLLBAR"

VERTICAL_SCROLLBAR = "VERTICAL_SCROLLBAR"

Which makes me assume that anything which gets added without such a key, is simply ignored.
I don't mean that you should .add() stuff and give as key "VIEWPORT", No idea how that will turn out really. :-P

Edit: I actually tested this adding with viewport thing:

scrollPane.add(new JLabel("hi"), "VIEWPORT");

leads to

java.lang.ClassCastException: javax.swing.JLabel cannot be cast to javax.swing.JViewport






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users