Java Swing GUI
HI all, I am Cochu. I am building a Swing GUI that contains components like Jcheck boxes,JList,JButtons,JComboBox etc. All these components are added to a JFrame . Now I want to add a scrollbar to this window. When I used the following code it converts all my window to a scrollbar !! I think it is the problem of my layout. I set the layout as null before. Now to add scrollbar I changed it to BorderLayout. Can anybody give me a solution. My code is like this:
String text = "Cochu";
JTabbedPane documents = new JTabbedPane();
JTextPane editor = new JTextPane();
editor.setText(text);
//add editor to the tabbed pane
documents.add("doc1", editor);
// put the tabbed pane in the scroll pane
JScrollPane editorScroll = new JScrollPane(documents);
parentpanel.setLayout(new BorderLayout());
parentpanel.add(editorScroll);
Also I want a vertical scrollbar not a horizontal one.
[952 byte] By [
Cochua] at [2007-10-2 6:02:45]

create a frame. create a panel.Add all your widgets like to this panel.create a scrollpane and add this panel to your scroll pane.add this scroll pane to your frame. If you follow this steps . I think your program should work.
you need to change the preferred size of your components:
String text = "Cochu";
JTabbedPane documents = new JTabbedPane();
JTextPane editor = new JTextPane();
editor.setText(text);
//add editor to the tabbed pane
documents.add("doc1", editor);
documents.setPreferredSize(new Dimension(50, 500));
// put the tabbed pane in the scroll pane
JScrollPane editorScroll = new JScrollPane(documents);
editorScroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
editorScroll.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
parentpanel.setLayout(new BorderLayout());
parentpanel.add(editorScroll);
parentpanel.setPreferredSize(new Dimension(300, 300));
hope this helps,
p.s. you are using the default layout which is FlowLayout rather than a null layout, the effects are massively different.
Ken_Sa at 2007-7-16 13:03:10 >

Hi yar this is also not working properly. But when I changed the layout as Flow Layout atlast the scrollbar appeared!! But all my components are scattered here and there. Can you tell me a way out? Thanks for your willingness to help me.Regards Cochu.
Cochua at 2007-7-16 13:03:10 >

i think there must be a problem with another part of your code. What I posted previsouly works in the following context:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
public class TestAreaJFrame extends JFrame{
public TestAreaJFrame() {
String text = "Cochu";
JTabbedPane documents = new JTabbedPane();
JTextPane editor = new JTextPane();
editor.setText(text);
//add editor to the tabbed pane
documents.add("doc1", editor);
documents.setPreferredSize(new Dimension(50, 500));
// put the tabbed pane in the scroll pane
JScrollPane editorScroll = new JScrollPane(documents);
editorScroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
editorScroll.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(editorScroll);
this.setPreferredSize(new Dimension(300, 300));
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String [] args){ new TestAreaJFrame(); }
}
what else are you trying to do? (or what other componenets are you trying to add and where?)
Ken_Sa at 2007-7-16 13:03:10 >

Hi Ken I modified your code just to add a JButton .But the button is only visible upon maximizing the window that also in an unusual way! You just try to run the follwing code and see it. Since I am just a biginner I may be doing lots of mistakes. But you please tell me even if it is a small thing.
Thank you,
Cochu.
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.JButton;
public class TestAreaJFrame extends JFrame{
public TestAreaJFrame() {
String text = "Cochu";
JTabbedPane documents = new JTabbedPane();
JTextPane editor = new JTextPane();
editor.setText(text);
//add editor to the tabbed pane
documents.add("doc1", editor);
documents.setPreferredSize(new Dimension(50, 500));
// put the tabbed pane in the scroll pane
JScrollPane editorScroll = new JScrollPane(documents);
editorScroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
editorScroll.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(editorScroll);
this.setPreferredSize(new Dimension(300, 300));
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.pack();
JButton test = new JButton("TEST");
test.setBounds(50,100,150,25);
this.getContentPane().add(test);
this.setVisible(true);
}
public static void main(String [] args){ new TestAreaJFrame(); }
}
Cochua at 2007-7-16 13:03:10 >

