Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPanel on JScrollBar possible?

 
Sandeep Mukherji
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*
I made two JPanel panel and p. Then pasted labels, tesxtFields and button
Then pasted that panel p to panel and put it on the scrollBar. scrollBar is on the JFrame in turn. The size of the scrollBar is set lesser than the panel. Still the scrollbars are not working. I can not see the whole form.
Can somebody help please.
*/
package exp;
import javax.swing.*;
import java.awt.*;
/**
* @author Sandeep Mukherji
*
*/
public class Invoice_Generate extends JFrame{
JScrollPane scrollPane;
JPanel panel,p;
JLabel label;
JTextField textField;
JButton button;
Invoice_Generate(){
super("Name");
setVisible(true);
setSize(400,200);
setLayout(null);
panel = new JPanel();
panel.setSize(300,100);
panel.setLayout(null);
//panel.setVisible(true);
panel.setBackground(Color.YELLOW);
label = new JLabel("Label");
textField = new JTextField();
button = new JButton("add");
label.setBounds(0,0,100,30);
textField.setBounds(120,0,100,30);
button.setBounds(0,50,80,30);
panel.add(label);
panel.add(textField);
panel.add(button);
p = new JPanel();
p.setVisible(true);
p.setSize(500,500);
p.setLayout(new BorderLayout());
p.add(panel, BorderLayout.CENTER);
scrollPane = new JScrollPane(p);
//scrollPane.add(panel);
scrollPane.setSize(300,100);
//scrollPane.setLayout(new ScrollPaneLayout());
scrollPane.setVerticalScrollBarPolicy(scrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(scrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//scrollPane.se
scrollPane.setVisible(true);
add(scrollPane);
}
public static void main(String a[]){
new Invoice_Generate();
}
}
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try setting the size of the panel as below:
Set your frame to fix the size and now you may get vertical bar.
 
Sandeep Mukherji
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Gopu. It is working now.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could managed to get the vertical bar for the scrollpane through setting the setPreferredSize for the panel. Is tehre any way to get the horizontal bar?
 
Sandeep Mukherji
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, you can use

This will fix the size of the columns and you will get the horizontal scrollbar.
I hope this will help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic