aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes ScrollPane trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ScrollPane trouble" Watch "ScrollPane trouble" New topic
Author

ScrollPane trouble

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I have a JPanel, that uses null layout.

Added to this is JScrollPane, that contains a JTable.

The JTable will often change its size, so I'd like theJScrollPane to fit around it nicely with no gaps,
as well as having a maximum size, say 500 by 300 so that the Scrollers appear as needed when the JTable gets too big.

I'm not sure how to do this, I need to give the JScrollPane a size as its in a null layout,
and I think I need to use revalidate on the JJScrollPane after the table is changed,
but I don't know how to set the size correctly, can anyone help? Thanks
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Why does the panel have a null layout?
Set the layout to BorderLayout and add the JScrollPanel with the JTable embedded inside it.

Check out set/getPreferredSize(). You can use this to define your panels dimensions. Of course honoring these dimensions would depend on the panel's parent's layout.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
I use null layout so I can arrange my components exactly where I want them.

I still have no clue on how to do what I wanted.

Thanks anyway
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
colin shuker wrote:I use null layout so I can arrange my components exactly where I want them.

I still have no clue on how to do what I wanted.


And neither do I. If you still need help, then post an SSCCE (I know you've seen this before). I also second the recommendation that you get away from using null layouts.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
ok, whats wrong with null layout?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

What isn't? Well ok, the most obvious drawbacks:
- you have to position every component yourself (may be what you want)
- it will not scale components if your user interfaces resizes
- it doesn't calculate the minimum, maximum and preferred sizes of components

A null layout is never a good idea. If you really need to manually position your components (like in a diagram editor or something, in just about every other situation you don't) you should still use a custom layout manager that at least has a body for preferredSize and minimumSize.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
colin shuker wrote:ok, whats wrong with null layout?


It's inflexible, it has a tremendous potential for bugginess (as you're finding out now), it hampers your ability to upgrade or modify your GUI...

With null layout, you yourself are completely responsible for the specifying the size and location of each and every component. If you have a GUI of any complexity then making any changes to your GUI (adding a component, moving a component) means more and more work for you and more chance for error. If the L&F of the program or setting changes (different screen resolution or different OS or machine), you'll often find labels with text that doesn't fix, or just plain ugliness.

There's more and I'm sure I'm forgetting something here...
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Well I'm making quite a complex gui, if I try and use GridBagLayout, I'll just have a nervous breakdown.
My gui is a fixed size, so it doesn't need to resize.
So it seems only problem is I can't get preferredSize or something.

I guess Ill have to try somehting else
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
colin shuker wrote:Well I'm making quite a complex gui, if I try and use GridBagLayout, I'll just have a nervous breakdown.

Solution: don't use GridBagLayout then. You often can do just as well or better nesting JPanels that each use a different simple layout. Also you may wish to look into MIgLayout, a layout manager that is not part of standard Java but can be downloaded for free and comes highly rated.

My gui is a fixed size, so it doesn't need to resize.
So it seems only problem is I can't get preferredSize or something.

I guess Ill have to try somehting else

Experiment. Play with it. Have fun!
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Ok, so basically I now have...


So this code displays the ScrollPane but the horizontal scrollBar appears at the very bottom, of the ScrollPane/panel.
There is a large gap between the JTable and the scrollBar.

I need the scrollBar to be directly under the table.

I used getPreferredSize on the timetablePane, and it seems to ignore the top row of table cells used to show the column names.
Setting the panel to this size seems to not to do anything.

Can anyone advise? Thanks
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
I used getPreferredSize on the timetablePane, and it seems to ignore the top row of table cells used to show the column names.


Yes, because the JTableHeader is a separate component.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Ok, heres an SSCCE



So here, with rows set to 10, we can see a gap, between the table and the horizontal scrollbar,
but with say rows = 30, then the vertical scrollbar kicks in, and its fine.

So I'd like the horizontal scrollbar to be directly below the table if possible.

Thanks for any help.>
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Switch

to
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689

colin shuker wrote:I use null layout so I can arrange my components exactly where I want them.

I still have no clue on how to do what I wanted.

Thanks anyway


Hi,

That's true using null layout you can place your component as you want.. But it good to have some layout better than null....

So, it you you want same to use as null they why don't you try SpringLayout.

SpringLayout provide you flexiblity to adjust your component where you want....


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Nishan,
I don't think SpringLayout is a good idea. SpringLayout is designed for DnD style GUI builders. Besides SpringLayout does not have the advantages of other layout managers.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689

Maneesh Godbole wrote:Nishan,
I don't think SpringLayout is a good idea. SpringLayout is designed for DnD style GUI builders. Besides SpringLayout does not have the advantages of other layout managers.


Hi Maneesh,

What is the reason behind SpringLayout is not a good idea. And what is DnD style GUI builders .. ???
Esmaeil Ashrafi
Ranch Hand

Joined: Feb 22, 2010
Posts: 73
Maneesh Godbole wrote:

Check out set/getPreferredSize(). You can use this to define your panels dimensions. Of course honoring these dimensions would depend on the panel's parent's layout.


I am so so happy ,my problem is solving !
I just wrote a program to exercize Graphics and GUIs,had a little making desired scrolling
i myself tried a bunch of tricks ( took days ) and have a look at javadoc ( as my habbit ) and then forums...yesterday i register in a forum,my post did'nt have been answered,and after a few hours,my topic was'nt in the top page.From netbeans site i linked here
and

but let's talk about my program that still have a bit of problem :
before adding this line ( setting prefferedSize ) the picture below was the reason made me sad :



so there is no problem with horizontal scrollbar(if i reduce the width of window)
but for vertical one if i reduce the height of window,scroll bar does'nt appear untill the height becomes as small as just a little of image remains,and then appears,that is it cant find out the height of panel "canvas" and sets its default size to a small size(I think)

here is the constructor method of frame and the main method :


and now, with help of you dear,and adding


this is the result :

so the problem is :
i want to fully have the image ,in this picture,the scale set to 160%

what can i do dear friends ?

thank you for any help

esmaeil


I'm really tired of being engaged with stuff other than Java and programming

Wish to get back soon to my love...
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744

Switch
view plaincopy to clipboardprint?

1. this.getContentPane().add(outerPanel);

this.getContentPane().add(outerPanel);
to
view plaincopy to clipboardprint?

1. this.getContentPane().add(panel);


I tried this, it still has the scrollbar at the bottom of the screen with a gap.
Anyone have any other suggestions? Thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
@ esmaeil ashrafi
"but let's talk about my program that still have a bit of problem : "

better to start your own thread instead of hijacking this one
(you'll get more people looking at it that way)

@ OP

from your SSCCE, add/change this

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
I need the scrollBar to be directly under the table.


Well, then you need to set the preferred size of the scrollpane to whatever you want. You would obviously need to adjust the height based on the number of rows in the table. And you can't add the scrollpane to a pane using a BorderLayout, since a BorderLayout does not respect the preferred size of the component.

Also, you should not be using a null layout on your main panel.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Big thanks to Mr Dunn for his code change, and others that commented too.

I see the value of 4 is correct for the inset.

I just have one problem, when the table is small and there are no scrollbars, there is like a grey gap
where the scrollbars normally fit.

I tried to change the colour of this using .setBackground(...) but that didn't work.

Alternatively, if I can retrieve from the code whether or not the scrollbars exist,
then I can shrink the panel a little more to hide this grey area.

Any suggestions? Thanks
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
Or a slightly different approach so you don't have to worry about the "inset somewhere":



However, the real problem with you code is because you are using a null layout. Learn to use layout managers its much simpler and you don't need to use these hacks:

Esmaeil Ashrafi
Ranch Hand

Joined: Feb 22, 2010
Posts: 73
Michael Dunn wrote:@ esmaeil ashrafi
"but let's talk about my program that still have a bit of problem : "

better to start your own thread instead of hijacking this one
(you'll get more people looking at it that way)


Thank you for your direction(this is my first activity in an international forum nad im not familiar with regulations here very well right now)
But,
I just wanted avoid redundancy,and trying not to make the forum heavy !

however i started a new thread,and will be glad if you take a look at it
Sincerely
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi again, I tried that 2nd variation but the it didn't work quite right with 2 scrollbars.
Using the 1st code, I have the program below which works fine, excpet for when table goes small,
there are grey areas where the scrollbars normally go, can anyone see a simple solution?
I tried colouring them but couldn't get it to work..

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You might also want to set the JScrollPane's viewport green:

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks that works well, there just 1 little problem now..

When the table is small, like 2 by 2, a small grey square is present at the right of the column headers.

I don't have a clue how to fix this, please let me know if anyone has any ideas, thanks.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Using the 1st code, I have the program below which works fine, excpet for when table goes small,
> there are grey areas where the scrollbars normally go, can anyone see a simple solution?

a couple of changes to your last SSCCE
(the grey areas are from setting the size to include width or height for scrollBars, when none are required/showing)

this will still sometimes leave a vertical scrollbar when its not really required (all rows visible)
possibly an inset adjustment (v scrollbar shows up when h scrollbar is required), but I'll leave it for you to track down

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks, I thought the last thread was getting out of control and figured there might just be a simple solution if I restate what I needed.

I tried the code given, but it doesn't work, I don't have a clue whats going on with that viewport stuff.

I made my own code by working out the size of table, and adding scrollbars if needed, but its a bit clumsy and it fails when theres a horizontal scrollbar, cause then the pane is too small to fit all the rows.

I guess if you guys can't do it, no point me even trying, but thanks anyway.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I tried the code given, but it doesn't work,

that's extremely helpful info

quite likely you just added the indicated lines to your existing code.
instead, try copy/paste all of the code I posted into a new project/file, then compile/run that.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thats what I did, and sometimes the vertical scrollbars didn't appear, you had to scroll down the table by pressing on it.
Also, it doesn't say anywhere about the maximum size the scrollpane should be before the horizontal and/or vertical scrollbars appear.
I wanted something like 400 by 400.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ScrollPane trouble