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

Scrollbar for jtable

janavi patel
Greenhorn

Joined: Jul 01, 2009
Posts: 13
hello friends..

Please tell me how to add vertical and horizantal scrollbar in jtable?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9


Swing related. Moving.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
janavi patel
Greenhorn

Joined: Jul 01, 2009
Posts: 13
Thanks for reply...

but I already tried everything......and nothing is helpful for me........
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Does the panel to which you are adding the table, has a BorderLayout?
It will help us help you better if you tell the details
Also check out this link on how to get the most out of the Ranch.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Then you're definitely doing something wrong, because the code Maneesh provided should do just what you need.

Can you show the bit of your code where you add your table to its parent?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> but I already tried everything......and nothing is helpful for me........

then you're possibly using a null layout, and not giving anything a size.

we can continue to play 20 guesses, or, if you really want a solution, post your code.
Rohit Aggarawal
Ranch Hand

Joined: Jun 26, 2009
Posts: 39
Use this. It will work ..

JScrollPane scrollPane = new JScrollPane(instance_of_Table);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_ALWAYS);
add(scrollPane, BorderLayout.CENTER); (Add should be called for any container....FRame or JPAnel)


SCJP 6.0
SCBCD for JEE 5

Rohit Aggarawal
Ranch Hand

Joined: Jun 26, 2009
Posts: 39
Or send me your code...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Rohit,
Please read this: http://faq.javaranch.com/java/UseTheForumNotEmail
janavi patel
Greenhorn

Joined: Jul 01, 2009
Posts: 13
here is my code

Rohit Aggarawal
Ranch Hand

Joined: Jun 26, 2009
Posts: 39
Ok. I got it...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9



Please see my comments.
Recommendations:
1) You do not need p. Discard it.
2) Use p1.add(scrollPane)
3) Use c.add(p1)
4) Even, c.add(scrollPane) will do, if your UI shows only the table.

Recommended reading
http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
janavi patel
Greenhorn

Joined: Jul 01, 2009
Posts: 13
I tried this code but this is also not working.....
janavi patel
Greenhorn

Joined: Jul 01, 2009
Posts: 13
Thanks Manish

but i tried that one also. when i use BorderLayout() it shows empty frame and nothing in it...

i really fade up of this problem....
Dany Rouss
Greenhorn

Joined: Jul 02, 2009
Posts: 2
[MG]Edited to remove hijack.

Danny,
Welcome to the Ranch.
Please start a new thread for your question.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

janavi patel wrote:
but i tried that one also. when i use BorderLayout() it shows empty frame and nothing in it...


Like you have been told before, unless you show us your code, we cannot really help you..and even people will get fed up and stop helping you.
So, show us your code
Vandre Caetano
Greenhorn

Joined: Dec 19, 2007
Posts: 15
I changed a few things in the code, here is one that works:



I thing you mixed up a few things like:
# p1.add(header);
# p1.add(table);
# p1.add(pane);
you use this only when there is no scrollpane in the table: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

I hope it helps
janavi patel
Greenhorn

Joined: Jul 01, 2009
Posts: 13
Here is my another code....

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
Why are you trying to write an entire program when you are trying to learn a new concept?. Your question is about a JTable and a scrollbar. People have told you to use a JScrollPane. So only include those two classes in your test programa and keep it simple. Something like this:

janavi patel
Greenhorn

Joined: Jul 01, 2009
Posts: 13
Are yaar*.... all things are related and impacting each other...thats why....

*Are yaar=="Hey dude" or "Man"

[MG]
Janavi,
We got people from all over the world here.
Please use real words
[/MG]
Rohit Aggarawal
Ranch Hand

Joined: Jun 26, 2009
Posts: 39
Code is slightly modified for the method MyFrame8 and that works now. Please use this piece of code...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Rohit,
Please use code tags
You can add them to your existing post by clicking on the button
Rohit Aggarawal
Ranch Hand

Joined: Jun 26, 2009
Posts: 39
Maneesh,

Done as per your suggestion.

-Rohit
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Thank you
janavi patel
Greenhorn

Joined: Jul 01, 2009
Posts: 13
Hi Rohit..
I tried this code:



but still its not showing a table and positions of all other components is different because of p = new JPanel(); ....

and if I used p = new JPanel(null); then components positions are right as they defined but still not showing a table.......
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Did you try out Rob Camick's code?
janavi patel
Greenhorn

