滚动条未创建/表未显示

时间:2016-03-29 12:58:47

标签: java swing jtable jscrollpane

我正在编写一个程序,其中有一个JTable可以创建(我能够)。但我想为它添加一个滚动条。以下是我的代码。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class TagReplaceUI extends JFrame {

    private JPanel contentPane;
    private JTextField srcTextField;
    private Executor executor = Executors.newCachedThreadPool();
    static DefaultTableModel model = new DefaultTableModel();
    static JTable table = new JTable(model);

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    final TagReplaceUI frame = new TagReplaceUI();
                    frame.add(new JScrollPane(table));
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    // FileDownloadTest downloadTest = new
    // FileDownloadTest(srcTextField.getText(), textArea);

    public TagReplaceUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 552, 358);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        srcTextField = new JTextField();
        srcTextField.setBounds(10, 26, 399, 20);
        contentPane.add(srcTextField);
        srcTextField.setColumns(10);

        JButton srcBtn = new JButton("Source Excel");
        srcBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fChoose = new JFileChooser();
                fChoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                if (fChoose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    srcTextField.setText(fChoose.getSelectedFile().getAbsolutePath());
                } else {
                    System.out.println("No Selection");
                }
            }
        });

        table.setBounds(10, 131, 516, 178);
        contentPane.add(table);
        model.addColumn("Col1");
        model.addColumn("Col2");

        srcBtn.setBounds(419, 25, 107, 23);
        contentPane.add(srcBtn);

        JButton dNcButton = new JButton("Process");
        dNcButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                executor.execute(new Test(srcTextField.getText(), model));
            }
        });
        dNcButton.setBounds(212, 70, 89, 23);
        contentPane.add(dNcButton);

    }

}

当我运行上述程序时,表格框架也不会显示。但是当我删除frame.add(new JScrollPane(table));时,它会在JTable区域中显示该表,但其中没有滚动条。

请告诉我如何在表格中找到滚动条并正确显示数据。

由于

1 个答案:

答案 0 :(得分:2)

首先不要使用null布局,请阅读here

frame.add(new JScrollPane(table));此滚动窗格未显示,因为在null布局中,每个组件都需要有边界。试试下面的代码。我只是将静态变量更改为实例变量。并将scrollpane添加到构造函数中。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class TagReplaceUI extends JFrame {

    private JPanel contentPane;
    private JTextField srcTextField;
    private Executor executor = Executors.newCachedThreadPool();
    private DefaultTableModel model = new DefaultTableModel();
    private JTable table = new JTable(model);

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    final TagReplaceUI frame = new TagReplaceUI();

                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    // FileDownloadTest downloadTest = new
    // FileDownloadTest(srcTextField.getText(), textArea);

    public TagReplaceUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 552, 358);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        srcTextField = new JTextField();
        srcTextField.setBounds(10, 26, 399, 20);
        contentPane.add(srcTextField);
        srcTextField.setColumns(10);

        JButton srcBtn = new JButton("Source Excel");
        srcBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fChoose = new JFileChooser();
                fChoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                if (fChoose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    srcTextField.setText(fChoose.getSelectedFile().getAbsolutePath());
                } else {
                    System.out.println("No Selection");
                }
            }
        });

        JScrollPane scroll = new JScrollPane(table);
        scroll.setBounds(10, 131, 516, 178);
        contentPane.add(scroll);
        model.addColumn("Col1");
        model.addColumn("Col2");

        srcBtn.setBounds(419, 25, 107, 23);
        contentPane.add(srcBtn);

        JButton dNcButton = new JButton("Process");
        dNcButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                executor.execute(new Test(srcTextField.getText(), model));
            }
        });
        dNcButton.setBounds(212, 70, 89, 23);
        contentPane.add(dNcButton);

    }

}