0

I'd like to achieve the following: A resizable JFrame class that has a panel of buttons on top and a JTextArea on the rest of the JFrame. It should look much like Notepad with buttons instead of drop down menus. So far I was able to write this, but the JTextArea resizes incorrectly.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class TestDialog extends JDialog implements ActionListener {

    public TestDialog(JFrame parent, String title, String message) {
        super(parent, title, true);     
        JPanel buttonPanel = new JPanel();         
        JButton buttonOK = new JButton("OK"); 
        buttonPanel.add(buttonOK); 
        buttonOK.addActionListener(this);
        //Listing 2 goes here   
        add(buttonPanel, BorderLayout.NORTH);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        JTextArea myTextArea = new JTextArea();
        myTextArea.setText(message);
        myTextArea.setName(title);

        myTextArea.setLineWrap(true);
        myTextArea.setWrapStyleWord(true);

        JScrollPane myScrollBar = new JScrollPane(myTextArea,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        add(myScrollBar, BorderLayout.SOUTH);
        setPreferredSize(new Dimension(1100, 800));
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        setVisible(false);        
        dispose();
    }
}

These are two closest hits: (JTextArea on JPanel inside JScrollPane does not resize properly) and (http://www1.cs.columbia.edu/~swapneel/1007/Notepad.java) However, in the first case the JTextArea resizes only to fit all text, not all available JFrame space. The second version version of Notepad doesn't have a scroll bar. As a result it doesn't work with large files.

Also, I have a lot of "overridable method calls in the constructor". I know this is bad. What should I do about it?

Community
  • 1
  • 1
Stepan
  • 1,391
  • 18
  • 40

1 Answers1

1

This app seems to behave as you described. https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/components/TextDemoProject/src/components/TextDemo.java

You should ask a new question regarding the warnings that you see, because it is a separate topic, imho.

Donald W
  • 26
  • 2