简体   繁体   English

在JDialog上使用滚动条摆动TextArea

[英]Swing TextArea with Scrollbar on JDialog

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. 为此,我使用了JDialog并在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. 我也将JScrollPane用于文本区域。

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();
    }
}

在此处输入图片说明

Essentially, txtArea.setPreferredSize(new Dimension(900, 500)); 本质上, 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. 正在删除JTextArea用来确定显示所有文本所需的空间量的自动计算。 You are effectivly saying, there is only 500 pixels worth of height that will ever be needed. 您实际上是在说,将仅需要500个像素的高度。

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 相反,您想要更改JTextAreagetPreferredScrollableViewportSize返回的值。

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 查看Scrollable了解更多详细信息

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... 正如AndrewThompson所指出的那样,一种更好的(也是首选的方式)将是简单地为JTextArea指定行和列,并让其根据平台渲染功能找出含义。

JTextArea txtArea = new JTextArea(40, 100);

Yea for simplicity... 是的,为了简单...

您正在使用dialog.pack()请参见此处并为对话框定义自己的大小

This way youu can use text area with scroll : 这样,您可以将文字区域与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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM