Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-29-2009, 03:37 PM
Member
 
Join Date: Jan 2009
Posts: 68
Rep Power: 0
AndrewM16921 is on a distinguished road
Default JTextArea with JScrollPane
Hey, I'm trying to add a vertical scrollbar to my JTexArea. The JTexArea displays and works properly without the JScrollPane, however when I add the JScrollPane it simply does not show up. I think I'm missing something, but I could figure out what. Any thoughts? :/

Code:
final JTextArea plainTextArea = new JTextArea();
plainTextArea.setBounds(10, 30, 190, 100);
plainTextArea.setEditable(true);
plainTextArea.setLineWrap(true);
frame.add(plainTextArea);
JScrollPane plainScroll = new JScrollPane(plainTextArea);
plainScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(plainScroll);
	    
final JTextArea cipherTextBox = new JTextArea();
cipherTextBox.setBounds(210, 30, 190, 100);
cipherTextBox.setEditable(true);
cipherTextBox.setLineWrap(true);
frame.add(cipherTextBox);
JScrollPane cipherScroll = new JScrollPane(cipherTextBox);	
cipherScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(cipherScroll);
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-29-2009, 03:59 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,491
Rep Power: 8
Fubarable is on a distinguished road
Default
What layouts are you using? If null layout -- simply don't do this. Please post a small compilable program that demonstrates your problem and we'll be able to give you better advice, an SSCCE. Much luck!
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-29-2009, 04:19 PM
Senior Member
 
Join Date: Aug 2009
Location: Pittsburgh, PA
Posts: 265
Rep Power: 1
zweibieren is on a distinguished road
Default
The code adds both plainTextArea and plainScroll to the frame.
It should only add the plainScroll; the latter allocates screen space for
te plainTextArea within the plainScroll screen area.

The code calls setBounds() outside a layout manager.
That this is wrong may be true or may be my own superstition.
It definitely would not be wrong to call setPreferredSize() instead.

As it is, the bounds are set on the JTextArea, but it is actually
the JScrollPanes that have to be laid out in the frame.

You may find helpful the discussion of Using Layout Managers
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-29-2009, 05:12 PM
Member
 
Join Date: Jan 2009
Posts: 68
Rep Power: 0
AndrewM16921 is on a distinguished road
Default
I am using null for layout, because I just really like being able to define exactly where things go. But if there's a better way to do this, I'll look into it. Going to read that link ^ in just a moment. Edit: I might switch over to like... GridBagLayout or something, for this program. Not sure yet.

As for the code, I'll post a version with some code fragments in comments so I don't have to post any other classes. I cut out the JMenu too, cause that's not important and rather chunky.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.PrintWriter;
import java.util.Scanner;

@SuppressWarnings("serial")
public class CryptFrame extends JFrame
{
	public static void main(String[] args)
	{
		//JFrame
		JFrame frame = new JFrame("Crypt");
		frame.setLayout(null);
		frame.setResizable(false);
		int windowWidth = 426;
		int windowHeight = 250; 
	    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	    frame.setBounds((int)(screenSize.getWidth()/2)-(windowWidth/2),
	    				(int)(screenSize.getHeight()/2)-(windowHeight/2),
	    				windowWidth, windowHeight);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Scanner load;
		//PrintWriter save;
		
		//JTextField
		final JTextArea plainTextArea = new JTextArea();
		plainTextArea.setBounds(10, 30, 190, 100);
		plainTextArea.setEditable(true);
		plainTextArea.setLineWrap(true);
		JScrollPane plainScroll = new JScrollPane(plainTextArea);
		plainScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		frame.add(plainScroll);
	    
	    final JTextArea cipherTextBox = new JTextArea();
	    cipherTextBox.setBounds(210, 30, 190, 100);
	    cipherTextBox.setEditable(true);
	    cipherTextBox.setLineWrap(true);
		JScrollPane cipherScroll = new JScrollPane(cipherTextBox);
		cipherScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		cipherScroll.setPreferredSize(new Dimension(190, 100));
		frame.add(cipherScroll);
		
	    //JLabel
	    final JLabel header = new JLabel("Crypt v0.1"); //Note to self: Think of a better name!
	    header.setBounds(10, 2, 100, 24);
	    frame.add(header);
	    
	    //JButton
	    JButton encrypt = new JButton("Encrypt >>");
	    ActionListener actionListenerEncrypt = new ActionListener()
	    {
	    	public void actionPerformed(ActionEvent actionEvent)
	    	{
	    		/*String str = plainTextArea.getText();
	    		str = CryptUtil.unicodeSubstitution(str, new int[] {1, 2, 3});
	    		str = CryptUtil.swap(str);
	    		str = CryptUtil.expansion(str, 3);
	    		cipherTextBox.setText(str);*/
	    	}
	    };
	    encrypt.addActionListener(actionListenerEncrypt);
	    encrypt.setToolTipText("Encrypt Text");
	    encrypt.setBounds(10, 130, 190, 24);
	    frame.add(encrypt);
	    
	    JButton decipher = new JButton("<< Decipher");
	    ActionListener actionListenerDecipher = new ActionListener()
	    {
	    	public void actionPerformed(ActionEvent actionEvent)
	    	{
	    		/*String str = cipherTextBox.getText();
	    		str = CryptUtil.reverseExpansion(str, 3);
	    		str = CryptUtil.swap(str);
	    		str = CryptUtil.unicodeSubstitution(str, new int[] {-1, -2, -3});
	    		plainTextArea.setText(str);*/
	    	}
	    };
	    decipher.addActionListener(actionListenerDecipher);
	    decipher.setToolTipText("Decipher Text");
	    decipher.setBounds(210, 130, 190, 24);
	    frame.add(decipher);
    
/* removed JMenu */
	    
		frame.setVisible(true);
	}
}

