Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name Columns in a JTable

I'm working on a program for a little store. When I click the button "Report", it must show a Table, like this one:

enter image description here

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


}

Employee and product variates depending how many are entered. nulo is how many products are ready to sell.

like image 538
aerojun Avatar asked Nov 21 '11 17:11

aerojun


4 Answers

You should add getColumnName() in your TableModel:

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

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

In your case this could be:

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

And here is a fully working example:

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(); 
    } 
}
like image 177
Jonas Avatar answered Sep 30 '22 04:09

Jonas


Here is the example from the oracle website, you can load the names you want for your columns into an array, then pass the array to the 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);

The resulting table looks like this:

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

like image 30
Hunter McMillen Avatar answered Sep 30 '22 06:09

Hunter McMillen


Try this:

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



}    

}`
like image 45
Guest Avatar answered Sep 30 '22 04:09

Guest


This is more of a manual approach but it works for me.

    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();
like image 45
Boheyga Avatar answered Sep 30 '22 06:09

Boheyga