0

实际上,我的需要是我有一个 38 列的 jtable,我想调用选定的 Jtable 列进行打印。问题不是打印功能,而是如何标记要打印的列?请

4

1 回答 1

2

对于JTable中的特定列,您可以将RowFilterString value "true"传递给checked和un_checked ,然后打印 JTable 并 在打印到打印机后清除JCheckBox(es)String value "false"JCheckBox(es)String to ""RowFilter

或更简单的可能是直接使用布尔值

在此处输入图像描述 在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class JTableFilterDemo {

    private static TableRowSorter<TableModel> sorter;

    public static void main(String[] args) {
        Object[][] data = {{"A", 5, true}, {"B", 2, false}, {"C", 4, false}, {"D", 8, true}};
        String columnNames[] = {"Item", "Value", "Boolean"};
        TableModel model = new DefaultTableModel(data, columnNames) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        JTable table = new JTable(model);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        RowFilter<Object, Object> filter = new RowFilter<Object, Object>() {

            public boolean include(Entry entry) {
                Boolean bol = (Boolean) entry.getValue(2);
                return bol.booleanValue() == true;
            }
        };
        sorter = new TableRowSorter<TableModel>(model);
        sorter.setRowFilter(filter);
        table.setRowSorter(sorter);
        JScrollPane scrollPane = new JScrollPane(table);
        JFrame frame = new JFrame("Filtering Table");
        frame.add(new JButton(new AbstractAction("Toggle filter") {

            private static final long serialVersionUID = 1L;
            private RowFilter<TableModel, Object> filter = new RowFilter<TableModel, Object>() {

                @Override
                public boolean include(javax.swing.RowFilter.Entry<? extends TableModel, ? extends Object> entry) {
                    Boolean bol = (Boolean) entry.getValue(2);
                    return bol.booleanValue() == false;
                }
            };

            @Override
            public void actionPerformed(ActionEvent e) {
                if (sorter.getRowFilter() != null) {
                    sorter.setRowFilter(null);
                } else {
                    sorter.setRowFilter(filter);
                }
            }
        }), BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(scrollPane);
        frame.pack();
        frame.setVisible(true);
    }
}
于 2012-05-31T12:25:00.667 回答