1

我的目标是创建一个 JTable,并仅呈现最左侧的列单元格,目的是为表创建行标题。

我在网上遇到的所有行表示例似乎都令人费解或不符合我的目的,所以我想知道是否有一种简单的方法可以通过仅渲染左列单元格来创建 JTable 行标题?

下面我有一个包含 2 列和 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);
    }
    });
  }
}
4

2 回答 2

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);
            }
        });
    }
}

从您的描述中不确定,您是指@camickr 的行号表,还是另一个half_sized 尝试

于 2012-08-08T07:20:04.693 回答
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"));
        }
    }

} 
于 2012-08-08T15:12:46.947 回答