I’m writting simple Swing application. I can’t uderstand why column name isn’t displayed? Here is my code:
public class HistoryFrame extends JFrame{
public JTable tbProducts=new JTable();
public JButton makePayButton;
public JPanel panel;
public PaymentServiceInterface paymentService=new PaymentService();
public HistoryFrame(){
setTitle("History of Payments");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel=new JPanel();
this.add(panel);
DisplayData(paymentService.getData());
ActionListener makePayAction=new PayAction();
addRegUserButton("Make Payment",makePayAction);
setVisible(true);
}
public JButton addRegUserButton(String label, ActionListener listener){
makePayButton=new JButton(label);
makePayButton.addActionListener(listener);
panel.add(makePayButton);
return makePayButton;
}
private void DisplayData(List<Payment> objectList) {
DefaultTableModel aModel = new DefaultTableModel() {
//setting the jtable read only
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
//setting the column name
Object[] tableColumnNames = new Object[3];
tableColumnNames[0]="User";
tableColumnNames[1] ="Payment";
tableColumnNames[2]="Date";
aModel.setColumnIdentifiers(tableColumnNames);
if (objectList == null) {
this.tbProducts.setModel(aModel);
return;
}
Object[] objects = new Object[3];
ListIterator<Payment> lstrg = objectList.listIterator();
//populating the tablemodel
while (lstrg.hasNext()) {
Payment p = lstrg.next();
objects[0]=p.getUser().getName();
objects[1]=p.getPayment();
objects[2]=p.getDate();
aModel.addRow(objects);
}
//binding the jtable to the model
this.tbProducts.setModel(aModel);
panel.add(tbProducts);
}
I want to know how I can provide user authorization and authentication in swing application? Something like in web apps when there is session and I put in it, for example, user name and I able to get it anywhere. In this app one of the task provide registered user payment but how I can get his name after log in?
Putting your JTable into a JScrollPane is the easiest way to get a decent display of the column headers. If you haven’t done so, you’ll want to go through the Oracle Swing Tutorials JTable section: How to Use Tables.
Also, it appears that you’ve already asked about user authorization etc and received an accepted answer here: User's authorization in Java Swing application.