• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding Scrolling Functionality to Button

 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I have already submitted the topic in coderanch.com before one month, but I didn't get a correct solution yet.

I will explain the scenario in a clear section...

"I have a JPanel contains many labels (eg:LogPanel, which shows one label in each row) and the I set the layout of the panel to GridLayout(0,1).
When the JPanel loads all the labels are appearing in the panel (the reason is why I put the GridLayout to (0,1)), is there any way to set a default
row size for the GridLayout and it has to change when the number of rows increasing(that is my first doubt).
The second problem is I have two JButtons (eg: UPButton and DOWNButton), and when I click on the UPButton, the JPanel(LogPanel) has to move one row up and when I click on the DOWNButton, the JPanel(LogPanel) has to move one row down.

I was already used JScrollPane to do that, but it is not working properly and one more thing, I don't need to display the JScrollPane to the JPanel.

Please help me...............................
Thanks In Advance
 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello............................
Please need a response!!!
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello............................
http://faq.javaranch.com/java/PatienceIsAVirtue!!!
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you probably shouldn't be using a GridLayout. The panel does not automatically change size as components are added to it. Instead the components are resized to fill the spac of the panel.

You should probably be usinging a BoxLayout, which is easily done by using:

Box panel = Box.getVerticalBox();
panel.add(component1);
panel.add(component2);

As you add components to the Box you need to revalidate() the Box so the preferred size can be recalculated. Then scrollbars will appear automatically when required.

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.

Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.
 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob,
Yes, you are correct. In GridLayout, components are resized to fill the space of the panel. I can try with the BoxLayout and I will set the preferred size of the panel as you said and I will let you know the result.
If my issue did not solved, then I will create a SSCCE and let you know...
I have one more doubt, please conside this also.
Is there any way to add JComponent (like JLabel, JButton) to JTable.

Once again
thanks Rob......


Renjith
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will set the preferred size of the panel as you said


That is not what I said. The job of the layout manager is to calculate the preferred size automatically for you. You don't need to do anything. If you do set the size manually, then your scollbars will NOT work.
 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
Thanks.
I was busy with my work for this week.
I set the panel layout to BoxLayout and added scrollpane to it. But all the components are again resized to fill the space of the panel.
Is there any other way to add scrolling functionality to the Buttons like using JTable or so..

Rob,

I have one more doubt,

Is there any way to add JComponent (like JLabel, JButton) to JTable.


Thanks in Advance...........
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From javax.swing.BoxLayout's documentation.
BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout).


Set preferred sizes for your components and add them to some JPanel which implements javax.swing.Scrollable interface and the panel you add to JScrollPane
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But all the components are again resized to fill the space of the panel.


You still haven't posted a SSCCE so I have no idea what your problem is.
 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mikko,
I did not set the preferred size of the components.

The components I am adding in a Panel are JLabels, so I will set the method setPreferredSize() for the JLabels (I was using Labels extends from JLabel) and I will let you know the result.

Very much thanks for your time, Mikko.




Hi Rob,

I have to rearrange the program to post as SSCCE, because the program uses many extended classes, that is extending JButtons,JLabels, JTextField and so on. So I will check, what Mikko said and after that I will post the program.

Anyway, very very much thanks for your time, Mr.Rob......

Regards..........
Renjith M
 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

Sorry for the late reply.
I was busy with my other works.
I have added the scrolling functionality to the buttons.
I used a JTable and added the scrollpane to it and then the scrolling button can work together with the scrollbar.setValue() method.
So there is not any issue with the layout (in my problem).
All the data are displaying via the scroll buttons.

Thanks Rob......

I have one more doubt,

Is there any way to add JComponent to a JTable.
I need to add JButton to a JTable.


Please help me with this too.


Thanks in Advance.
Renjith M
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to add JButton to a JTable.


Table Button Column
 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob Camick and Rob Prime,

I will study the topics that you have sent and I will let you know if I have any problem with the implementation.

This is the one website, from here I am getting the correct answer.

Thanks to that also.



Thanks
Renjith M
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!