JTable行选择 - 未触发任何事件且未显示蓝色

时间:2011-10-16 09:29:56

标签: java swing jtable

我有一个JTable,它只允许使用ListSelectionListener进行行选择,如下面的代码片段所示。 JTable中存在两个问题:

Q1)虽然我使用setSelectionRow方法设置了默认选择的第一行,但是在程序启动时不会触发相关的listSelection事件。只有当我点击另一行而不是第一行时才会触发该事件 - 考虑到以下代码,您认为我应该怎么做才能解决这个问题?

Q2)当我选择一行时,它在JTable中没有用蓝色着色。我注意到在将ListSelectionListener引入JTable的选择模型后,这种情况开始发生。在添加监听器之前,当我选择一行时,该表正在进行这种颜色业务 - 考虑到下面的代码片段,您认为我应该怎么做才能解决这个问题?非常感谢。

     jTableBookings = new javax.swing.JTable();

     jTableBookings.setModel(new MyBookingTableModel(bookingTableData));
     jTableBookings.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
     jTableBookings.setRowSelectionAllowed(true);
     jTableBookings.setCellSelectionEnabled(false);

     jTableBookings.setColumnSelectionAllowed(false);
     initColumnSizesForMinaBokBokningarJTable(jTableBookings);

     //QUESTION 1
     //Set selected row to first row for inital load.
     if (jTableBookings.getModel().getRowCount() > 0) {
         jTableBookings.setRowSelectionInterval(0, 0);
     }

     //QUESTION 2
     jTableBookings.getSelectionModel().addListSelectionListener(new   ListSelectionListener() {
     public void valueChanged(ListSelectionEvent e) {
           int sel = jTableBookings.getSelectedRow();
           fillBookingRecordFields(sel); //Here we do some business logic based on the selected row
     }
    });

    jScrollPane9.setViewportView(jTableBookings);

1 个答案:

答案 0 :(得分:4)

1)myTable.changeSelection(row, column, false, false);

2)禁用setCellSelectionEnabled()和setColumnSelectionAllowed(),然后工作

enter image description here

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

class SelectedColumnTest {

    private JTableHeader header;
    private Object selectedColumn = null;
    private String[] columnNames = {"String", "Integer", "Boolean"};
    private Object[][] data = {{"aaa", 12, true}, {"bbb", 5, false}, {"CCC", 92, 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 JComponent makeUI() {
        //table.setRowSelectionAllowed(true);
        //table.setCellSelectionEnabled(false);
        //table.setColumnSelectionAllowed(false);
        JPanel p = new JPanel(new BorderLayout());
        p.add(new JScrollPane(table));
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.changeSelection(0, 0, false, false);
        return p;
    }

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

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static void createAndShowGUI() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.getContentPane().add(new SelectedColumnTest().makeUI());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}