由于某些原因,我的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);
}
}
}
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();
}
}
}
答案 0 :(得分:2)
可能的问题是,您没有包裹JTable
,TableModel
代表JScrollPane
中的add(new JScrollPane(new JTable(new MemTableModel())));
How to Use Tables
只需使用类似......
JScrollPane
我可以得到:
有关详情,请参阅How to Use Scroll Panes
您的桌子中没有一个实际上包含在其自己的// 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));
您刚刚将表格添加到其他容器中。相反,请考虑使用类似
的内容#include <cctype>