Friday, October 3, 2014

setSize vs setPreferredSize in JTextArea, JTextField, JButton, JLabel, JPanel and JFrame

For JFrame:
          Always use setSize for setPreferredSize does nothing.

For JPanel, JButton and JLabel:
          No need to set their sizes for their sizes are always expanding to the space available. You need to use some invisible components or fillers to occupy the space that you don't want these components to be.

For JTextField:
         The setColumns method designates the size of the JTextField.
         The setPreferredSize makes the JTextField disappear, while the setSize does nothing.

For JTextArea:
          The setRows and setColumns methods designate the size of the JTextArea.

         The setPreferredSize fixes the displaying area. If the setLineWrap(true) method is not called, the input characters will keep adding to the end of the line, however the width of the JTextArea will not expand and it only displays the number of characters its width can hold. If the setLineWrap(true) method is called, the input characters will be displayed in a new line when the width of the displaying area is reached. However, when the height of the displaying area is reached, the height of the displaying area will not expand even though the input characters are keeping adding to the end of the text.

        When setSize is used instead of setPreferredSize, the behavior of the JTextArea is the same as it is not used, if the setLineWrap(true) method is not called, the input characters will keep adding to the end of the line and the width of the JTextArea expands accordingly. If the setLineWrap(true) method is called, the input characters will be displayed in a new line when the width of the displaying area is reached and when the height of the displaying area is reached, the height of the displaying area will expand accordingly.

          If you really want to use the setPreferredSize or the setSize to set the size of the JTextArea instead of using setRows and setColumns, do not call the setRows and setColumns methods and call the setLineWrap(true) before you call the setPreferredSize or the setSize method.

           The setPreferredSize mthod will set the size of the JTextArea to the size specified in the method. The size is fixed and will not expand according to the size of the text you put in it.

           The setSize method will set the width to the specified amount, but will not set the height. The height dynamically expands according to the the text put in the text area.

When the JTextArea is put in a JScrollPane:
          No matter whether the setRows and setColumns or the setPreferredSize or the setSize is used, the size of the JTextArea will occupy the whole available space in the JScrollPane. When setSize is used, it will automatically scroll, where else when setPreferredSize is used the JScrollPane will not scroll.

           However, when a JTextArea is put in a JScrollPane, it is better to not use either setPreferredSize or setSize on the JTextArea and the JScrollPane will scroll when it is needed.

---------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment