2013-10-17 85 views
2

我已左對齊我的單元格渲染器,但如何左對齊我的單元格編輯器?這發生在我編輯的細胞更改TableCellEditor對象的對齊方式

sample

import java.awt.BorderLayout; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTable; 
import javax.swing.JTextField; 
import javax.swing.JTree; 
import javax.swing.SwingConstants; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableCellEditor; 
import javax.swing.table.TableCellRenderer; 

public class TextPaneTest extends JFrame { 

    public static void main(String args[]) { 
     new TextPaneTest(); 
    } 

    public TextPaneTest() { 

     // Main panel 
     JPanel panel = new JPanel(); 
     panel.setLayout(new BorderLayout()); 

     JTable table = new JTable() { 
      @Override 
      public TableCellEditor getCellEditor(int row, int column) { 
       if (column == 1) 
       { 
        //////////////////////// 
        // What might go here // 
        //////////////////////// 
        return getDefaultEditor(Boolean.class); 
       } 
       return super.getCellEditor(row, column); 
      } 

      @Override 
      public TableCellRenderer getCellRenderer(int row, int column) { 
       if (column == 1) 
       { 
        TableCellRenderer renderer = getDefaultRenderer(Boolean.class); 
        ((JCheckBox)renderer).setHorizontalAlignment(SwingConstants.LEFT); 
        return renderer; 
       } 
       return super.getCellRenderer(row, column); 
      } 
     }; 

     String[] columnNames = {"Name", "Value"}; 
     Object[][] data = {{"one", true}, {"two", true}}; 
     DefaultTableModel model = new DefaultTableModel(data, columnNames);  
     table.setModel(model); 

     panel.add(table); 
     // Add everything to the frame 
     this.add(panel); 
     this.setSize(300, 200); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
    } 
} 

回答

2

作爲第一步,改變這種狀況的一個:由

if(getValueAt(row, column) instanceof Boolean) 

if (column == 1) 

會讓你的方法更靈活(考慮布爾值的5列,你不想檢查每個列號重新轉動默認的渲染器/編輯器)。

我也建議你製作自己的細胞渲染器,從JCheckBox延伸,並將其設置爲Boolean類的默認細胞渲染器。它會避免你在getCellRenderer()方法內改變對齊方式。

注意默認getDefaultRenderer(Boolean.class)返回JCheckBox但在邏輯上說,應該返回一個JCheckBoxgetDefaultEditor(Boolean.class)返回javax.swing.JTable.BooleanEditor。所以也許投射默認渲染器可能是一個問題。

我做了它的工作是這樣的:

JTable table = new JTable(dtm){ 
    @Override 
    public TableCellEditor getCellEditor(int row, int column) { 
     if(getValueAt(row, column) instanceof Boolean){ 
      return super.getDefaultEditor(Boolean.class); 
     } 
     return super.getCellEditor(row, column); 
    } 

    @Override 
    public TableCellRenderer getCellRenderer(int row, int column) { 
     if(getValueAt(row, column) instanceof Boolean){ 
      JCheckBox cb = (JCheckBox) getDefaultRenderer(Boolean.class); 
      cb.setHorizontalAlignment(SwingConstants.LEFT); 
      return getDefaultRenderer(Boolean.class); 
     } 

     return super.getCellRenderer(row, column); 
    } 
}; 

JCheckBox cb = new JCheckBox(); 
cb.setHorizontalAlignment(SwingConstants.LEFT); 
TableCellEditor tce = new DefaultCellEditor(cb);   
table.setDefaultEditor(Boolean.class, tce); 

正如你所看到的鍵設置一個DefaultCellEditorJCheckBoxBoolean類的默認單元格編輯器開始。

截圖

enter image description here

SSCCE

下面是一個完整SSCCE來測試這一點:

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import javax.swing.DefaultCellEditor; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.SwingConstants; 
import javax.swing.SwingUtilities; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableCellEditor; 
import javax.swing.table.TableCellRenderer; 

public class Demo { 

    private void initGUI(){ 

     DefaultTableModel dtm = new DefaultTableModel(new Object[]{"Property", "Value"}, 0); 
     dtm.addRow(new Object[]{"Property 1", true}); 
     dtm.addRow(new Object[]{"Property 2", false}); 

     JTable table = new JTable(dtm){ 
      @Override 
      public TableCellEditor getCellEditor(int row, int column) { 
       if(getValueAt(row, column) instanceof Boolean){ 
        return super.getDefaultEditor(Boolean.class); 
       } 
       return super.getCellEditor(row, column); 
      } 

      @Override 
      public TableCellRenderer getCellRenderer(int row, int column) { 
       if(getValueAt(row, column) instanceof Boolean){ 
        JCheckBox cb = (JCheckBox) getDefaultRenderer(Boolean.class); 
        cb.setHorizontalAlignment(SwingConstants.LEFT); 
        return getDefaultRenderer(Boolean.class); 
       } 

       return super.getCellRenderer(row, column); 
      } 
     }; 

     JCheckBox cb = new JCheckBox(); 
     cb.setHorizontalAlignment(SwingConstants.LEFT); 
     TableCellEditor tce = new DefaultCellEditor(cb);   
     table.setDefaultEditor(Boolean.class, tce); 

     JScrollPane scrollPane = new JScrollPane(table); 
     scrollPane.setPreferredSize(new Dimension(300,200)); 

     JPanel content = new JPanel(new BorderLayout()); 
     content.add(scrollPane); 

     JFrame frame = new JFrame("Demo"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.setContentPane(content); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true);   
    } 


    public static void main(String[] args) {   
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Demo().initGUI(); 
      } 
     }); 
    } 

} 
相關問題