Results 1 to 8 of 8
  1. #1
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default text area size problem

    Hello, I'm making a gui that has a JTextArea inside a JScrollPane inside a JPanel. When I didn't have the JPanel it would always make the JTextArea the size of the window, but when I have them inside the JPanel, the JTextArea will stay the preferred size and the JScrollPane wouldn't work. I want to make it so I can have the JPanel and make it so that the JScrollPane is the same size as the window, and if there is too much text, the scrollbars will appear. Please help, thanks.

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

    Default

    I think that your problem is with layout managers. Are you using any layout managers here, and if so which ones? JPanel by default uses FlowLayout which might not be the best fit for your purposes. Perhaps a Borderlayout is what you want, and then you can add the JScrollPane to the JPanel in the BorderLayout.CENTER position.

    If this doesn't help, then your best bet is to post a small compilable program that demonstrates your problem and lets us play with your code, an SSCCE.

    Much luck!

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

    Default

    Oh, and before I forget, welcome to the forum!!

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    So here is an example,
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    
    public class Resize extends JFrame 
    {
    JPanel pane = new JPanel(false);
    JScrollPane scrollPane;
    JTextArea text = new JTextArea(50, 80);
    JTabbedPane tabbedPane = new JTabbedPane();
    
    public Resize()
    {
    scrollPane = new JScrollPane(text);
    pane.add(scrollPane, BorderLayout.CENTER);
    tabbedPane.addTab("Resize", null, pane, "");
    add(tabbedPane);
    }
    
    
    public static void main( String args[] )
          {
             final Resize window = new Resize();
             window.setSize( 1680, 1050 );
             window.setVisible( true );
          }
    
    }
    In this example, you will see that the text area is fixed at 50, 80. However, if you change "add(tabbedPane);" to "add(scrollPane);", you will see that the text area is the size of the window, and if you make the window smaller than 50, 80 then you will see scrollbars. This is what I want the first one to look like. Thanks very much for the help so far.

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

    Default

    If you set the JPanel's layout to BorderLayout, does it do what you want? For e.g.,
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Resize extends JFrame {
       JPanel pane = new JPanel(false);
       JScrollPane scrollPane;
       JTextArea text = new JTextArea(50, 80);
       JTabbedPane tabbedPane = new JTabbedPane();
    
       public Resize() {
          pane.setLayout(new BorderLayout()); // set the JPanel's layout to BorderLayout
          
          scrollPane = new JScrollPane(text);
          pane.add(scrollPane, BorderLayout.CENTER);
          tabbedPane.addTab("Resize", null, pane, "");
          add(tabbedPane);
       }
    
       public static void main(String args[]) {
          final Resize window = new Resize();
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          window.setSize(1680, 1050);
          window.setVisible(true);
       }
    }

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Well, I'm not sure you can do what you want.

    Scrollbars appear automatically when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane.

    When you add the scrollpane directly to the frame, then the size of the scrollpane gets changed to reflect the space available. Therefore scrollbars appear as required.

    When you add the scrollpane to the tabbed pane, the size of the scrollpane doesn't change, only the size of the tabbed pane changes, so no scrollbars appear.

    The only solution I can think of is:

    Java Code:
    // add(tabbedPane);
    add(new JScrollPane(tabbedPane));
    Now the tabbed pane is scrollable so you can see the entire text area and the text area will become scrollable if you add more than 50 lines of test to it.

  7. #7
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Thank you very much Fubarable!! I've been trying to find a solution to my problem for about a week and it was so simple. Thanks, this is exactly what I wanted.

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

Similar Threads

  1. eol in text area....
    By Nicholas Jordan in forum Networking
    Replies: 0
    Last Post: 09-14-2008, 10:59 PM
  2. [SOLVED] Text Area new line problem
    By Jay-1.1 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 04:18 AM
  3. Text Area problem
    By mcal in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 09:42 PM
  4. Displaying data into text area
    By abhiN in forum New To Java
    Replies: 1
    Last Post: 01-22-2008, 10:30 AM
  5. textfiled and text area to UTF-16?
    By Mr tuition in forum AWT / Swing
    Replies: 0
    Last Post: 12-04-2007, 12:40 PM

Tags for this Thread

Posting Permissions

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