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);

}

}

Yannixa at 2007-7-29 13:36:08 > top of Java-index,Desktop,Core GUI APIs...
# 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

s1au_k14a at 2007-7-29 13:36:08 > top of Java-index,Desktop,Core GUI APIs...
# 3

I think that you use setPreferredSize when you are using layout managers, since the manager actually sets the size of the component, and will use the preferred size if possible, but not necessarily. Since the example produced above uses no layout manager (it has a "null" in the place of a layout manager), I think that you can use setSize. I am no Swing expert though, so take this with a grain of salt.

petes1234a at 2007-7-29 13:36:08 > top of Java-index,Desktop,Core GUI APIs...
# 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.

camickra at 2007-7-29 13:36:08 > top of Java-index,Desktop,Core GUI APIs...
# 5

what LayoutManagers that is usually used? i think not all of them are good. any comment?

what is the difference between setSize and setPreferredSize?

s1au_k14a at 2007-7-29 13:36:08 > top of Java-index,Desktop,Core GUI APIs...
# 6

Read the tutorial and use a combination of layout managers to get your desired layout. You learn by experience.

> what is the difference between setSize and setPreferredSize?

Never use setSize().

The layout manager takes the information provided by the setPreferredSize() method and calculates the size based on the rules of the layout manager.

camickra at 2007-7-29 13:36:08 > top of Java-index,Desktop,Core GUI APIs...