By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,129 Members | 2,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,129 IT Pros & Developers. It's quick & easy.

JTextArea not displaying correct size

P: 67
Ok my problem is that when it loads the JTextArea, it does not show the full component's width and height. I've tried specifying it with setrow, setcolumn, setprefferedsize, all of that. It does display if I start to highlight the text but not on the initial load.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.Color;
  2. import java.awt.GridBagLayout;
  3. import javax.swing.ImageIcon;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JPanel;
  7. import javax.swing.JTextArea;
  8.  
  9. /**
  10.  * @author Hanshawc
  11.  */
  12.  
  13. public class MyDesk extends JFrame
  14. {
  15.    private JPanel panel;
  16.    private JTextArea questionBox;
  17.    private JPanel questionPanel;
  18.  
  19.    public static void main(String[] args)
  20.    {
  21.        MyDesk myDesk = new MyDesk();
  22.    }
  23.  
  24.    public MyDesk()
  25.    {
  26.        this.setTitle("MyDesk Alpha Version: 0.1");
  27.        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  28.        this.setSize(660,525);
  29.        this.setResizable(false);
  30.        this.setBackground(Color.WHITE);
  31.        this.add(getMainPanel());
  32.        this.add(getUI());
  33.        this.setVisible(true);
  34.    }
  35.  
  36.    private JPanel getUI()
  37.    {
  38.       JPanel userInterface = (JPanel) this.getGlassPane();
  39.       userInterface.setVisible(true);
  40.       userInterface.setLayout(new GridBagLayout());
  41.       userInterface.add(getQuestionPanel());
  42.       return userInterface;
  43.    }
  44.  
  45.    private JPanel getMainPanel()
  46.    {
  47.        panel = new JPanel();
  48.        panel.setSize(640,480);
  49.        panel.setBackground(Color.WHITE);
  50.        panel.add(getBackgroundImage());
  51.        return panel;
  52.    }
  53.  
  54.    private JLabel getBackgroundImage()
  55.    {
  56.        ImageIcon titleIcon = new
  57.        ImageIcon(getClass().getResource("background.jpg"));
  58.        JLabel titleLabel = new JLabel();
  59.        titleLabel.setIcon(titleIcon);
  60.        return titleLabel;
  61.    }
  62.  
  63.    private JPanel getQuestionPanel()
  64.    {
  65.        questionPanel = new JPanel();
  66.        questionPanel.add(getQuestionBox());
  67.        questionPanel.setBackground(Color.WHITE);
  68.  
  69.        return questionPanel;
  70.    }
  71.  
  72.    private JTextArea getQuestionBox()
  73.    {
  74.         questionBox = new JTextArea(getQuestion());
  75.         questionBox.setBackground(Color.WHITE);
  76.         questionBox.setLineWrap(true);
  77.         questionBox.setWrapStyleWord(true);
  78.         return questionBox;
  79.    }
  80.  
  81.    private String getQuestion()
  82.    {
  83.        String question = "Test question";
  84.        return question;
  85.    }
  86. }
Mar 10 '09 #1
Share this Question
Share on Google+
1 Reply


P: 67
Duh, well sorta I had to add the ui before the mainpanel.
Mar 10 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.