Last edited by AndrewM16921; 10-29-2009 at 05:20 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-29-2009, 05:19 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,491
Rep Power: 8
Fubarable is on a distinguished road
Default
The problem is due to your use of null layouts. When you use null, you are completely responsible for stipulating the size and location of everything you add. Right now, you are not doing this with the scrollpanes. For eg, if you add this:
Code:
    plainScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    frame.add(plainScroll);
    plainScroll.setBounds(10, 25, 190, 100); // added this!!!
your code would work.

But i agree w/ the poster above: don't use null layout.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-29-2009, 05:26 PM
Member
 
Join Date: Jan 2009
Posts: 68
Rep Power: 0
AndrewM16921 is on a distinguished road
Default
It works! ^_^

Thank you very much. And, I will (reluctantly) TRY to break my null layout habits. :P
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-29-2009, 05:31 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,491
Rep Power: 8
Fubarable is on a distinguished road
Default
I'll show you one example with a mixture of layouts:
Code:
import java.awt.*;
import javax.swing.*;

public class Crypt2 {
  private static final Dimension SCROLL_SIZE = new Dimension(190, 100);
  private JPanel mainPanel = new JPanel();
  private JTextArea area1 = new JTextArea();
  private JTextArea area2 = new JTextArea();
  private JButton encryptBtn = new JButton("Encrypt >>");
  private JButton decipherBtn = new JButton("<< Decipher");
  
  public Crypt2() {
    JScrollPane scrollPane1 = new JScrollPane(area1);
    JScrollPane scrollPane2 = new JScrollPane(area2);
    scrollPane1.setPreferredSize(SCROLL_SIZE);
    scrollPane2.setPreferredSize(SCROLL_SIZE);
    scrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    
    JPanel leftPanel = new JPanel(new BorderLayout(5, 5));
    JPanel rightPanel = new JPanel(new BorderLayout(5, 5));
    
    leftPanel.add(scrollPane1, BorderLayout.CENTER);
    leftPanel.add(encryptBtn, BorderLayout.SOUTH);
    rightPanel.add(scrollPane2, BorderLayout.CENTER);
    rightPanel.add(decipherBtn, BorderLayout.SOUTH);
    
    JPanel centerPanel = new JPanel(new GridLayout(1, 0, 10, 10));
    centerPanel.add(leftPanel);
    centerPanel.add(rightPanel);
    
    int eb = 7;
    mainPanel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
    mainPanel.setLayout(new BorderLayout(6, 6));
    mainPanel.add(centerPanel, BorderLayout.CENTER);
    mainPanel.add(new JLabel("Crypt v0.1", SwingConstants.LEFT), BorderLayout.NORTH);
  }

  public JComponent getComponent() {
    return mainPanel;
  }

  private static void createAndShowUI() {
    JFrame frame = new JFrame("Crypt");
    frame.getContentPane().add(new Crypt2().getComponent());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        createAndShowUI();
      }
    });
  }
}
Then see what happens if you resize the JFrame w/ my code vs yours.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-29-2009, 05:48 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,491
Rep Power: 8
Fubarable is on a distinguished road
Default
Also, think what you need to do with your code if you think that the text areas and their scrollpanes are too small and you want to enlarge them. You would have to change the bounds of the scrollpanes, the buttons, the whole app itself, and anything else in the app that is effected by the change.

With my code, on the other hand, all you would do is:
Code:
  //private static final Dimension SCROLL_SIZE = new Dimension(190, 100);
  private static final Dimension SCROLL_SIZE = new Dimension(400, 300);
Which do you feel is easier to maintain?
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-30-2009, 01:06 AM
Member
 
Join Date: Jan 2009
Posts: 68
Rep Power: 0
AndrewM16921 is on a distinguished road
Default
Hmm, that's very interesting. I'll definitely try using that. I setResizable(false) in my program, though. But I may change that.

Thanks.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use JTextArea with JScrollPane? Splat New To Java 5 10-19-2009 04:50 AM
jscrollpane kaemonsaionji New To Java 3 02-25-2009 09:39 AM
problem with Jscrollpane ravrajesh.ap AWT / Swing 5 01-03-2009 11:38 PM
JScrollPane with HTML Java Tip Java Tips 0 03-14-2008 12:32 PM
help with JScrollPane tommy AWT / Swing 1 08-06-2007 08:58 PM


All times are GMT +2. The time now is 07:17 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org