Joined: Jul 01, 2009
Posts: 13
Yes, I tried that.....
It works when there is only container and not panel and it hides all other components....
And when panel is used all positions are proper bue there is no scrollbar....
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Yes, I tried that.....

and, as usual, it worked.


> It works when there is only container and not panel and it hides all other components....
> And when panel is used all positions are proper bue there is no scrollbar....

then, why don't you post your (altered Rob Camick's) code to prove the above?
Rohit Aggarawal
Ranch Hand

Joined: Jun 26, 2009
Posts: 39
Dear janavi patel

You can use the modified code that was posted by me earlier and It will run like anything. I took not more than 1 minute to modiffy and run your code.
I am still wondeing why that does not work on your machine.

-Rohit
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

"Rohit agl",
Please check your private messages for an important administrative matter.
janavi patel
Greenhorn

Joined: Jul 01, 2009
Posts: 13
Hi Rohit
I tried your code snippet in my program as follows:


1. Now it adds scrollpane to the table but not showing other components.
2. Also shows srollable table for full frame. For this I tried pane.setBounds() but no effect.
3. Doesn't matters panel's layout even whether panel is used or not.


Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

janavi patel wrote:
1. Now it adds scrollpane to the table but not showing other components.
2. Also shows srollable table for full frame. For this I tried pane.setBounds() but no effect.
3. Doesn't matters panel's layout even whether panel is used or not.


I am not surprised. You have chosen to ignore the hints/ready made code provided to you.
Recommended reading
http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html
janavi patel
Greenhorn

Joined: Jul 01, 2009
Posts: 13
manish
this is last time I am telling to you I tried all hints and every other thing that I can do...
if you cant help or dont want to help then its more better for both of us that you just stay away from this matter.......
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

janavi patel wrote:manish
this is last time I am telling to you I tried all hints and every other thing that I can do...
if you cant help or dont want to help then its more better for both of us that you just stay away from this matter.......


To start with I am a volunteer here and I do not get paid for this work. Even if I was getting paid, I still do not have any reason to put up with this kind of language.
I do not see you trying out my suggestions which I gave here I wonder if you found time to go through the links I had provided.
Michael Dunn (another volunteer) had requested you to
then, why don't you post your (altered Rob Camick's) code to prove the above?
. You did not respond to it. BTW, Rob Camick is another volunteer.

If you want to learn, it might be a good idea to try out suggestions. You can always ask why. If you want ready made solutions (and the learning is not really important), then you are in the wrong place. All volunteers here are entitled to some respect.

Have a nice day and best of luck with your code.
janavi patel
Greenhorn

Joined: Jul 01, 2009
Posts: 13
sorry for that.....
actually I never behave like this with anyone but everytime manish replied against me.....
everytime i said i tried and you just opposed me and nothing else thats why...........
and in my code i did too many changes so its not possible for me to send the code......
and I want to learn thats why I am doing all these things.......

sorry once again......
and I m saying sorry not because you are a volunteer but because of this is not in my nature.......
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
I did too many changes so its not possible for me to send the code......


You learn by walking, not running. Forget about your real program, you are just trying to learn basic concepts, so you start with a basic program.

Why do you think I gave you the 10 line complete example program?

That program shows how to display a table with scrollbars. The next step is to add a couple of text fiields. If it works great, if not, then you have a 10-15 line program to post in the forum that we can look at. Once that works you move on to the next step.

Finally, when you understand how LayoutMangers work and how to you add components to a panel you go back to your real program to figure out what you did wrong. We are not here to debug you program for you. Frankly, if you can't figure out how to add components to a panel then you shouldn't be doing Swing programming. This should be the first think you learn because it only gets more difficult.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> actually I never behave like this with anyone but everytime manish replied against me.....

you've missed the point, totally. There's nothing 'personal' in it.

all everyone is doing is trying to get you to think about what you're doing, how you're going about it,
giving you some hints/nudges in the right direction.
This makes it much easier for you the next time you have a problem.

Dipanjan Kailthya
Greenhorn

Joined: Jul 04, 2009
Posts: 20
Another problem is that you're creating and displaying your GUI objects in the main thread. All that should really be done in the event dispatching thread. If you're using java6, try http://java.sun.com/javase/6/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scrollbar for jtable