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 / October 2005

Tip: Looking for answers? Try searching our database.

JScrollPane - strange repainting problem?

Thread view: 
lmierzej - 24 Oct 2005 13:50 GMT
Hi,

why JScrollPane is not repainting like other components (like JButton)?

repaint()on main frame doesn't work on JScrollPane, only
setVisible(true) on main frame works well

could anyone help? (there is a quick, working code example below)

thanks very much in advance for your help!

CODE SAMPLE:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class ScrollPaneTest extends JFrame{
   
    public ScrollPaneTest() {
       
        this.setLayout(null);
       
        this.setBounds(100,100,300,300);
        this.setVisible(true);
       
        //JScrollPane begin
        JScrollPane scrollPane = new JScrollPane();
       
        JLabel label = new JLabel("This is JLabel!");
        scrollPane.setViewportView(label);
       
        scrollPane.setBounds(50,50,80,80);
        scrollPane.setVerticalScrollBarPolicy(
            ScrollPaneConstants.
            VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(
            ScrollPaneConstants.
            HORIZONTAL_SCROLLBAR_ALWAYS);
       
        this.getContentPane().add(scrollPane);
        //JScrollPane end
       
       
        //JButton begin
        JButton button = new JButton();
       
        button.setBounds(50,200,50,50);
       
        this.getContentPane().add(button);
        //JButton end
       
        this.repaint(); //JScrollPane content is blank!
       
        //But setVisible(true) makes everything ok.
    }

    public static void main(String[] args) {
       
        new ScrollPaneTest();
    }
   
}

regards,
lmierzej
Andrew Thompson - 24 Oct 2005 14:38 GMT
> why ... is not repainting ...?
..
> could anyone help?

Sure.  Your problem begins right...

> CODE SAMPLE:
> import javax.swing.JButton;
[quoted text clipped - 8 lines]
>        
>         this.setLayout(null);

..here.  Put those components in an appropriate layout,
and the problem is (partially) solved..

You will also need to stop sizing components using setBounds,
and call 'pack()'.

So I suppose the real solution is.

Learn to use layouts.  Start here..
<http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html>

HTH
lmierzej - 24 Oct 2005 14:58 GMT
> ..here.  Put those components in an appropriate layout,
> and the problem is (partially) solved..

I don't like "automatic" layouts, but I guess they make live easier :)

> You will also need to stop sizing components using setBounds,
> and call 'pack()'.

Working fine with pack() for frames and validate() for conteiners. Thank
you for your help!

lmierzej
Monique Y. Mudama - 24 Oct 2005 16:56 GMT
>> ..here.  Put those components in an appropriate layout, and the
>> problem is (partially) solved..
>
> I don't like "automatic" layouts, but I guess they make live easier
> :)

That probably just means that you don't understand them well enough
yet.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Roedy Green - 25 Oct 2005 05:42 GMT
>I don't like "automatic" layouts, but I guess they make live easier :)

That write or use  a "manual" one. Don't muddle application and layout
logic.

see http://mindprod.com/jgloss/layout.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Vova Reznik - 24 Oct 2005 15:51 GMT
> Hi,
>
[quoted text clipped - 19 lines]
>        
>         this.setLayout(null);

May not set JFrame layout directly - only to its content pane

>        
>         this.setBounds(100,100,300,300);
>         this.setVisible(true);

Don't setVisible true before finishing construction

>        
>         //JScrollPane begin
[quoted text clipped - 4 lines]
>        
>         scrollPane.setBounds(50,50,80,80);

>         scrollPane.setVerticalScrollBarPolicy(
>             ScrollPaneConstants.
[quoted text clipped - 6 lines]
>         //JScrollPane end
>        

If you will not set content pane layout to null - you should specify
position, because default layout for content pane is BorderLayout

>        
>         //JButton begin
[quoted text clipped - 6 lines]
>        
>         this.repaint(); //JScrollPane content is blank!

    repaint may not need to be called.

>        
>         //But setVisible(true) makes everything ok.
[quoted text clipped - 6 lines]
>    
> }

Construct UI. (Constructor)
Pack it.      (pack())
Show it       (setVisible(true))

try this:
public ScrollPaneTest() {
  JLabel label = new JLabel("This is JLabel!");
  JScrollPane scrollPane = new JScrollPane(label,
    ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

  this.getContentPane().add(scrollPane, BorderLayout.CENTER);

  JButton button = new JButton("Button");
  getContentPane().add(button, BorderLayout.NORTH);

  pack();
}

public static void main(String[] args) {
  final ScrollPaneTest f = new ScrollPaneTest();
  f.setDefaultCloseOperation(EXIT_ON_CLOSE);

  EventQueue.invokeLater(new Runnable() {
    public void run() {
    f.setVisible(true);
    }
  });
}
Andrew Thompson - 24 Oct 2005 16:04 GMT
>>                this.setLayout(null);
>
> May not set JFrame layout directly - only to its content pane

Sure you can ..in 1.5.

From the 1.5 JFrame JDocs..
"As a conveniance* add and its variants, remove
and setLayout have been overridden to forward
to the contentPane as necessary."

..That means that there are a bunch of inexperienced
developers now using 1.5, that are writing code that
is not compatible with 1.4 JRE's.

<dripping with sarcasm>
Now that's 'conveniant'*, isn't it?
</dripping with sarcasm>

* Incorrectly spelt, as it happens.
Vova Reznik - 24 Oct 2005 16:11 GMT
>>>                this.setLayout(null);
>>
[quoted text clipped - 16 lines]
>
> * Incorrectly spelt, as it happens.

Then it shouldn't be

this.getContentPane().add...

just add...
Andrew Thompson - 24 Oct 2005 16:39 GMT
...
> Then it shouldn't be
>
> this.getContentPane().add...
>
> just add...

(chuckles) The OP's code, is another matter...
lmierzej - 24 Oct 2005 17:17 GMT
> Don't setVisible true before finishing construction

Thanks for help!

But what if I have many panels (with many components, every panel
gathers different functionality).
Should I create all of them at the start of my application (some of them
can never be used and displayed)? Or maybe create them on the fly (when
requested by user)?

Should I "hide/show" panels or "created/destroy"?

regards,
lmierzej
Vova Reznik - 24 Oct 2005 17:25 GMT
> But what if I have many panels (with many components, every panel
> gathers different functionality).

Create, set, pack, resize everything that you are going to show now
before setVisible(true)

> Should I create all of them at the start of my application (some of them
> can never be used and displayed)? Or maybe create them on the fly (when
> requested by user)?

Inspect your design. May be you need JDesktopPane with different
JInternalFrame(s) or CardLayout or something else.

Read:
http://java.sun.com/docs/books/tutorial/uiswing/index.html

Also
http://mindprod.com/

May "Gay's right" page will not help you with Swing
but Java and Applets are very helpful.

> Should I "hide/show" panels or "created/destroy"?

You may not hide/show JPanel   

> regards,
> lmierzej
lmierzej - 24 Oct 2005 17:40 GMT
> Inspect your design. May be you need JDesktopPane with different
> JInternalFrame(s) or CardLayout or something else.
[quoted text clipped - 4 lines]
> Also
> http://mindprod.com/

Thank you very much for useful links!

> You may not hide/show JPanel  

But I can remove JPanel from frame and save the reference to it and then
add once more. But I guess it's completly silly...

lmierzej
Roedy Green - 25 Oct 2005 05:49 GMT
>> Should I "hide/show" panels or "created/destroy"?
>
>You may not hide/show JPanel   

if you revalidate the JPanel's container, why not?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 25 Oct 2005 05:44 GMT
>Should I create all of them at the start of my application (some of them
>can never be used and displayed)? Or maybe create them on the fly (when
>requested by user)?

Do it both ways and ask your users which way they find more painful.

It is a psychological question really.  Unless you never use some of
the frames, both are going to take the same amount of time. It is just
a matter of when you make the user wait.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 25 Oct 2005 05:48 GMT
>Should I create all of them at the start of my application (some of them
>can never be used and displayed)? Or maybe create them on the fly (when
>requested by user)?

A Zen master was keeper of some monkeys.  Every day he fed them,
giving each monkey three large nuts a day. He fed them a nut in the
morning and a two nuts in the afternoon.

The monkeys complained they were hungry and being mercilessly starved.

So he decided to feed them two nuts in the morning and one in the
afternoon, and the monkeys were thoroughly placated.

.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Oliver Wong - 25 Oct 2005 22:21 GMT
> But what if I have many panels (with many components, every panel gathers
> different functionality).
> Should I create all of them at the start of my application (some of them
> can never be used and displayed)? Or maybe create them on the fly (when
> requested by user)?

   You probably don't want to get too fancy when you're still learning
Swing, but another possibility (if you have a VERY large amount of
components to set up) is to put a list of all the components in a queue, and
then in a seperate thread, create them while the user is using the program.

   If the user requests a panel that hasn't been created yet, you move the
requested panel to the top of the queue, giving it highest priority, so to
speak. To get even fancier than that, you can profile your user to see what
panels are used most often and in what order (perhaps via some sort of
Bayesian or Markov Chain predictor), save the profile in a file, and on next
start up, sort your queue in some sort of optimal way.

   All of this adds overhead to your application, which is why it probably
isn't worthwhile unless you have a very large number of components.

   - Oliver


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.