• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

ScrollPane trouble

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, whats wrong with null layout?
 
Sheriff
Posts: 22850
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Switch

to
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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....
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 .. ???
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might also want to set the JScrollPane's viewport green:

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic