繁体   English   中英

JScrollPane不显示其内容(JTable)

[英]JScrollPane does not show its content (JTable)

我正在研究IT管理器工具之类的东西,用于基于MySQL数据库存储有关pc,笔记本电脑,服务器等的数据。

我的问题:
JScrollPane不显示JTable。 我已经尝试添加JButton了,但是也没有显示出来。 已经在Google上搜索并发现了与table.revalidate()有关的一些想法,但到目前为止没有任何帮助。

这是我的代码:

类“ 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();
        }
    }
}

类“ ComputerList”:注意:String values [] []用于测试目的,但是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);
    }
}

对于那些同样有问题的人,我现在发现由于“ scrollPane.removeAll()”而未显示该表,因为这似乎从滚动窗格中删除了显示该表所需的内容。 现在要检查一下。 问题可以结束。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM