[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 508
  • Last Modified:

Java GUI JScrollPane/JTextArea issue

i'm trying to make a simple GUI that contains the following elements on top of eachother:

Text (JLabel)
Drop down menu (JComboBox)
"Ok" button (JButton... haven't done this part yet)
More Text (JLabel)
Scrollable text area (JTextArea + JScrollPane?)

the text area doesn't seem to be showing up and i don't know what the problem is.  check my attached jpg picture to see what it does.  code is included below!  i copied some of it from a demo so i don't fully understand all of it.

thanks.
public StressGUILI()
    	{
    		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
           
	        String[] numStrings = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
 
	        
            JLabel numLabel1 = new JLabel("Enter how stressed you feel");
            JLabel numLabel2 = new JLabel("whenever you feel a change:");
	        
	        JComboBox numList = new JComboBox(numStrings);
	        //numList.setSelectedIndex(0);
	        numList.addActionListener(this);
 
            //Create the UI for displaying result.
            JLabel resultLabel = new JLabel("Past Entries:");
            
            JTextArea textArea = new JTextArea(8, 30);
	        textArea.setEditable(false);
	        JScrollPane scrollPane = new JScrollPane(textArea);
 
 
            //Lay out everything.
            JPanel numPanel = new JPanel();
            numPanel.setLayout(new BoxLayout(numPanel,
                                   BoxLayout.PAGE_AXIS));
            numPanel.add(numLabel1);
            numPanel.add(numLabel2);
            numList.setAlignmentX(Component.LEFT_ALIGNMENT);
            numPanel.add(numList);
 
            JPanel resultPanel = new JPanel(new GridLayout(0, 1));
            resultPanel.add(resultLabel);
	    resultPanel.add(scrollPane);
 
            numPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
            resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
 
            add(numPanel);
            add(Box.createRigidArea(new Dimension(0, 10)));
            add(resultPanel);
 
            setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
}

Open in new window

guiexample.JPG
0
vee417
Asked:
vee417
  • 9
  • 4
1 Solution
 
CEHJCommented:
Make sure you set the GUI large enough to see all the components
0
 
CEHJCommented:
Also what is the superclass?
0
 
CEHJCommented:
If it's a JFrame or JDialog, try the following - works well for me
    private void setGui() {
	try {
 
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    Container c = getContentPane();
	    c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
 
	    String[] numStrings = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
 
 
	    JLabel numLabel1 = new JLabel("Enter how stressed you feel");
	    JLabel numLabel2 = new JLabel("whenever you feel a change:");
 
	    JComboBox numList = new JComboBox(numStrings);
	    //numList.setSelectedIndex(0);
	    //numList.addActionListener(this);
 
	    //Create the UI for displaying result.
	    JLabel resultLabel = new JLabel("Past Entries:");
 
	    JTextArea textArea = new JTextArea(8, 30);
	    textArea.setEditable(false);
	    JScrollPane scrollPane = new JScrollPane(textArea);
 
 
	    //Lay out everything.
	    JPanel numPanel = new JPanel();
	    numPanel.setLayout(new BoxLayout(numPanel,
			BoxLayout.PAGE_AXIS));
	    numPanel.add(numLabel1);
	    numPanel.add(numLabel2);
	    numList.setAlignmentX(Component.LEFT_ALIGNMENT);
	    numPanel.add(numList);
 
	    JPanel resultPanel = new JPanel(new GridLayout(0, 1));
	    resultPanel.add(resultLabel);
	    resultPanel.add(scrollPane);
 
	    numPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
	    resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
 
	    c.add(numPanel);
	    c.add(Box.createRigidArea(new Dimension(0, 10)));
	    c.add(resultPanel);
 
	    ((JPanel)c).setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
vee417Author Commented:
i'm getting the error: "The method getContentPane() is undefined for the type StressGUILI" on the "Container c = getContentPane();" line.  if it helps, here is my other method that is run from another class:
public static void createAndShowGUI() {
	        //Create and set up the window.
	        JFrame frame = new JFrame("Listening Mode Stress Entry");
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        
	        
	        JComponent newContentPane = new StressGUILI();
	        newContentPane.setOpaque(true); //content panes must be opaque
	        frame.setContentPane(newContentPane);
	
	        
	        //Display the window.
	        frame.pack();
	        frame.setVisible(true);
	        
	    }

Open in new window

0
 
CEHJCommented:
I would make StressGUILI a subclass of JPanel
0
 
CEHJCommented:
Doing that and sizing the window gives me the following:
0
 
CEHJCommented:
Here:
guili.png
0
 
vee417Author Commented:
ahh, well i got it to work that much by changing around some numbers, but why are there big spaces between everything?
0
 
CEHJCommented:
That's due to the layouts that you're using
0
 
vee417Author Commented:
do you know how to fix it?
0
 
CEHJCommented:
For one thing - don't use GridLayout
0
 
vee417Author Commented:
ok, i figured it out.  thanks.
0
 
CEHJCommented:
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now