I'm working on a program for a little store. When I click the button "Report", it must show a Table, like this one:
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.
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();
}
}
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:
Link to tutorial on JTables: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data
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);
}
}`
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With