gpt4 book ai didi

java - AbstractTabelModel 将不会显示 JTable 列名称

转载 作者:行者123 更新时间:2023-11-30 03:23:17 25 4
gpt4 key购买 nike

出于某种原因,我的 JTable 没有显示它的列名称?!我确信我所做的一切都是正确的。我从演示中复制了这个,所以我不明白为什么它不起作用。

这是我的代码:

public class MemTableModel extends AbstractTableModel{
private ArrayList<member> members = new ArrayList<member>();
private String[] columnNames = {"ID", "Name", "Email", "Country", "Genre",
"Gender", "Description", "Type", "Limit", "Card No", "Expiry Date"};

public MemTableModel(){
LoadTableFromDB();
}
public int getRowCount(){
return members.size();
}
public int getColumnCount(){
return columnNames.length;
}

public Object getValueAt(int row, int col){
//Get the row from the about get method
member f = members.get(row);
switch(col){
case 0: return f.getmembId();
case 1: return f.getname();
case 2: return f.getemail();
case 3: return f.getcountry();
case 4: return f.getfavGenre();
case 5: return f.getgender();
case 6: return f.getdescription();
case 7: return f.getmemberType();
case 8: return f.getsongLimit();
case 9: return f.getcard_no();
case 10: return f.getexpiry_date();
}
return null;
}
public String getColumnName(int col){
return columnNames[col];
}

public member getRow(int row){
member c = members.get(row);
return c;
}
public Connection getConnection(){
Connection conDB = null;
/****** DEFAULT MYSQL DRIVERS **************************/
String url = connection.geturl();
String username = connection.getUsername();
String password = connection.getPassword();
try{
//load the MYSQL driver
Class.forName(connection.getDriver());
conDB = DriverManager.getConnection(url, username, password);
}
catch(Exception e){

}
return conDB;
}
//Load all DB values into ARRAY
public void LoadTableFromDB(){
Connection conDB = null;
Statement stmt = null;
ResultSet r = null;
try{
//Connection + Statement
conDB = getConnection();
stmt = conDB.createStatement();
//Queries
String sqlSelectAll = "SELECT * FROM members";
r = stmt.executeQuery(sqlSelectAll);

members.clear();
//Loop through the resultset
while(r.next()){
members.add(new member(r.getInt("membId"), r.getString("name"),
r.getString("email"), r.getString("country"), r.getString("favGenre"),
r.getString("gender"), r.getString("description"), r.getString("memberType"),
r.getString("songLimit"), r.getString("card_no"), r.getString("expiry_date")));
}
conDB.close(); // Close the DB connection

}//End of TRY
catch(Exception er){
System.out.println("Error was: " + er);
}
}
}

以下是我实现 JTable 的方法:

public class ViewAll extends JFrame implements ActionListener{
//Jtextfields, buttons, labels
private JButton btnBack = new JButton("Back");

private static JLabel lblMembTitle = new JLabel("<html><h1>All Members</h1></html>");
private static JLabel lblPlayTitle = new JLabel("<html><h1>All Playlists</h1><br /></html>");
//Containers, Panels, Scrollpanes
private Container mainCon = this.getContentPane();
private static JPanel pnlTable = new JPanel(new BorderLayout());

//Jpanels - sections
private JPanel mainPanel = new JPanel();
private JPanel subPanel1 = new JPanel();
private JPanel subPanel2 = new JPanel();
private JPanel subPanel3 = new JPanel();

//Tables
private static JTable tblShowAllMemb = new JTable();
private static JTable tblShowAllPlay = new JTable();
JScrollPane scrollPane = new JScrollPane(mainPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

public ViewAll(){
super("Search/Edit/Delete Members");
this.setBounds(400, 800, 854,400);
this.setVisible(true);
mainCon.add(scrollPane);

//Table Models:
MemTableModel tblMembers = new MemTableModel();
PlayTableModel tblPlaylist = new PlayTableModel();

//LAYOUT
/*By removing this the scrollpane works
^^mainPanel is already added to the scrollPane object above ^^
*/
// mainCon.add(mainPanel);

//Main Panel
mainPanel.setLayout(new BorderLayout());
mainPanel.add(BorderLayout.NORTH, subPanel1);
mainPanel.add(BorderLayout.CENTER, subPanel2);

//Panel1 - Member table + Back Button
subPanel1.setLayout(new BorderLayout());
subPanel1.add(BorderLayout.NORTH, btnBack);
subPanel1.add(BorderLayout.CENTER, lblMembTitle);
subPanel1.add(BorderLayout.SOUTH, tblShowAllMemb);
tblShowAllMemb.setModel(tblMembers);
btnBack.addActionListener(this);



//Panel2 - Playlist table
subPanel2.add(BorderLayout.NORTH, lblPlayTitle);
subPanel2.add(BorderLayout.CENTER, tblShowAllPlay);
tblShowAllPlay.setModel(tblPlaylist);
}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnBack){
this.dispose();
}
}

}

最佳答案

可能的问题是您没有包装 JTable,它代表 JScrollPane 中的 TableModel,如 How to Use Tables 中所示。

通过简单地使用诸如...之类的东西

add(new JScrollPane(new JTable(new MemTableModel())));

我可以获得:

Example

另请参阅How to Use Scroll Panes了解更多详情

根据更新的代码进行更新...

没有一个表格实际上包含在它自己的 JScrollPane

// By the way, static here is very, very bad idea
private static JTable tblShowAllMemb = new JTable();
private static JTable tblShowAllPlay = new JTable();
JScrollPane scrollPane = new JScrollPane(mainPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

public ViewAll(){
//....

//Table Models:
MemTableModel tblMembers = new MemTableModel();
PlayTableModel tblPlaylist = new PlayTableModel();

//...
subPanel1.add(BorderLayout.SOUTH, tblShowAllMemb);

//...
subPanel2.add(BorderLayout.CENTER, tblShowAllPlay);

您刚刚将表本身添加到其他容器中。相反,考虑使用类似的东西

//...
subPanel1.add(BorderLayout.SOUTH, new JScrollPane(tblShowAllMemb));

//...
subPanel2.add(BorderLayout.CENTER, new JScrollPane(tblShowAllPlay));

关于java - AbstractTabelModel 将不会显示 JTable 列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30818336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com