Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
The attached pic is the Editor thing I am working on, and i cannot for the life of me get JScrollPane working on it.. Please help me out!

Code:
        //Creating JFrame for the Edit window
        JFrame text = new JFrame("Text Field/Edit");
        //Need the MenuBar for the Save choice
        JMenuBar menuBar = new JMenuBar();
        text.setDefaultLookAndFeelDecorated(true);
        //A container to hold all the content
        Container content = text.getContentPane();
        text.setBackground(Color.GRAY);
        text.setSize(800,300);
        text.setLocation(500, 500);
        //Setting the JPanel for the JTextArea and the ScrollBar
        JPanel jp = new JPanel();
        //jp.setLayout(new FlowLayout());
        //this.field is the JTextField
        this.field = new JTextArea(txt,70, 62);
        this.field.setRows(70);
        this.field.setColumns(62);
        //Menu Bar Option
        JMenu file = new JMenu("File");
        
        //Does this not set the ScrollBar?
        JScrollPane jsp = new JScrollPane(this.field); 
        //The line below this one, I thought this was suppose to set it, but doesnt.
        //jsp.getViewport().add(this.field);
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        JMenuItem save = new JMenuItem("Save");
        file.add(save);
        save.setActionCommand("save");
        save.addActionListener(this);
        menuBar.add(file);
        text.setJMenuBar(menuBar);
        jp.add(this.field);
        jp.add(jsp, BorderLayout.CENTER); 
        content.add(jp);
        text.setVisible(true);
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
Try using
Code:
jsp.setViewportView(this.field);
? Is it really the text field you're trying to scroll, or the content pane? Also I do not see an attached pic...
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
Sorry about that.. I am trying to add it to just the JTextArea
 

Attachments

  • scrollbar.JPG
    scrollbar.JPG
    119.3 KB · Views: 177

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
What you are doing is almost correct:

First, create the text area (you are already doing this):
Code:
textArea = new JTextArea(5, 30);

Next, create the scroll pane (you are already doing this):
Code:
JScrollPane scrollPane = new JScrollPane(textArea);

Set the preferred size of the scroll pane:
Code:
scrollPane.setPreferredSize(new Dimension(450, 110));

Add the scroll pane to the container (you are adding both the text area and the scroll pane - you should only add the scroll pane.):
Code:
container.add(scrollPane, BorderLayout.CENTER);
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
mrichmon said:
What you are doing is almost correct:

First, create the text area (you are already doing this):
Code:
textArea = new JTextArea(5, 30);

Next, create the scroll pane (you are already doing this):
Code:
JScrollPane scrollPane = new JScrollPane(textArea);

Set the preferred size of the scroll pane:
Code:
scrollPane.setPreferredSize(new Dimension(450, 110));

Add the scroll pane to the container (you are adding both the text area and the scroll pane - you should only add the scroll pane.):
Code:
container.add(scrollPane, BorderLayout.CENTER);


So I removed the line of code you told me to. So i am only adding that Scroll to the Pane... But now i do not even have a text area to write on... Nor a scroll bar.

Code:
        //Creating JFrame for the Edit window
        JFrame text = new JFrame("Text Field/Edit");
        //Need the MenuBar for the Save choice
        JMenuBar menuBar = new JMenuBar();
        text.setDefaultLookAndFeelDecorated(true);
        //A container to hold all the content
        Container content = text.getContentPane();
        text.setBackground(Color.GRAY);
        text.setSize(800,300);
        text.setLocation(500, 500);
        //Setting the JPanel for the JTextArea and the ScrollBar
        JPanel jp = new JPanel();
        //jp.setLayout(new FlowLayout());
        //this.field is the JTextField
        this.field = new JTextArea(txt,70, 62);
        this.field.setRows(70);
        this.field.setColumns(62);
        //Menu Bar Option
        JMenu file = new JMenu("File");
        
        //Does this not set the ScrollBar?
        JScrollPane jsp = new JScrollPane(this.field); 
        //The line below this one, I thought this was suppose to set it, but doesnt.
        jsp.getViewport().add(this.field);
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jsp.setPreferredSize(new Dimension(450, 110));
        JMenuItem save = new JMenuItem("Save");
        file.add(save);
        save.setActionCommand("save");
        save.addActionListener(this);
        menuBar.add(file);
        text.setJMenuBar(menuBar);
        jp.add(jsp, BorderLayout.CENTER);
        content.add(jsp, BorderLayout.CENTER);
        content.add(jp);
        text.setVisible(true);
    }
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
jtalerico said:
So I removed the line of code you told me to. So i am only adding that Scroll to the Pane... But now i do not even have a text area to write on... Nor a scroll bar.

Without seeing the rest of the code it is difficult to see what is going on. Maybe you have set the window size to be a fixed size that is large enough that scroll bars are not necessary on this text.

As a style hint, you should be using better variable names and spacing out your code a little.

Anyway, I've just thrown this sample code together which does what you are asking about:

Code:
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

public class TextAreaDemo extends JPanel {

