English 中文(简体)
缩略语
原标题:Name Columns in a JTable

Im公司为一家小店经营一个方案。 当我点击“报告”时,它必须显示一个表象:

“entergraph

Column names "A", "B"..."N", must be the names of the employees. But I can t figure out how. Here is my code:

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


}

雇员和产品数量取决于加入人数。 努洛是有多少产品可以出售。

最佳回答

页: 1 注

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(); 
    } 
}
问题回答

这里就是专栏网站的例子,你可以把你想要的栏目打成一个阵列,然后将阵列通过<代码>。 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”/。

Link to tutorial on JTables: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data/a>

为此:

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



}    

}`

这更是一种手工方法,但对我来说是可行的。

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签