heres what you did wrong:
you explicitly set the bounds using test.setBounds(50,100,150,25);
This is the wrong thing to do (you very rarely want to do this, and never in conjunction with a layout manager). Just add the object to the panel and let the layout manager decide where to put it. Then it is up to you to manipulate the layout managers so that they do the right thing.
(also, you should call this.pack() after adding all of you components as that tells the layout managers to sort themselves out. However, they normally get themselves right anyway, it's just good practice).
here is the code again, but I am telling to border layout that the button belongs in the south border:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.JButton;
public class TestAreaJFrame extends JFrame{
public TestAreaJFrame() {
String text = "Cochu";
JTabbedPane documents = new JTabbedPane();
JTextPane editor = new JTextPane();
editor.setText(text);
//add editor to the tabbed pane
documents.add("doc1", editor);
documents.setPreferredSize(new Dimension(50, 500));
// put the tabbed pane in the scroll pane
JScrollPane editorScroll = new JScrollPane(documents);
editorScroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
editorScroll.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(editorScroll);
this.setPreferredSize(new Dimension(300, 300));
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JButton test = new JButton("TEST");
//test.setBounds(50,100,150,25);
this.getContentPane().add(test, BorderLayout.SOUTH);
this.pack();
this.setVisible(true);
}
public static void main(String [] args){ new TestAreaJFrame(); }
}
remember you can put components inside JPanels with diffrent layout managers in them, then add these JPanels to the rootPane or another JPanel. This way you can you diffrent layout managers in diferent areas.
e.g. try:
JButton test = new JButton("TEST");
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bottomPanel.add(test);
this.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
here is a Tutorial on using layout managers in cae you want to know more: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
Ken_Sa at 2007-7-16 13:03:10 >

Yes you got it right. I must say you are very good in Java. I tried your second option by giving border layout only to the scroll bar. Now the scroll bar appeared. But it scrolls only the editor pane contents. How to make the entire Frame contents to scroll. I mean if there is a methode to do this as you used setText() to set the textual content. Sorry if I am wasting your time asking stupid questions.
Thanks a lot
Regards ,
Cochu
Cochua at 2007-7-16 13:03:10 >

Sorry for not replying over the weekend, here is what you want to do: add all of your components to a JPanel, using whatever layout managers you want. Then add that JPanel to the ScrolPane. here is an example:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.JPanel;
public class TestAreaJFrame extends JFrame{
public TestAreaJFrame() {
// create the bottome panel in a jpanel:
JButton test = new JButton("TEST");
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bottomPanel.add(test);
// create the editor (jtextpane)
String text = "Cochu";
JTextPane editor = new JTextPane();
editor.setText(text);
// add these to a jpanel:
JPanel panelToScroleOver = new JPanel();
panelToScroleOver.setLayout(new BorderLayout());
panelToScroleOver.add(editor, BorderLayout.CENTER);
panelToScroleOver.add(bottomPanel, BorderLayout.SOUTH);
// add this to a scrole pane
JScrollPane editorScroll = new JScrollPane(panelToScroleOver);
editorScroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
editorScroll.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// add that to a tabed pane:
JTabbedPane documents = new JTabbedPane();
documents.add("doc1", editorScroll);
documents.setPreferredSize(new Dimension(50, 500));
//add to Frame
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(editorScroll);
this.setPreferredSize(new Dimension(300, 300));
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.validate();
//this.pack();
this.setVisible(true);
}
public static void main(String [] args){ new TestAreaJFrame(); }
}
I think this does what you want, you may need to add some extra text into the text area and shrink the size of the frame before you can see it scrolling thou.
try playing with adding diffrect components and layout managers to panelToScroleOver.
Ken_Sa at 2007-7-16 13:03:10 >

Thank you Ken , you gave me the correct solution. Again a hundred thanks for helping me.Regards ,Cochu.
Cochua at 2007-7-16 13:03:10 >
