滚动条和按钮将被绘制,但不会绘制表格中的单元格,并且控制台已正确打印数据,因此我想知道我在哪里做错了。
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();
}
}
(省略了一些代码,但它们与GUI无关。) 提前谢谢!
答案 0 :(得分:1)
我没有在代码中看到您将表实际添加到任何UI现有容器的任何位置。
此外,在调用pack()之前,创建表并将其添加到其预期的父组件可能是个好主意。
您可能想要考虑将表的创建与加载数据分开。
此外,省略的“某些代码”可能会对表的行为产生非常大的影响,因此请小心。
答案 1 :(得分:1)
好的......这是一个如何使用DefaultTableModel
模型填充表格的示例,因为您的伪SSCCE无用:
package com.model.impl;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class FillTable extends JFrame{
JPanel panel = new JPanel();
JLabel lblName = new JLabel("Name: ");
JLabel lblIp = new JLabel("Ip: ");
JTextField txtName = new JTextField(10);
JTextField txtIp = new JTextField(10);
JButton btnAdd = new JButton("Add");
JTable table = new JTable();
DefaultTableModel model = new DefaultTableModel(new Object[][]{}, new String[]{"Name","Ip"});
public FillTable(){
super("Test table");
setLayout(new BorderLayout());
add(panel,BorderLayout.NORTH);
panel.add(lblName);
panel.add(txtName);
panel.add(lblIp);
panel.add(txtIp);
panel.add(btnAdd);
table.setModel(model);
add(new JScrollPane(table),BorderLayout.CENTER);
btnAdd.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
String name = txtName.getText();
String ip = txtIp.getText();
model.addRow(new Object[]{name,ip});
}});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
FillTable ft = new FillTable();
ft.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
ft.setLocationByPlatform(true);
ft.pack();
ft.setVisible(true);
}});
}
}
我希望它会有所帮助。祝你好运!
答案 2 :(得分:1)
JFrame
内创建JPanel
只是为了将自己添加到框架中是不好的做法您未从super.tableChanged
方法
tableChanged
table = new JTable(data, colNames) {
private static final long serialVersionUID = 1L;
public void tableChanged(TableModelEvent e) {
super.tableChanged(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
}
}
};