2012-08-08 75 views
1

我的目標是創建一個JTable,並僅渲染最左列單元格,目的是爲表格創建行標題。Java JTable:如何爲簡單行標題目的渲染左列單元格

我在網上遇到過的所有行表格例子看起來很複雜或不符合我的目的,所以我想知道有沒有簡單的方法通過渲染左列單元格來創建JTable行標題?

下面我有一個簡單的表2列和兩行的代碼。是否有人可以修改這個問題,或者用簡單的術語解釋,我如何才能爲了行標題目的渲染最左邊的列。

謝謝。

import javax.swing.*; 
import java.awt.*; 
import javax.imageio.*; 
import java.io.*; 
import java.awt.image.BufferedImage; 
import java.util.*; 
public class GUITable extends JFrame{public GUITable(){ 


init(); 
    } 

    public final void init(){ 
    String[] columnNames = {"", "Gross Weight"}; 

    Object[][] data = { 
    {"", new Integer(100)},}; 

    final JTable table = new JTable(data, columnNames); 
    table.setPreferredScrollableViewportSize(new Dimension(500, 70)); 
    table.setFillsViewportHeight(true); 

    JScrollPane scrollPane = new JScrollPane(table); 
    add(scrollPane); 
} 

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

public void run() { 
try { 
    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
    GUITable ex = new GUITable(); 
    ex.setVisible(true); 
    } 
    }); 
    } 
} 
+0

所以這會是一些像Excel中的固定列? – MadProgrammer 2012-08-08 07:38:39

回答

1

是 - 通過使用自定義TableCellRenderer,您可以修改的方式顯示第一列(僅第一列)。

從本質上講,你可以用它來設置TableCellRenderer僅在第一列:

table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer()); 

而且可以延長DefaultTableCellRenderer照顧任何特殊的渲染,你想做的事:

//Custom Renderer - does the default rendering except if told the row should be a different color 
public static class CustomRenderer extends DefaultTableCellRenderer{ 

    public CustomRenderer(){ 
     super(); 
     //Customize the rendering however you want 
     setBackground(UIManager.getColor("TableHeader.background")); 
    } 
} 

把它放在你的例子中:

import javax.swing.*; 
import javax.swing.table.DefaultTableCellRenderer; 

import java.awt.*; 
public class TestTable extends JFrame{ 

    public TestTable(){ 
     init(); 
    } 

    public final void init(){ 
     String[] columnNames = {"", "Gross Weight"}; 

     Object[][] data = {{"", new Integer(100)},}; 

     final JTable table = new JTable(data, columnNames); 
     // Add Renderer to first column only 
     table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer()); 
     table.setPreferredScrollableViewportSize(new Dimension(500, 70)); 
     table.setFillsViewportHeight(true); 

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

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

      public void run() { 
       try { 
        UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       TestTable ex = new TestTable(); 
       ex.pack(); 
       ex.setVisible(true); 

      } 
     }); 
    } 

    //Custom Renderer - does the default rendering except if told the row should be a different color 
    public static class CustomRenderer extends DefaultTableCellRenderer{ 

     public CustomRenderer(){ 
      super(); 
      //Customize the rendering however you want 
      setBackground(UIManager.getColor("TableHeader.background")); 
     } 
    } 

} 
+0

太棒了,謝謝! – Jnanathan 2012-08-10 09:42:18

1

您的代碼示例可以

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

public class GUITable extends JFrame { 

    public GUITable() { 
     init(); 
    } 

    public final void init() { 
     String[] columnNames = {"", "Gross Weight"}; 
     Object[][] data = {{"", new Integer(100)},}; 
     final JTable table = new JTable(data, columnNames); 
     table.setPreferredScrollableViewportSize(new Dimension(500, 70)); 
     table.setFillsViewportHeight(true); 
     JScrollPane scrollPane = new JScrollPane(table); 
     add(scrollPane); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     pack(); 
    } 

    public static void main(String[] args) { 
     try { 
      UIManager.setLookAndFeel(
       "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");//Java6 
      //UIManager.setLookAndFeel(
       //"javax.swing.plaf.nimbus.NimbusLookAndFeel");//Java7 
     } catch (Exception fail) { 
     } 
     SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 

       GUITable ex = new GUITable(); 
       ex.setVisible(true); 
      } 
     }); 
    } 
} 

不是從你的描述確定,你的意思Row Number Table by @camickr或其他half_sized attempt

+0

mKorbel,謝謝你的回覆。你的「半身」嘗試更接近我所期望的。有沒有更簡單的方法來實現行頭渲染,通過訪問列索引並將其渲染爲頭部的索引? – Jnanathan 2012-08-08 07:40:54

+0

'有沒有更簡單的',據我所知,在大多數情況下JTable不希望工作添加/刪除行,也沒有正確的RowSorter或RowFilter,最好可以使用@camickr代碼,當然也許在那裏是另一個代碼 – mKorbel 2012-08-08 07:52:15