Results 1 to 5 of 5
  1. #1
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default JScrollPane doesn't display

    I wrote a program to create every permutation of a string to help solve a daily anagram puzzle, and am trying to create a GUI. Since the string is either 5 or 6 characters long, I will have either 120 or 720 words. Found a spell checker to find the misspelled words.

    I want to use a JTextArea component to display the permutations, and will need to put it inside a scroll pane. GridBox isn't satisfactory because cells will all be the same size. GridGrabBag seems to have a mind of its own since my text area was placed to the right of my labels and text field, although the grid values were correct. I am trying to use absolute positioning, and have done that before in my limited swing experience.

    My problem is that the text area does not display when I try to put it in a scroll pane. Am not including import statements.

    Java Code:
    public class JumbleAbsolute extends JFrame {
    	   
    	JumbleAbsolute(){
    //	    Container pane = getContentPane();
    	    setLayout(null);
    
            JLabel  lblInstruction = new JLabel("Input the Anagram");
            JTextField txfAnagram = new JTextField(6);
            JTextArea txArea = new JTextArea(5,30);
    
            add(lblInstruction);
            add(txfAnagram);
    
            //This works
            add(txArea);
    
            //This doesn't
            //add(new JScrollPane(txArea));
    
            Insets insets = getInsets();
            Dimension size = lblInstruction.getPreferredSize();
            lblInstruction.setBounds(25 + insets.left, 5 + insets.top,
                         size.width, size.height);
            size = txfAnagram.getPreferredSize();
            txfAnagram.setBounds(25 + insets.left, 40 + insets.top,
                         size.width, size.height);
            size = txArea.getPreferredSize();
            txArea.setBounds(25 + insets.left, 80 + insets.top,
                         size.width , size.height);
           } // constructor
    }// class JumbleAbsolute
            
    
    public class JumbleApp extends JFrame {
    	public static void main(String[] args) {
    		JumbleAbsolute app = new JumbleAbsolute();
    		app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		app.setSize( 200, 500 ); // set frame size
    		app.setVisible(true);
    	}
    }

  2. #2
    Taiko is offline Member
    Join Date
    Jan 2010
    Location
    Opava, Czech Republic
    Posts
    14
    Rep Power
    0

    Default

    You are trying to add the txArea to the content pane of your frame two times. Try to delete this:
    Java Code:
    //This works
    add(txArea);
    Just:
    Java Code:
    add(new JScrollPane(txArea));
    should work.

    But, it might be, that you won't see what you want. So, try something like this:

    Java Code:
    JTextArea txArea = new JTextArea();
    txArea.setPreferredSize(new Dimension(200, 200));
    JScrollPane s = new JScrollPane(txArea);
    s.setPreferredSize(new Dimension(100, 100));
    add(s);
    Size (preferred, maximum) of JScrollPane must be smaller than size of its ViewPort, if you want scrollbars to show. Or, you can set the ScrollBarPolicy (setVerticalScrollBarPolicy(int policy), setHorizontalScrollBarPolicy(int policy)).
    Last edited by Taiko; 01-18-2010 at 08:23 AM.

  3. #3
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default suggestion didn't work

    I think the code was misinterpreted. One add statement was commented out; the intention was to indicate that one line of code added and displayed the text area. The line that was commented didn't.

    I added the suggested changes. Same problem.

    Do I need to create and add a panel somewhere? My model code from Dietel "java for programmers" doesn't indicate that I would, but they use layout managers.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class JA extends JFrame {
        JA() {
            setLayout(null);
    
            JLabel  lblInstruction = new JLabel("Input the Anagram");
            JTextField txfAnagram = new JTextField(6);
            JTextArea txArea = new JTextArea(5,30);
    
            add(lblInstruction);
            add(txfAnagram);
    
            //This works
            //add(txArea);
    
            //This doesn't
            //add(new JScrollPane(txArea));
    
            JScrollPane scrollPane = new JScrollPane(txArea);
            add(scrollPane);
    
            Insets insets = getInsets();
            System.out.printf("insets: [%d, %d, %d, %d]%n",
                insets.top, insets.left, insets.bottom, insets.right);
            Dimension size = lblInstruction.getPreferredSize();
            lblInstruction.setBounds(25 + insets.left, 5 + insets.top,
                                     size.width, size.height);
            size = txfAnagram.getPreferredSize();
            txfAnagram.setBounds(25 + insets.left, 40 + insets.top,
                                 size.width, size.height);
            size = txArea.getPreferredSize();
    //        txArea.setBounds(25 + insets.left, 80 + insets.top,
    //                         size.width , size.height);
            scrollPane.setBounds(25 + insets.left, 80 + insets.top,
                                 size.width , size.height);
        }
    
        public static void main(String[] args) {
            JA app = new JA();
            app.setDefaultCloseOperation( EXIT_ON_CLOSE );
            app.setSize( 400, 300 ); // set frame size
            app.setVisible(true);
            Insets insets = app.getInsets();
            System.out.printf("insets: [%d, %d, %d, %d]%n",
                insets.top, insets.left, insets.bottom, insets.right);
        }
    }

  5. #5
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default thank you

    Thank you. You have gained much merit, and are obviously a bodhisattva, the enlightened buddhist saint leading others to enlightenment.

Similar Threads

  1. jscrollpane
    By kaemonsaionji in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 08:39 AM
  2. problem with Jscrollpane
    By ravrajesh.ap in forum AWT / Swing
    Replies: 5
    Last Post: 01-03-2009, 10:38 PM
  3. Replies: 8
    Last Post: 12-29-2008, 07:12 AM
  4. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 07:58 PM
  5. JScrollPane not scrolling
    By Riftwalker in forum Advanced Java
    Replies: 2
    Last Post: 07-17-2007, 08:16 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
  •