Results 1 to 9 of 9
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    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? :/

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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!

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    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

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    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.

    Java 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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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:
    Java 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.

  6. #6
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    It works! ^_^

    Thank you very much. And, I will (reluctantly) TRY to break my null layout habits. :P

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'll show you one example with a mixture of layouts:
    Java 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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    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:
    Java 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?

  9. #9
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    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. :)

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •