Problem with JScrollPane on JTextArea
I am trying to develop a swing app. This app contains JLabel-JTextField/JTextArea pairs. However when I run the code, the JTextArea does not contains the vertical scroll bars. I am enclosing the method that creates the swing components. Am I missing something in the code for adding JScrollPane?
private void createErrorPanel() {
errorPanel = new JPanel();
errorPanel.setLayout( null );
errorPanel.setBounds( 0, 0, 750, 200 );
Font font = new Font("Courier", Font.BOLD, 12);
// First Row
JLabel l11 = new JLabel("Error Code:",JLabel.RIGHT);
l11.setFont( font );
l11.setBounds( 9, 27, 116, 24 );
errorPanel.add( l11 );
errorCdTxt = new JTextField("Type Error Code ...");
errorCdTxt.setBounds( 128, 27, 125, 21 );
errorCdTxt.addMouseListener( new MouseHandler() );
errorPanel.add( errorCdTxt );
JLabel l21 = new JLabel("Error Desc:", JLabel.RIGHT);
l21.setFont( font );
l21.setBounds( 0, 45, 125, 24 );
errorPanel.add( l21 );
errorDescTxtArea = new JTextArea( );
errorDescTxtArea.setBounds( 129, 51, 350, 75 );
errorDescTxtArea.setLineWrap( true );
errorDescTxtArea.setWrapStyleWord( true );
JScrollPane sdPane = new JScrollPane( errorDescTxtArea );
errorPanel.add( errorDescTxtArea );
JLabel l31 = new JLabel( "Error Resn :", JLabel.RIGHT );
l31.setFont( font );
l31.setBounds( 0, 125, 125, 24 );
errorPanel.add( l31 );
errorResnTxtArea = new JTextArea( );
errorResnTxtArea.setBounds( 129, 130, 350, 80 );
errorResnTxtArea.setLineWrap( true );
errorResnTxtArea.setWrapStyleWord( true );
JScrollPane srPane = new JScrollPane( errorResnTxtArea );
errorPanel.add( errorResnTxtArea );
errCdOkBtn = new JButton( "OK" );
errCdOkBtn.setFont( font );
errCdOkBtn.setBounds( 150, 250, 90, 24 );
errCdOkBtn.addActionListener( new ButtonHandler() );
errorPanel.add( errCdOkBtn );
errCdResBtn = new JButton( "Reset" );
errCdResBtn.setFont( font );
errCdResBtn.setBounds( 250, 250, 90, 24 );
errCdResBtn.addActionListener( new ButtonHandler() );
errorPanel.add( errCdResBtn );
menuFrame.getContentPane().add( errorPanel );
menuFrame.setSize(790, 750);
menuFrame.setVisible(true);
}
[2408 byte] By [
scshekhara] at [2007-11-27 11:33:35]

# 1
You are adding your text areas to a scroll pane but not adding the scroll pane to the panel.
i.e. you are doing this:
errorDescTxtArea = new JTextArea( );
errorDescTxtArea.setBounds( 129, 51, 350, 75 );
errorDescTxtArea.setLineWrap( true );
errorDescTxtArea.setWrapStyleWord( true );
JScrollPane sdPane = new JScrollPane( errorDescTxtArea );
errorPanel.add( errorDescTxtArea );
and you should be doing this:
errorDescTxtArea = new JTextArea( );
errorDescTxtArea.setBounds( 129, 51, 350, 75 );
errorDescTxtArea.setLineWrap( true );
errorDescTxtArea.setWrapStyleWord( true );
JScrollPane sdPane = new JScrollPane( errorDescTxtArea );
errorPanel.add(sdPane);
# 5
I tried again using a layout managers and I was able to get the scrollpane on the JTextArea. Now however I am not to bring the 2 buttons ( Ok and Reset ) together in the middle of the row. Any help is highly appreciated. Code of the method that generates the gui is given below. I have highlighted the area where I need the help.
private void createErrorPanel() {
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
errorPanel = new JPanel();
errorPanel.setLayout( gbl );
JLabel topDummyLbl = new JLabel( " " );
JLabel sepDummyLbl = new JLabel( " " );
JLabel l11 = new JLabel(" Error Code:",JLabel.RIGHT);
errorCdTxt = new JTextField("Type Error Code ...");
JLabel l21 = new JLabel("Error Description: ", JLabel.RIGHT);
errorDescTxtArea = new JTextArea(3,50 );
JLabel l31 = new JLabel( "Error Resolution : ", JLabel.RIGHT );
errorResnTxtArea = new JTextArea( 3,50 );
errCdOkBtn = new JButton( "Ok" );
errCdResBtn = new JButton( "Reset" );
GridBagConstraints topDummyLblGBC = new GridBagConstraints();
topDummyLblGBC.gridwidth = GridBagConstraints.REMAINDER;
topDummyLblGBC.gridheight = 1;
errorPanel.add( topDummyLbl,topDummyLblGBC );
l11.setLabelFor(errorCdTxt);
errorPanel.add( l11 );
GridBagConstraints errCdTxtGBC = new GridBagConstraints();
errCdTxtGBC.gridwidth = GridBagConstraints.REMAINDER;
errCdTxtGBC.anchor = GridBagConstraints.WEST;
errorPanel.add (errorCdTxt, errCdTxtGBC);
l21.setLabelFor( errorDescTxtArea );
errorPanel.add( l21 );
GridBagConstraints errDescTxtAreaGBC = new GridBagConstraints();
errDescTxtAreaGBC.gridwidth = GridBagConstraints.REMAINDER;
errDescTxtAreaGBC.fill = GridBagConstraints.BOTH;
errDescTxtAreaGBC.insets = new Insets (1, 1, 1, 1);
JScrollPane sdPane = new JScrollPane( errorDescTxtArea );
errorPanel.add (sdPane, errDescTxtAreaGBC);
l31.setLabelFor( errorResnTxtArea );
errorPanel.add( l31 );
GridBagConstraints errResTxtAreaGBC = new GridBagConstraints();
errResTxtAreaGBC.gridwidth = GridBagConstraints.REMAINDER;
errResTxtAreaGBC.fill = GridBagConstraints.BOTH;
errResTxtAreaGBC.insets = new Insets (1, 1, 1, 1);
JScrollPane srPane = new JScrollPane( errorResnTxtArea );
errorPanel.add (srPane, errResTxtAreaGBC);
// NEED HELP FOR THIS ROW OF COMPONENTS
GridBagConstraints sepDummyLblGBC = new GridBagConstraints();
sepDummyLblGBC.gridwidth = GridBagConstraints.REMAINDER;
sepDummyLblGBC.gridheight = 4;
errorPanel.add( sepDummyLbl,sepDummyLblGBC );
JLabel okBtnLbl = new JLabel( " " );
GridBagConstraints okBtnLblGBC = new GridBagConstraints();
okBtnLblGBC.gridwidth = GridBagConstraints.RELATIVE;
errorPanel.add( okBtnLbl, okBtnLblGBC );
GridBagConstraints okBtnGBC = new GridBagConstraints();
okBtnGBC.gridwidth = GridBagConstraints.RELATIVE;
okBtnGBC.insets = new Insets (1, 1, 1, 0);
errorPanel.add( errCdOkBtn, okBtnGBC );
GridBagConstraints resBtnGBC = new GridBagConstraints();
resBtnGBC.gridwidth = GridBagConstraints.RELATIVE;
resBtnGBC.insets = new Insets (1, 1, 1, 1);
errorPanel.add( errCdResBtn, resBtnGBC );
JLabel resBtnLbl = new JLabel( " " );
GridBagConstraints resBtnLblGBC = new GridBagConstraints();
resBtnLblGBC.gridwidth = GridBagConstraints.REMAINDER;
errorPanel.add( resBtnLbl, resBtnLblGBC );
// END FOR NEED HELP FOR THIS ROW OF COMPONENTS
GridBagConstraints dummyLabelGBC = new GridBagConstraints();
dummyLabelGBC.gridwidth = GridBagConstraints.REMAINDER;
dummyLabelGBC.weighty = 0.5;
errorPanel.add (new JLabel(""), dummyLabelGBC);
menuFrame.getContentPane().add( errorPanel );
menuFrame.setSize(790, 550);
menuFrame.setVisible(true);
}