5

在我的 JTable 中,我想使用右键单击选项重命名表标题。右键单击任何标题,它提供了重命名标题的选项。但是当我这样做时,它会重命名所选列的标题。所以我必须先左键单击该列,然后才能进一步处理。有什么方法可以让我在任何列上单击鼠标右键,它也会被选中,以便在重命名后它的值被重命名。

我的动作监听器如下:

int column = jTable1.getSelectedColumn();
                        if (column == -1) {
                            return;
                        }
                        if (column == 0) {
                            JOptionPane.showMessageDialog(jTable1, "You can not rename this column.");
                            return;
                        }
                        boolean blag = true;
                        String sColumnName = null;
                        while (blag) {

                        sColumnName = (String) JOptionPane.showInputDialog(jTable1, "Enter Column Name", "Add Column", JOptionPane.INFORMATION_MESSAGE, null, null, null);
                        if (sColumnName == null) {
                            return;
                        }
                        if (sColumnName.trim().equalsIgnoreCase("")) {
                            JOptionPane.showMessageDialog(jTable1, "Column name can not be blank.");
                            blag = true;
                        } else {
                            blag = false;
                        }
                        Vector vColumns = new Vector();
                        for (int i = 0; i < defaultTableModel.getColumnCount(); i++) {
                            vColumns.add(defaultTableModel.getColumnName(i));
                        }
                        if (vColumns.contains(sColumnName)) {
                            JOptionPane.showMessageDialog(jTable1, "Column name " + sColumnName + " already exist.");
                            return;
                        }
                    }
                    int viewColumn = jTable1.convertColumnIndexToView(column);
                    int totalColumn = defaultTableModel.getColumnCount();
                    Object[] headers = new Object[totalColumn];
                    for (int i = 0; i < totalColumn; i++) {
                        if (i == column) {
                            headers[i] = sColumnName;
                        } else {
                            headers[i] = defaultTableModel.getColumnName(i);
                        }
                    }
                    defaultTableModel.setColumnIdentifiers(headers);
4

1 回答 1

6

我想念那里

1)MouseListener并确定选择Column的值返回

    header.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            JTableHeader h = (JTableHeader) e.getSource();
            int i = h.columnAtPoint(e.getPoint());
            Object o = h.getColumnModel().getColumn(i).getHeaderValue();
            if (i < 0 || o == selectedColumn) {
                selectedColumn = null;
                return;
            }
            selectedColumn = o;
            h.requestFocusInWindow();
        }
    });

2) 变更代码header.getColumnModel().getColumn(i).setHeaderValue("Clicked");

简单的例子

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

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

public class SelectedTableHeader {

    private JFrame frame = new JFrame("Table Demo");
    private JTableHeader header;
    private Object selectedColumn = null;
    private String[] columnNames = {"String", "Integer", "Float", "Double", "Locale & Double", "Boolean"};
    private Object[][] data = {
        {"aaa", new Integer(12), new Float(12.15), new Double(100.05), new Double(12.05), true},
        {"bbb", new Integer(5), new Float(7.154), new Double(6.1555), new Double(417.55), false},
        {"CCC", new Integer(92), new Float(0.1135), new Double(3.1455), new Double(11.05), true},
        {"ddd", new Integer(12), new Float(31.15), new Double(10.05), new Double(23.05), true},
        {"eee", new Integer(5), new Float(5.154), new Double(16.1555), new Double(17.55), false},
        {"fff", new Integer(92), new Float(4.1135), new Double(31.1455), new Double(3.05), true}};
    private TableModel model = new DefaultTableModel(data, columnNames) {

        private static final long serialVersionUID = 1L;

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

    public SelectedTableHeader() {
        header = table.getTableHeader();
        header.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                JTableHeader h = (JTableHeader) e.getSource();
                int i = h.columnAtPoint(e.getPoint());
                Object o = h.getColumnModel().getColumn(i).getHeaderValue();
                if (i < 0) {
                    selectedColumn = null;
                    return;
                }
                selectedColumn = o;
                header.getColumnModel().getColumn(i).setHeaderValue("Clicked");
                h.requestFocusInWindow();
            }
        });
        final TableCellRenderer hr = table.getTableHeader().getDefaultRenderer();
        header.setDefaultRenderer(new TableCellRenderer() {

            private JLabel lbl;

            @Override
            public Component getTableCellRendererComponent(
                    JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                if (selectedColumn == value) {
                    lbl = (JLabel) hr.getTableCellRendererComponent(table, value, true, true, row, column);
                    lbl.setBorder(BorderFactory.createCompoundBorder(lbl.getBorder(), BorderFactory.createLineBorder(Color.red, 1)));
                    lbl.setHorizontalAlignment(SwingConstants.LEFT);
                } else {
                    lbl = (JLabel) hr.getTableCellRendererComponent(table, value, false, false, row, column);
                    lbl.setBorder(BorderFactory.createCompoundBorder(lbl.getBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 0)));
                    lbl.setHorizontalAlignment(SwingConstants.CENTER);
                }
                if (column == 0) {
                    lbl.setForeground(Color.red);
                } else {
                    lbl.setForeground(header.getForeground());
                }
                /*return (value == selectedColumn) ? hr.getTableCellRendererComponent(
                table, value, true, true, row, column) : hr.getTableCellRendererComponent(
                table, value, false, false, row, column);*/
                return lbl;
            }
        });

        table.setRowHeight(20);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scroll = new JScrollPane(table);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(scroll);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    }

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

            @Override
            public void run() {
                SelectedTableHeader selectedTableHeader = new SelectedTableHeader();
            }
        });
    }
}

3)另一种方法是将CellEditor 添加到 JTableHeader(注意从未尝试过,并且不确定编辑器是否按预期工作,仅作为示例,我确定需要进行一些修改)

于 2012-04-11T12:51:09.887 回答