7

I want to set a fixed size for JtextArea within JOptionPane

public static void main(String[] args) {

        JTextArea headersTxt = new JTextArea();
        for (int i = 0 ; i < 50 ; i ++ ) {
            headersTxt.append("test \n") ;
        }
        JScrollPane scroll = new JScrollPane(headersTxt); 
        scroll.setSize (300,600) ;  // this line silently ignored
        int test = JOptionPane.showConfirmDialog(null,  scroll,"test",  JOptionPane.OK_CANCEL_OPTION) ;

    }

However, the above code ignores scroll.setSize (300,600) ;

It works fine but the size is not fixed . What is the problem with scroll.setSize (300,600) ; ?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Borat Sagddiev
  • 807
  • 5
  • 14
  • 28

1 Answers1

12

Because each system can render fonts differently, you should avoid using pixel measurements where possible

Instead, provide the rows and columns you want to display

JTextArea ta = new JTextArea(5, 20);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you . That works perfect ! I will avoid pixels where possible – Borat Sagddiev Mar 09 '14 at 02:16
  • Adjusting the **font size** of of a text area with columns and rows specified will also change the size of the text area. See also [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) That Q&A does not explicitly mention avoiding `setSize(..)`, probably because the size of a component is usually *ignored* by the layout manager, which will more commonly set a size based upon the preferred, maximum and minimum size. – Andrew Thompson Mar 09 '14 at 04:48