How to set JTextArea size?

I want to set a fixed size for JtextAreainsideJOptionPane

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 great, but the size is not fixed. What is the problem with scroll.setSize (300,600) ;?

+4
source share
1 answer

Since each system can render fonts differently, you should avoid using pixel measurements if possible.

Instead, specify the rows and columns that you want to display.

JTextArea ta = new JTextArea(5, 20);
+9
source

Source: https://habr.com/ru/post/1530791/


All Articles