2011-12-16 134 views
0

我想在列排序後以編程方式選擇JTable中的第一行。我能怎麼做? 我寫了我自己的類來在JTable的頭部上的鼠標事件中偵聽。在這個類中,我在表中分配或不分配TableRowSorter。例如:先點擊排序升序,然後點擊降序等等。如果我把這個選擇放在這門課結束時,結果是不可預測的。行排序後在JTable中選擇行

class ClickHeader extends MouseAdapter { 

JTable table; 
static int headerClickCount; 
TableRowSorter sorter = new TableRowSorter(); 
static int lastColumn = -1; 


ClickHeader(JTable t) { 
    table = t; 
    headerClickCount=0; 
    lastColumn=-1; 
} 

@Override 
public void mousePressed(MouseEvent e) {  
    TableModel model = table.getModel(); 
    int vColIndex = table.getTableHeader().getColumnModel().getColumnIndexAtX(e.getX()); 
    if (vColIndex == -1) { 
     return; 
    } 

    vColIndex = table.convertColumnIndexToModel(vColIndex); 

    if(lastColumn != vColIndex){ 
     lastColumn = vColIndex; 
     headerClickCount=0; 
    } 
    // 
    headerClickCount++; 
    if (headerClickCount == 3) { 
     table.setRowSorter(null); 
     sorter = new TableRowSorter(); 
     headerClickCount = 0; 
    } else { 
     if (table.getRowSorter() == null) { 
      sorter.setModel(model); 

     } 

     table.setRowSorter(sorter); 
    } 

    table.getTableHeader().repaint(); 
} 

}

+1

不明白你想什麼來實現:JTable中(如JDK6的)支持清理關閉對話框。你所要做的就是通過table.setAutoCreateRowSorter(true)來啓用它。 – kleopatra 2011-12-16 15:49:41

+0

我想在顯示排序方向的標題中顯示一個圖標。所以我需要重寫一些代碼。 – 2011-12-16 17:42:31

回答

2

如果你需要選擇第一次。您JTable

setRowSelectionInterval(int index0, int index1); 

,更特別爲:行,那麼你可以使用JTablechangeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend),例如

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

enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

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

public class TableWithGradient extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JTable myTable; 
    public int columnIndex = 0; 
    public boolean columnIndexAble = false; 

    public TableWithGradient() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; 
     Object[][] data = { 
      {"Kathy", "Smith", "Snowboarding", new Integer(5), false}, 
      {"John", "Doe", "Rowing", new Integer(3), true}, 
      {"Sue", "Black", "Knitting", new Integer(2), false}, 
      {"Jane", "White", "Speed reading", new Integer(20), true}, 
      {"Joe", "Brown", "Pool", new Integer(10), false}, 
      {"Kathy", "Smith", "Snowboarding", new Integer(5), false}}; 
     myTable = new TableBackroundPaint(data, columnNames); 
     myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     myTable.setGridColor(Color.gray); 
     final TableCellRenderer tcrOs = myTable.getTableHeader().getDefaultRenderer(); 
     myTable.getTableHeader().setDefaultRenderer(new TableCellRenderer() { 

      @Override 
      public Component getTableCellRendererComponent(JTable table, 
        Object value, boolean isSelected, boolean hasFocus, int row, int column) { 
       JLabel lbl = (JLabel) tcrOs.getTableCellRendererComponent(
         table, value, isSelected, hasFocus, row, column); 
       lbl.setForeground(Color.black); 
       lbl.setBorder(BorderFactory.createCompoundBorder(
         lbl.getBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 0))); 
       lbl.setHorizontalAlignment(SwingConstants.LEFT); 
       if (columnIndex == column && columnIndexAble) { 
        lbl.setForeground(Color.red); 
        lbl.setBackground(Color.yellow); 
       } else { 
        lbl.setForeground(Color.black); 
        lbl.setBackground(Color.orange); 
       } 
       return lbl; 
      } 
     }); 
     myTable.setPreferredScrollableViewportSize(new Dimension(
       myTable.getPreferredSize().width + 100, myTable.getPreferredSize().height)); 
     final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(myTable.getModel()); 
     myTable.setRowSorter(sorter); 
     JTableHeader header = myTable.getTableHeader(); 
     header.addMouseListener(new OsPanelImportColumnHeaderListener(this)); 
     add(new JScrollPane(myTable), BorderLayout.CENTER); 
     setLocation(100, 100); 
     pack(); 
     setVisible(true); 
    } 

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

      @Override 
      public void run() { 
       TableWithGradient tableWithGradient = new TableWithGradient(); 
      } 
     }); 
    } 
} 

class OsPanelImportColumnHeaderListener extends MouseAdapter { 

    private TableWithGradient osPanelImport; 

    public OsPanelImportColumnHeaderListener(TableWithGradient osPanelImport) { 
     this.osPanelImport = osPanelImport; 
    } 

    @Override 
    public void mouseClicked(MouseEvent evt) { 
     osPanelImport.columnIndexAble = true; 
     JTable table = ((JTableHeader) evt.getSource()).getTable(); 
     TableColumnModel colModel = table.getColumnModel(); 
     osPanelImport.columnIndex = colModel.getColumnIndexAtX(evt.getX()); 
     table.changeSelection(0, 0, false, false); 
     return; 
    } 
} 

class TableBackroundPaint extends JTable { 

    private static final long serialVersionUID = 1L; 

    TableBackroundPaint(Object[][] data, Object[] head) { 
     super(data, head); 
     setOpaque(false); 
     ((JComponent) getDefaultRenderer(Object.class)).setOpaque(false); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     Color background = new Color(168, 210, 241); 
     Color controlColor = new Color(230, 240, 230); 
     int width = getWidth(); 
     int height = getHeight(); 
     Graphics2D g2 = (Graphics2D) g; 
     Paint oldPaint = g2.getPaint(); 
     g2.setPaint(new GradientPaint(0, 0, background, width, 0, controlColor)); 
     g2.fillRect(0, 0, width, height); 
     g2.setPaint(oldPaint); 
     for (int row : getSelectedRows()) { 
      Rectangle start = getCellRect(row, 0, true); 
      Rectangle end = getCellRect(row, getColumnCount() - 1, true); 
      g2.setPaint(new GradientPaint(start.x, 0, controlColor, 
        (int) ((end.x + end.width - start.x) * 1.25), 0, Color.orange)); 
      g2.fillRect(start.x, start.y, end.x + end.width - start.x, start.height); 
     } 
     super.paintComponent(g); 
    } 
} 
+0

不起作用。它隨機選擇一行。此外,如果我轉換索引查看或模型 – 2011-12-16 15:23:22

2

要做到這一點只需使用第一排做到:

table.setRowSelectionInterval(0,0); 

後,該表已排序