当用户按下按钮时,我想从我的数据库中填充JTable。目前我没有得到任何错误,我已经确定DefaultTableModel正在返回到按钮的Action Event并且它包含数据。我无法让JTable显示数据。我认为这与我声明JTable和GroupLayout addComponent函数的位置有关,但我无法弄明白。
这是代码。我试图将GroupLayout包括在内,因为我对此表示怀疑 - 我已经尽可能地删除了这个问题。我保证这不是代码转储:
package Console;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JEditorPane;
import javax.swing.JTable;
public class Console {
String myquery="";
private JFrame frame;
private JTextField textField;
JTable table_ResQues;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Console window = new Console();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Console() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 950, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 838, Short.MAX_VALUE)
.addContainerGap())
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 872, Short.MAX_VALUE)
.addContainerGap())
);
JPanel panel_Question = new JPanel();
tabbedPane.addTab("Question", null, panel_Question, null);
JTextArea textAreaQuery = new JTextArea();
JButton btnNewButton = new JButton("Execute");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e )
{
//Execute when button is pressed
String connectDB = "jdbc:ucanaccess:///Users/sebastianzeki/Documents/PhysJava/Physiology.mdb;";
System.out.println("Connection To Database Made");
Connection conn = null;
try {
conn = DriverManager.getConnection(connectDB);
} catch (SQLException e1) {
e1.printStackTrace();
}
Statement st = null;
try {
st = conn.createStatement();
} catch (SQLException e1) {
e1.printStackTrace();
}
myquery=textAreaQuery.getText();
String stg2 = "Select "+myquery;
System.out.println(stg2);
try {
ResultSet rs = st.executeQuery(stg2);
buildTableModel(rs).fireTableDataChanged();
table_ResQues.setModel(buildTableModel(rs));
} catch (SQLException e1) {
e1.printStackTrace();
}
}
});
JButton btnExportToCsv = new JButton("Export to CSV");
GroupLayout gl_panel_Question = new GroupLayout(panel_Question);
gl_panel_Question.setHorizontalGroup(
gl_panel_Question.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_Question.createSequentialGroup()
.addGap(41)
.addGroup(gl_panel_Question.createParallelGroup(Alignment.LEADING)
.addComponent(btnExportToCsv)
.addGroup(gl_panel_Question.createSequentialGroup()
.addGroup(gl_panel_Question.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_Question.createSequentialGroup()
.addComponent(textAreaQuery, GroupLayout.PREFERRED_SIZE, 346, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addGroup(gl_panel_Question.createParallelGroup(Alignment.LEADING)
.addComponent(btnNewButton)))
.addComponent(table_ResQues, GroupLayout.PREFERRED_SIZE, 349, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_panel_Question.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_panel_Question.createParallelGroup(Alignment.LEADING)
))
.addGap(18)
.addGroup(gl_panel_Question.createParallelGroup(Alignment.LEADING))))
.addContainerGap(147, Short.MAX_VALUE))
);
gl_panel_Question.setVerticalGroup(
gl_panel_Question.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_Question.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel_Question.createParallelGroup(Alignment.LEADING)
.addComponent(textAreaQuery, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
.addGroup(gl_panel_Question.createSequentialGroup()
.addComponent(btnNewButton)
.addGap(12)
.addComponent(btnPostbox)))
.addGroup(gl_panel_Question.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_Question.createSequentialGroup()
.addGap(58)
.addGroup(gl_panel_Question.createParallelGroup(Alignment.TRAILING, false)
.addGroup(gl_panel_Question.createSequentialGroup()
.addGroup(gl_panel_Question.createParallelGroup(Alignment.BASELINE)
.addComponent(button)
.addComponent(btnNewButton_8))
)
.addGroup(gl_panel_Question.createSequentialGroup()
)))
.addGroup(gl_panel_Question.createSequentialGroup()
.addGap(18)
.addComponent(table_ResQues, GroupLayout.PREFERRED_SIZE, 190, GroupLayout.PREFERRED_SIZE)))
.addGap(28)
.addComponent(btnExportToCsv)
.addContainerGap(226, Short.MAX_VALUE))
);
panel_Question.setLayout(gl_panel_Question);
}
以及填充模型的代码
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
System.out.println(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}