使用Java Swing在JTable上获取字符串值而不是复选框?

时间:2017-10-27 09:17:53

标签: java swing jtable jcheckbox

我正在创建一个JTable,我已经设法创建了一个可滚动的表,并添加了一些随机数据。现在我想尝试在每行的最后一列添加一个复选框。

现在这是我到目前为止所得到的:

Screenshot of Table

当你查看他的表时,你可以看到,而不是获取复选框,我得到一个字符串值为“false”的列,这不是我想要的。我想要一个可选择的框,我想允许多个选择。这是该类的代码:

import java.awt.BorderLayout; 
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;


public class BiogramTable extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private JCheckBox checkbox;
    private static final long serialVersionUID = 1L; 




    /**
     * Create the frame.
     */
    public BiogramTable() {
        Object[] columns = {"Name", "Age" , "Gender" , "Boolean"};

        Object[][] data = {{"John", "18", "Male", false}, 
                {"Jessica", "19", "Female", false}, 
                {"Dave", "52", "Male", false},
                {"Jake", "30", "Male", false}, 
                {"Jeremy", "14", "Male", false},
                {"Jemma", "34", "Female", false},
                {"Amy", "16", "Female", false},
                {"Patrick", "18", "Male", false}};
        DefaultTableModel model = new DefaultTableModel(data, columns);
        final JCheckBox checkBox = new JCheckBox();


        table = new JTable(model) {

            private static final long serialVersionUID = 1L;


            //@Override
            //public Class getColumnClass(int column) {
                //switch (column) {
                //case 0:
                //  return String.class;
                //case 1:
                    //return String.class;
                //case 2:
                    //return Integer.class;
                //case 3:
                    //return Double.class;
                //default:
                    //return Boolean.class;
                //}
            //}

        };

        //table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                BiogramTable frame = new BiogramTable();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocation(150, 150);
                frame.setVisible(true);
            }

        });
    }
}

我已经对此做了大量研究,但我还没有找到使用swing的解决方案。如果您能让我知道我的方法有什么问题,我会很高兴,让我知道正确的方法是什么,因为这是我第一次使用Windows构建器在Java swing中创建JTable。我在堆栈溢出中看到了类似的问题,但我的问题是不同的,因为我得到的是String而不是复选框。所以我的问题是为什么会这样?

1 个答案:

答案 0 :(得分:3)

您应该根据您的数据要求覆盖getColumnClass方法。请参阅以下更改。

table image

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;

class BiogramTable extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private JCheckBox checkbox;
    private static final long serialVersionUID = 1L;

    /**
     * Create the frame.
     */
    public BiogramTable() {
        Object[] columns = {"Name", "Age", "Gender", "Boolean"};

        Object[][] data = {{"John", "18", "Male", false},
        {"Jessica", "19", "Female", false},
        {"Dave", "52", "Male", false},
        {"Jake", "30", "Male", false},
        {"Jeremy", "14", "Male", false},
        {"Jemma", "34", "Female", false},
        {"Amy", "16", "Female", false},
        {"Patrick", "18", "Male", false}};
        DefaultTableModel model = new DefaultTableModel(data, columns);
        final JCheckBox checkBox = new JCheckBox();

        table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:
                        return String.class;
                    case 1:
                        return Integer.class;
                    case 2:
                        return String.class;
                    case 3:
                        return Boolean.class;
                    default:
                        return String.class;
                }
            }
        };

        //table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                BiogramTable frame = new BiogramTable();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocation(150, 150);
                frame.setVisible(true);
            }

        });
    }
}