本文介绍了JTable使用行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在JAVA中创建一个像 Microsoft Excel 一样工作的程序。我的问题是如何将行号放在 JTable 的每一行旁边?我已经看到它在其他Java程序中工作 - 我只是无法弄清楚如何将它包含在我的程序中。谷歌给了我,显然它有效。当我在我的程序中使用它时,这没有任何反应。 :/

I am creating a program to work like Microsoft Excel, in JAVA. My problem is how do I put the row numbers beside each row of the JTable? I have seen it working in other Java Programs - I just can not figure out how to include it in mine. Google gave me this website, and apparently it works. When I use it in my program this nothing happen. :/

我有更好的方法在 JTable 中添加行号?

I there a better way of adding row numbers in a JTable?

我查看了已经。

如果之前已经提出这个问题,我很抱歉。

I am sorry if this question has been asked before.

推荐答案

存在简单的解决方案,看起来像@camickr更复杂,(来自forums.sun.com的Walter或Darryl)

There exists simple solution for that, and looks like as @camickr is more complex, (by Walter or Darryl from forums.sun.com)

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.UIManager.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TestTableRowTable {

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

            @Override
            public void run() {
                try {
                    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if (info.getName().equals("Nimbus")) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                    // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e1) {
                    e1.printStackTrace();
                }

                final JTable table = new JTable(10, 6);
                for (int i = 0; i < table.getRowCount(); i++) {
                    table.setValueAt(i, i, 0);
                }
                final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
                table.setRowSorter(sorter);
                final AbstractTableModel model = new AbstractTableModel() {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public int getColumnCount() {
                        return 1;
                    }

                    @Override
                    public Object getValueAt(int row, int column) {
                        return table.convertRowIndexToModel(row);
                    }

                    @Override
                    public int getRowCount() {
                        return table.getRowCount();
                    }
                };
                JTable headerTable = new JTable(model);
                headerTable.setShowGrid(false);
                headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                headerTable.setPreferredScrollableViewportSize(new Dimension(30, 0));
                headerTable.getColumnModel().getColumn(0).setPreferredWidth(30);
                headerTable.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {

                    @Override
                    public Component getTableCellRendererComponent(JTable x, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

                        boolean selected = table.getSelectionModel().isSelectedIndex(row);
                        Component component = table.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(table, value, false, false, -1, -2);
                        ((JLabel) component).setHorizontalAlignment(JLabel.CENTER);
                        if (selected) {
                            component.setFont(component.getFont().deriveFont(Font.BOLD));
                        } else {
                            component.setFont(component.getFont().deriveFont(Font.PLAIN));
                        }
                        return component;
                    }
                });
                table.getRowSorter().addRowSorterListener(new RowSorterListener() {

                    @Override
                    public void sorterChanged(RowSorterEvent e) {
                        model.fireTableDataChanged();
                    }
                });
                table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

                    @Override
                    public void valueChanged(ListSelectionEvent e) {
                        model.fireTableRowsUpdated(0, model.getRowCount() - 1);
                    }
                });
                JScrollPane pane = new JScrollPane(table);
                pane.setRowHeaderView(headerTable);
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.getContentPane().add(pane);
                frame.getContentPane().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) {
                            return ((Number) entry.getValue(0)).intValue() % 2 == 0;
                            //return ((String) entry.getValue(0)).length()>0 ;
                            //return ((Date) entry.getValue(0)).getTime()< ;
                        }
                    };

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (sorter.getRowFilter() != null) {
                            sorter.setRowFilter(null);
                        } else {
                            sorter.setRowFilter(filter);
                        }
                    }
                }), BorderLayout.PAGE_END);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private TestTableRowTable() {
    }
}

这篇关于JTable使用行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:15