我正在编写简单的Swing应用程序。我无法理解为什么不显示列名?这是我的代码:
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);
}
我想知道如何在swing应用程序中提供用户授权和身份验证?当有会话时我喜欢在网络应用程序中,例如,用户名,我可以在任何地方获取它。在这个应用程序中,其中一个任务提供注册用户付款但我如何在登录后获得他的名字?
答案 0 :(得分:3)
将JTable放入JScrollPane是获得正确显示列标题的最简单方法。如果您还没有这样做,那么您需要阅读Oracle Swing Tutorials JTable部分:How to Use Tables。
此外,您似乎已经询问过用户授权等,并在此处收到了接受的答案:User's authorization in Java Swing application。