The scrollbar and the button will be painted but not the cells in the table,and the console has printed the data correctly so I am wondering where I have done wrong.
public class MyGUIManager extends JPanel {
protected JFrame frame;
protected JScrollPane scrollPane=null;
protected JTable table=null;
protected TableModel model=null;
protected JButton btnInsert=null;
protected String dbname,tblname;
protected Vector<String> colNames;
protected Vector<Vector<String>> data,backup;
protected int col;
protected MySQLGUIManager() throws Exception{
frame =new JFrame("MySQL Manager");
frame.setContentPane(this);
frame.setMinimumSize(new Dimension(800,600));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
init();
}
protected void init() throws Exception{
//some inrrelevant operations about database
loadTbl();
}
protected void loadTbl() throws SQLException{
table=new JTable(data,colNames){
private static final long serialVersionUID = 1L;
public void tableChanged(TableModelEvent e){
int column=e.getColumn();
assert e.getFirstRow()==e.getLastRow():"more than 1 row have been changed!";
int row=e.getFirstRow();
if(scrollPane!=null){
//some codes
}
}
};
model=table.getModel();
scrollPane=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(scrollPane,BorderLayout.CENTER);
btnInsert=new JButton("Insert a row");
add(btnInsert,BorderLayout.SOUTH);
repaint();
revalidate();
// scrollPane.repaint();
// scrollPane.revalidate();
System.out.println(model.getColumnCount()+" "+model.getRowCount());
for(int i=0;i<data.size();i++){
for(int j=0;j<data.get(0).size();j++){
System.out.print(model.getValueAt(i, j)+" ");
}
System.out.println();
}
}
public static void main(String[] args) throws Exception {
new MySQLGUIManager();
}
}
(Some codes are omitted but they have nothing to do with GUI.)
Thanks in advance!
JFramefrom within aJPanelonly to add itself to the frame is bad practiceYou’re not call
super.tableChangedfrom within youtableChangedmethod