Question

I have a scenario like, when user clicks on a button a pop window will be opened with a text area. The text area will have some contents with scroll bar when need. To achieve this i have used JDialog and added a text area to the JDialog. In my case i am able to show the dialog on button click and the text area on dialog with contents. But i could not get the scroll bar for the text area. I have used JScrollPane for text area too.

public class DialogPanel {

    public void createDialog() {
        final JFrame mainFrame = new JFrame();
        mainFrame.setVisible(true);
        mainFrame.setSize(500, 600);
        mainFrame.setLayout(new BorderLayout());
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton btn = new JButton("Open Dialog");
        mainFrame.add(btn, BorderLayout.SOUTH);
        JTextField txtField = new JTextField();
        mainFrame.add(txtField, BorderLayout.NORTH);
        btn.setPreferredSize(new Dimension(100, 100));
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JDialog dialog = new JDialog(mainFrame);
                dialog.setLocationByPlatform(true);
                JTextArea txtArea = new JTextArea();
                txtArea.setAutoscrolls(true);
                txtArea.setPreferredSize(new Dimension(900, 500));
                txtArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                txtArea.setFont(new Font("courier new", Font.PLAIN, 12));
                txtArea.setLineWrap(true);
                JScrollPane txtAreaScroll = new JScrollPane();
                txtAreaScroll.setViewportView(txtArea);
                txtAreaScroll.setAutoscrolls(true);

                File file;
                String line = null;
                StringBuilder fileContents = new StringBuilder();
                try {
                    file = new File(
                            "D:\\Softwares\\Apache\\apache-tomcat-7.0.47\\RUNNING.txt");
                    BufferedReader reader = new BufferedReader(new FileReader(
                            file));
                    while ((line = reader.readLine()) != null) {
                        fileContents.append(line + "\n");
                    }
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                txtArea.setText(fileContents.toString());

                dialog.add(txtAreaScroll);
                dialog.pack();
                dialog.setVisible(true);
            }
        });
    }

    public static void main(String[] args) {
        DialogPanel dialogPanel = new DialogPanel();
        dialogPanel.createDialog();
    }
}

enter image description here

Was it helpful?

Solution

Essentially, txtArea.setPreferredSize(new Dimension(900, 500)); is removing the automatic calculations employed by JTextArea that it uses to determine the amount of space it needs to display all the text. You are effectivly saying, there is only 500 pixels worth of height that will ever be needed.

You "could" set the preferred size of the scroll pane, but that's not really recommended. Instead, you want to change the value returned by getPreferredScrollableViewportSize in the JTextArea

This tells the scroll pane how big to make the viewable area ... if it can...

JTextArea txtArea = new JTextArea() {

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return new Dimension(900, 500);
    }

};

Take a look at Scrollable for more details

Updated

As AndrewThompson has pointed out, a better (and preferred way) would be to simply specify the rows and columns for the JTextArea and let it figure out what that means based on the platforms rendering capabilities...

JTextArea txtArea = new JTextArea(40, 100);

Yea for simplicity...

OTHER TIPS

You are using dialog.pack() see here and define own size for dialog

This way youu can use text area with scroll :

                JTextArea txtArea = new JTextArea(40,100);
                txtArea.setAutoscrolls(true);
                txtArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                txtArea.setFont(new Font("courier new", Font.PLAIN, 12));
                txtArea.setLineWrap(true);
                txtArea.setText(j);
                JScrollPane txtAreaScroll = new JScrollPane (txtArea, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                txtAreaScroll.setViewportView(txtArea);
                txtAreaScroll.setAutoscrolls(true);
                // now add scroll pane in frame
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top