JTable 的问题以及有关授权和身份验证的问题
我正在编写简单的 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 应用程序中提供用户授权和身份验证?就像在网络应用程序中,当有会话时,我将其放入其中,例如用户名,我可以在任何地方获取它。在此应用程序中,其中一项任务提供注册用户付款,但登录后如何获取他的姓名?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 JTable 放入 JScrollPane 中是获得列标题良好显示的最简单方法。如果您还没有这样做,您需要阅读 Oracle Swing 教程 JTable 部分: 如何使用表格。
另外,您似乎已经询问过用户授权等问题,并在此处收到了接受的答案: Java Swing 应用程序中的用户授权。
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.