need help
hello buddy,currently i am working to make a program for my auntie however i got a problem right here..
this is my code :
import javax.swing.*;
import java.awt.*;
publicclass scrollInTabbedPane{
public scrollInTabbedPane(){
JFrame frame =new JFrame("Testing");
frame.setResizable(false);
JTabbedPane tab =new JTabbedPane();
tab.addTab("JAVA",new firstPanel());
frame.add(tab,null);
frame.setSize(800,600);
frame.setVisible(true);
}
publicstaticvoid main(String[]args){
new scrollInTabbedPane();
}
}
class firstPanelextends JPanel{
public firstPanel(){
setLayout(null);
JLabel label1 =new JLabel("JAVA");
label1.setBounds(new Rectangle(10,528,100,25));
add(label1);
}
publicstaticvoid main(String[]args){
new firstPanel();
}
}
the problem is i want to see the "JAVA" word with out resize the frame which mean i want a scrollable page in the tabbedpane page.
if i need to use JScrollPane,how to use it so that i can see the "JAVA" word?
thanks
[2355 byte] By [
s1au_k14a] at [2007-11-27 11:09:33]

# 1
is this what you are trying to accomplish?
copy and paste:
import javax.swing.*;
import java.awt.*;
public class sample1{
public sample1(){
JFrame frame = new JFrame("Testing");
frame.setResizable(false);
JTabbedPane tab = new JTabbedPane();
JPanel panel = new JPanel();
JScrollPane pane = new JScrollPane();
pane.setViewportView(new firstPanel());
pane.setBounds(10,10,150,200);
panel.setLayout(null);
panel.add(pane);
tab.addTab("JAVA",panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tab,null);
frame.setSize(800,600);
frame.setVisible(true);
}
public static void main(String[]args){
new sample1();
}
}
class firstPanel extends JPanel{
public firstPanel(){
setLayout(null);
setPreferredSize(new Dimension(100,800));
JLabel label1 = new JLabel("JAVA");
label1.setBounds(5,600,100,100);
add(label1);
}
}
# 2
yeah...
thank you so much..
i have a couple questions..
why can't i use setSize instead of setPreferredSize in the firstPanel class?
one more, why use pane.setBounds(10,10,150,200); in order to show the pane including the panel.setLayout(null)?
thanks
# 4
The general rule is "don't use a null layout manager". Read the Swing tutorial on "How to Use Layout Managers".
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
Understand how they work and use them and you won't have the type of problem you are having with your demo code.
Regarding scroll panes, the scroll bars will appear automatically when the "preferred size" of the component added to the scroll pane is greater than the "size" of the scroll pane.
If you use LayoutManagers, then they will calculate the preferred size of each component so you don't have to do any work.
If you use a null layout manager, then you are responsible for calculating the preferred size of every component. Much more work.