我正在开发类似于IT管理器工具的东西,用于存储基于MySQL数据库的pc,笔记本,服务器等数据。
我的问题:
JScrollPane不显示JTable。
我已经尝试过添加JButton,这也没有显示出来。
已经谷歌搜索并找到了table.revalidate()的一些想法,但到目前为止没有任何帮助。
这是我的代码:
class" Gui":
package ManageIT;
import java.awt.BorderLayout;
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.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
/**
* @author a.struck
*
*/
public class Gui extends JFrame {
private static final long serialVersionUID = 1287952773890821788L;
private static DatabaseConnection dbSettings = new DatabaseConnection();
public static Gui main;
private ComputerList computerList;
private JScrollPane scrollPane = new JScrollPane();
private String ipAddress;
private String port;
private String database;
private String user;
private String password;
public static void main(String[] args) {
main = new Gui();
dbSettings.loadConnection();
}
/**
* initialize Gui
*/
public Gui() {
// initialize gui and its behavior
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// menu bar
JMenuBar menuBar = new JMenuBar();
getContentPane().add(menuBar, BorderLayout.NORTH);
// computer list
JMenuItem computerMenuItem = new JMenuItem("Computers");
computerMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent clickedAbout) {
// create new computerList
computerList = new ComputerList();
computerList.initializeMain(main);
loadDatabase(dbSettings.getDatabaseConnection(), "computers");
}
});
menuBar.add(computerMenuItem);
// database settings
JMenuItem mItemDatabase = new JMenuItem("Connect Database");
mItemDatabase.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
dbSettings.loadConnection();
dbSettings.initializeDatabaseSettingsGui();
}
});
menuBar.add(mItemDatabase);
// initialize scroll pane where the tables will be placed on
getContentPane().add(scrollPane, BorderLayout.CENTER);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setSize(1600, 768);
setVisible(true);
}
/**
* Returns scrollPane, where the computerList is placed on
*
* @return JScrollPane
*/
public JScrollPane getScrollPane() {
return scrollPane;
}
public void loadDatabase(String[] connection, String function) {
ipAddress = connection[0];
port = connection[1];
database = connection[2];
user = connection[3];
password = connection[4];
try {
// load driver
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
} catch (Exception e) {
System.err.println("Unable to load driver.");
e.printStackTrace();
}
try {
// open connection
Connection conn = DriverManager.getConnection("jdbc:mysql://" + ipAddress + ":" + port + "/" + database
+ "?" + "user=" + user + "&" + "password=" + password);
// create statement
Statement stmt = conn.createStatement();
if (function == "computers") {
// create querie
String sqlCommand = "SELECT internal_id, user, state, product_name, serial_number, operating_system, harddrive, ram, cpu, date_purchased, date_installed, ip_address, network_plug, teamviewer_id, notes FROM computer";
ResultSet rs = stmt.executeQuery(sqlCommand);
computerList.initializeComputers(rs);
}
// close statement
stmt.close();
// close connection
conn.close();
} catch (SQLException sqle) {
System.out.println("SQLException: " + sqle.getMessage());
System.out.println("SQLState: " + sqle.getSQLState());
System.out.println("VendorError: " + sqle.getErrorCode());
sqle.printStackTrace();
}
}
}
class" ComputerList": 注意:字符串值[] []用于测试目的,但MySQL连接正常。
package ManageIT;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
* @author a.struck
*
*/
public class ComputerList {
public static Gui main;
/**
* @param args
*/
public ComputerList() {
}
public void initializeMain(Gui x) {
main = x;
}
/**
* Creates a new table
*
*/
public void initializeComputers(ResultSet rs) {
// remove all components of the panel
main.getScrollPane().removeAll();
// create table
String header[] = { "Internal ID", "User", "State", "Product Name", "Serial Number", "Operating System",
"Harddrive", "RAM", "CPU", "Date Purchased", "Date Installed", "IP Address", "Network Plug",
"Teamviewer ID", "Notes" };
String values[][] = { { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" },
{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" } };
JTable table = new JTable(values, header);
DefaultTableModel model = new DefaultTableModel(new String[] { "Internal ID", "User", "State", "Product Name",
"Serial Number", "Operating System", "Harddrive", "RAM", "CPU", "Date Purchased", "Date Installed",
"IP Address", "Network Plug", "Teamviewer ID", "Notes" }, 0);
try {
while (rs.next()) {
model.addRow(new Object[] { rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4),
rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9),
rs.getString(10), rs.getString(11), rs.getString(12), rs.getString(13), rs.getString(14),
rs.getString(15) });
}
table.setModel(model);
} catch (SQLException sqle) {
System.out.println("SQLException: " + sqle.getMessage());
System.out.println("SQLState: " + sqle.getSQLState());
System.out.println("VendorError: " + sqle.getErrorCode());
sqle.printStackTrace();
}
main.getScrollPane().setViewportView(table);
main.setVisible(true);
}
}
答案 0 :(得分:0)
对于那些遇到同样问题的人,我现在发现该表未显示,因为" scrollPane.removeAll()"因为这似乎从滚动窗格中删除了显示表所需的内容。现在去检查一下。问题可以结束。