	private static String TEXT_VALUE = "HORATIO:\nO, my dear lord,--\n\nHAMLET:\n\nNay, do not think I flatter;\nFor what advancement may I hope from thee\nThat no revenue hast but thy good spirits,\nTo feed and clothe thee? Why should the poor be flatter'd?\nNo, let the candied tongue lick absurd pomp,\nAnd crook the pregnant hinges of the knee\nWhere thrift may follow fawning. Dost thou hear?\nSince my dear soul was mistress of her choice\nAnd could of men distinguish, her election\nHath seal'd thee for herself; for thou hast been\nAs one, in suffering all, that suffers nothing,\nA man that fortune's buffets and rewards\nHast ta'en with equal thanks: and blest are those\nWhose blood and judgment are so well commingled,\nThat they are not a pipe for fortune's finger\nTo sound what stop she please. Give me that man\nThat is not passion's slave, and I will wear him\nIn my heart's core, ay, in my heart of heart,\nAs I do thee.--Something too much of this.--\nThere is a play to-night before the king;\nOne scene of it comes near the circumstance\nWhich I have told thee of my father's death:\nI prithee, when thou seest that act afoot,\nEven with the very comment of thy soul\nObserve mine uncle: if his occulted guilt\nDo not itself unkennel in one speech,\nIt is a damned ghost that we have seen,\nAnd my imaginations are as foul\nAs Vulcan's stithy. Give him heedful note;\nFor I mine eyes will rivet to his face,\nAnd after we will both our judgments join\nIn censure of his seeming.";
	
	private static boolean USE_CROSS_PLATFORM_UI = true;
	
	public TextAreaDemo() {
		super(new BorderLayout());
		
		JTextArea textArea = new JTextArea(TEXT_VALUE);
		
		JScrollPane scrollPane = new JScrollPane(textArea);
		
		scrollPane.setPreferredSize(new Dimension(200,300));
		this.add(scrollPane);
		
		this.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
	}
	
	
	private static void run() {
		if(USE_CROSS_PLATFORM_UI) {
			try {
				UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		JFrame frame = new JFrame("Button Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JComponent contentPane = new TextAreaDemo();
		contentPane.setOpaque(true);
		
		frame.setContentPane(contentPane);
		frame.pack();
		
		frame.setVisible(true);
	}
	
	public static void main(String[] args) {
		run();
	}

}
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
Here is my whole method...

I just for the life of me, cannot get that scroll bar to come up...

Do you think it is because of not having enough text? I have it set to ALWAYS come up..

Code:
    public void build(String txt) {
        //Creating JFrame for the Edit window
        JFrame frameEd = new JFrame("Text Field/Edit");
        //Need the MenuBar for the Save choice
        JMenuBar menuBar = new JMenuBar();
        frameEd.setDefaultLookAndFeelDecorated(true);
        //A container to hold all the content
        Container content = frameEd.getContentPane();
        frameEd.setBackground(Color.GRAY);
        frameEd.setSize(800,300); 
        frameEd.setLocation(500, 500);
        //Setting the JPanel for the JframeEdArea and the ScrollBar
        JPanel jp = new JPanel();
        //jp.setLayout(new FlowLayout());
        //this.field is the JframeEdField
        this.field = new JTextArea(txt);
        this.field.setRows(70);
        this.field.setColumns(62);
        //Menu Bar Option
        JMenu file = new JMenu("File");
        
        //Does this not set the ScrollBar?
        JScrollPane jsp = new JScrollPane(this.field); 
        //The line below this one, I thought this was suppose to set it, but doesnt.
        jsp.getViewport().add(this.field);
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jsp.setPreferredSize(new Dimension(450, 110));
        JMenuItem save = new JMenuItem("Save");
        file.add(save);
        save.setActionCommand("save");
        save.addActionListener(this);
        menuBar.add(file);
        frameEd.setJMenuBar(menuBar);
        jp.add(jsp, BorderLayout.CENTER);
        content.add(jsp, BorderLayout.CENTER);
        content.add(jp);
        frameEd.setVisible(true);
    }
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
jtalerico said:
Here is my whole method...

I just for the life of me, cannot get that scroll bar to come up...

Do you think it is because of not having enough text? I have it set to ALWAYS come up..

It looks like you don't understand what you are doing with the content pane and the JPanel.

I suspect the JScrollPane *is* being displayed, but due to all the random things your code is doing the TextArea itself is also being displayed directly and just happens to be displayed on top of the JScrollPane.
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
Nope, i am not a very big GUI person! I am trying to figure things out though!

If you look at that pic you will see some thing that "looks" like a scrool bar, but it does not scroll!!
 

Le Je-Pe

macrumors newbie
Jan 10, 2006
3
0
Montreal, Qc.
I think your problem is here...

jtalerico said:
Code:
        jp.add(jsp, BorderLayout.CENTER);
        content.add(jsp, BorderLayout.CENTER);
        content.add(jp);

You usually can't attach an gui object to different containers.
Try deleting the

content.add(jsp, BorderLayout.CENTER);

line. Only the JPanel (which contains your JScrollPane) should be attached to the content pane.
Haven't tried it out... but sounds right..

Give it a try
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
I agree with mrichmon, you need to put some time into developing good habits regarding your coding style (spacing, indentation, comments etc.). You already have a few comments, that's good, but your code needs to be readable and having everything all mushed together into a long same-looking list is going to cause you problems in the long run, trust me. I have trouble just remembering what I was doing in my own code last week. Look at the example that mrichmon wrote and you will see that it is much easier to figure out what's going on and follow the logic, because of the style. Another thing I notice about your code is that you aren't always grouping operations that logically go together. For example, you set up some stuff with the JPanel, then you add and configure some menus in-between, then go back to working on the JPanel. I would try to group things more logically, as much as possible; for example do all the JPanel stuff in one place and do all the menu stuff in another, and separate the sections with a blank line. If you find yourself with really huge methods, or find yourself typing the same bits of code twice, think how you can pull out chunks of that code and replace it with method calls (move that code into a separate method and then call the method from where the code used to be). You might have a constructor that reads like this:
Code:
setUpFrame();
setUpMenus();
setUpFonts();
loadFile();
or whatever.
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
Thanks for the tips guys :)

I have been doing Java for about 2 years now. I have never been to keen on the GUI stuff. I know my code is pretty hidious! I usually comment really well, but I am not turning this program in for any sort of credit so it really dosn't bother me that it isn't too heavly commented.

I will try the suggestions and see what i can get! Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.