Search code examples
javaswingjscrollpanejtextarea

JScrollPane does not work with JTextArea


i create a little pop-up window. I have 2 input which are JTextField and JTextArea. I added JScrollPane to JTextArea. When i run to program, scrollpane is seen. But i couldnt use it. I share the image how it look like. I think i have to add height but i dont know what height

enter image description here

public class AddCommentDialog extends JDialog implements ShowableDialog, ActionListener {

    private static final long serialVersionUID = 1L;
    private static JTextField inputTitle = new JTextField();
    private static JTextArea inputComment = new JTextArea();
    private Rectangle rectangle = new Rectangle();
    private JLabel lblTitle = new JLabel("Başlık: ");
    private JLabel lblComment = new JLabel("Yorum: ");
    private JButton buttonSave = new JButton(" Kaydet ");
    private JButton buttonCancel = new JButton(" İptal ");

    // Kullanıcı tarafından girilen başlık ve yorum
    private String title, comment;


    /**
     * The constructor that sets the fields and the dialog.
     */
    public AddCommentDialog() {

        super(ImageViewerGui.getMainFrame(), "Yorum Ekle", true);

        JPanel panel = new JPanel(new BorderLayout(5, 5));
        // the input text fields
        JPanel northContainer = new JPanel(new BorderLayout(7, 7));

        JPanel labelPanel = new JPanel(new BorderLayout(5, 5));
        JPanel fieldPanel = new JPanel(new BorderLayout(5, 5));

        // the buttons
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));

        buttonSave.addActionListener(this);
        buttonCancel.addActionListener(this);
        inputTitle.setPreferredSize(new Dimension(250, 25));
        inputComment.setPreferredSize(new Dimension(250, 75));
        buttonSave.setPreferredSize(new Dimension(90, 25));
        buttonCancel.setPreferredSize(new Dimension(90, 25));

        // Satır bitince alt satıra inmesi için
        inputComment.setLineWrap(true);
        JScrollPane scrollCommentArea = new JScrollPane(inputComment);
        scrollCommentArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        try {

            labelPanel.add(lblTitle, BorderLayout.NORTH);
            labelPanel.add(lblComment, BorderLayout.CENTER);

            fieldPanel.add(inputTitle, BorderLayout.NORTH);
            fieldPanel.add(scrollCommentArea, BorderLayout.CENTER);
            buttonPanel.add(buttonCancel);
            buttonPanel.add(buttonSave);

            northContainer.add(labelPanel, BorderLayout.WEST);
            northContainer.add(fieldPanel, BorderLayout.CENTER);
            panel.add(northContainer, BorderLayout.NORTH);
            panel.add(buttonPanel, BorderLayout.SOUTH);
            panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            inputTitle.setText("");
            inputComment.setText("");
            add(panel);
            this.setResizable(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Solution

  • static is going to a potential issue, but inputComment.setPreferredSize(new Dimension(250, 75)); is definitely part of your core problem. Use setRows and setColumns instead.

    inputComment.setRows(4);
    inputComment.setColumns(38);
    

    The basic problem is, JScrollPane relies on the components preferred size to determine if it should show the scroll bars or not (or if they actually do anything)

    See also Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?