命名JTable中的列

时间:2011-11-21 17:45:47

标签: java swing jtable jframe

我正在为一家小商店开发一个程序。当我点击“报告”按钮时,它必须显示一个表,如下所示:

enter image description here

列名“A”,“B”......“N”必须是员工的姓名。但我无法弄清楚如何。 这是我的代码:

public void Inform()
{
    String[] employee;
    String[] product[];
    this.setLayout(null);

    Inform=new JTable(nulo, employee.length);

     model = new DefaultTableModel() {

            private static final long serialVersionUID = 1L;

            @Override
            public int getColumnCount() {
                return 1;
            }

            @Override
            public boolean isCellEditable(int row, int col) {
                return false;
            }

            @Override
            public int getRowCount() {
                return Inform.getRowCount();
            }
        };

        headerTable = new JTable(model);

       for (int i = 0; i < Inform.getRowCount(); i++) 
            headerTable.setValueAt(product[i], i, 0);


        headerTable.setShowGrid(false);
        headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        headerTable.setPreferredScrollableViewportSize(new Dimension(50, 0));
        headerTable.getColumnModel().getColumn(0).setPreferredWidth(50);

        scrollPane = new JScrollPane(Inform);
        scrollPane.setRowHeaderView(headerTable);
        scrollPane.setBounds(5,5,500,500);
        scrollPane.setEnabled(false);
        this.add(scrollPane);


}

员工和产品会根据输入的数量而变化。 nulo有多少产品可以出售。

4 个答案:

答案 0 :(得分:12)

您应该在TableModel中添加getColumnName()

String[] employee = {"Employee 1", "Employee 2"};

@Override
public String getColumnName(int index) {
    return employee[index];
}

在你的情况下,这可能是:

model = new DefaultTableModel() {

    private static final long serialVersionUID = 1L;
    String[] employee = {"Employee 1", "Employee 2"};

    @Override
    public int getColumnCount() {
         return employee.length;
    }

    @Override
    public boolean isCellEditable(int row, int col) {
         return false;
    }

    @Override
    public int getRowCount() {
         return Inform.getRowCount();
    }

    @Override
    public String getColumnName(int index) {
        return employee[index];
    }
};

这是一个完全有效的例子:

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.table.DefaultTableModel; 

public class TableNamesTest extends JFrame { 

    public TableNamesTest() { 
        DefaultTableModel model = new DefaultTableModel() { 
            String[] employee = {"emp 1", "emp 2"}; 

            @Override 
            public int getColumnCount() { 
                return employee.length; 
            } 

            @Override 
            public String getColumnName(int index) { 
                return employee[index]; 
            } 
        }; 

        JTable table = new JTable(model); 
        add(new JScrollPane(table)); 
        pack(); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
        setVisible(true); 
    } 

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

答案 1 :(得分:4)

以下是oracle网站上的示例,您可以将列所需的名称加载到数组中,然后将数组传递给JTable

String[] columnNames = {"First Name",
                        "Last Name",
                        "Sport",
                        "# of Years",
                        "Vegetarian"};

//Then the Table is constructed using these data and columnNames:

JTable table = new JTable(data, columnNames);

结果表如下所示:

http://docs.oracle.com/javase/tutorial/figures/uiswing/components/TableSelection-new.png

关于JTable教程的链接:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

答案 2 :(得分:2)

试试这个:

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;


public class Class1 extends JFrame{

static JTable table;    
static String[] Employees = {"Employee1","Employee2","Employee3","Employee4"};

static int NumberOfRows=4;
static int NumberOfColumns=4;




public static void main(String[] args){

table = new JTable(NumberOfRows,NumberOfColumns);

for(int i=0;i<Employees.length;i++){

TableColumn tc = table.getColumnModel().getColumn(i);
tc.setHeaderValue(Employees[i]);

DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
dtcr.setHorizontalAlignment(SwingConstants.CENTER); //For Aligning the Elements of all columns to CENTER
tc.setCellRenderer(dtcr);
}


JFrame frame = new JFrame();
frame.add(new JScrollPane(table));
frame.setSize(300,300);
frame.setVisible(true);



}    

}`

答案 3 :(得分:0)

这更多是一种手动方法,但对我有用。

    JTabel TABLE = new JTable();
    JTableHeader HEADER = TABLE.getTableHeader();
    TableColumnModel TMC = HEADER.getColumnModel();
    TableColumn TC = TMC.getColumn(0);
    TC.setHeaderValue("Person 1");
    TableColumn TC1 = TMC.getColumn(1);
    TC1.setHeaderValue("Person 2");
    HEADER.repaint();
    TABLE.getTableHeader().repaint();