-1

有谁知道我如何在这段代码中添加一个复选框:

String data[][]={
       {"Apple","Banana","Mango"}, {"Apple","Banana","Mango"}, {"Apple","Banana","Mango"}
   };
   String column[]={"Fruits","Fruits","Fruits"};
   table=new JTable(new DefaultTableModel(data, column)){
            private Border outside = new MatteBorder(1, 0, 1, 0, Color.RED);
            private Border inside = new EmptyBorder(0, 1, 0, 1);
            private Border highlight = new CompoundBorder(outside, inside);

            public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
            {
                Component c = super.prepareRenderer(renderer, row, column);
                JComponent jc = (JComponent)c;

                // Add a border to the selected row

                if (isRowSelected(row))
                    jc.setBorder( highlight );

                                return c;
            }
        };


jScrollPane1.setViewportView(table);

我只想添加复选框,这样如果我选中一个复选框,它将突出显示,并且所有选中的复选框都将突出显示。预先感谢您帮助我!

4

1 回答 1

0

这是伪代码,我在存储库的某个地方找到了。根据您的用途使用它。

import java.awt.Color;
import java.awt.Component;

import javax.swing.*;
import javax.swing.table.*;

public class TableCheckBoxHighLight extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTable table;

    public TableCheckBoxHighLight() {
        Object[] columnNames = { "Col1", "Col2", "Select" };
        Object[][] data = { 
                            { "Item1", "123", false },
                            { "Item2", "345", false }, 
                            { "Item3", "678", false },
                            { "Item4", "901", false }
                          };
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @SuppressWarnings("unchecked")
            @Override
            public Class getColumnClass(int column) {
                switch (column) {
                case 0:
                    return String.class;
                case 1:
                    return String.class;

                default:
                    return Boolean.class;
                }
            }

            @Override
            public Component prepareRenderer(TableCellRenderer renderer,
                    int row, int col) {
                Component c = super.prepareRenderer(renderer, row, col);

                int[] selCols = table.getSelectedColumns();
                table.setSelectionBackground(Color.GREEN);
                for (int i : selCols)
                    c.setBackground(Color.RED);

                return c;
            }
        };

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

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TableCheckBoxHighLight frame = new TableCheckBoxHighLight();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocation(150, 150);
                frame.setVisible(true);
            }
        });
    }
}
于 2013-05-30T00:35:50.143 回答