JScrollPane没有在JTable上显示

时间:2016-10-16 21:11:39

标签: java swing jtable

我正在尝试让JScrollPane出现在我的JTable上。当我创建组件的实例时,我将表传递给了滚动窗格。但无济于事还没有显示在我的桌子上。

 table = new JTable();
 scrollPane = new JScrollPane(table);
 table.setPreferredSize(new Dimension(200,100));

我不知道如何解决这个问题,我似乎无法找到导致它失败的问题。这是GUI代码的其余部分。这很长。将jtable添加到jpanel从第152行开始。

  /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javasql;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.ActionEvent;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author KJ4CC
 */
public class UserInterface implements ActionListener {

    DefaultTableModel dtm = new DefaultTableModel(0, 0);

    public UserInterface() {
        startGui();
    }
    JFrame frame = new JFrame();
    Javasql sql = new Javasql();
    JPanel buttom = new JPanel(new GridBagLayout());
    JPanel commandPane = new JPanel(new GridBagLayout());
    JPanel top = new JPanel(new GridBagLayout());
    JPanel buttons = new JPanel(new GridBagLayout());
    JPanel label = new JPanel(new GridBagLayout());

    JButton connect = new JButton("Connect To Database");
    JButton clr = new JButton("Clear Command");
    JButton exeSql = new JButton("Execute SQL Command");
    JButton clrRes = new JButton("Clear Result Window");

    JLabel infoLabel = new JLabel("Enter Database Information ");
    JLabel driverLabel = new JLabel("JDBC Driver: ");
    JLabel dbLabel = new JLabel("Database URL: ");
    JLabel userLabel = new JLabel("Username:  ");
    JLabel passLabel = new JLabel("Password: ");
    JLabel sqlLabel = new JLabel("Enter SQL Command: ");
    JLabel connectionLabel = new JLabel("No Connection Now ");
    JLabel exeLabel = new JLabel("SQL Execution Result: ");
    //creating an instance of the new table
    public JTable table;
    public JScrollPane scrollPane;

    JComboBox driverSelect = new JComboBox();
    JComboBox url = new JComboBox();

    JTextField username = new JTextField();
    JTextField pass = new JTextField();
    JTextArea command = new JTextArea(1, 1);

    public void startGui() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagConstraints c = new GridBagConstraints();
        System.out.println("sdf");
        c.insets = new Insets(0, 0, 0, 10);
        c.fill = 0;
        c.weightx = 1;
        //adding all of the compoenets to their panel and then to the frame.
        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        top.add(infoLabel, c);
        c.gridx = 0;
        c.gridy = 1;
        top.add(driverLabel, c);
        c.gridx = 1;
        c.gridy = 1;
        c.ipadx = 150;

        c.fill = GridBagConstraints.HORIZONTAL;
        top.add(driverSelect, c);
        c.gridx = 0;
        c.gridy = 2;
        c.fill = 0;

        top.add(dbLabel, c);
        c.gridx = 1;
        c.gridy = 2;
        c.fill = GridBagConstraints.HORIZONTAL;
        top.add(url, c);
        c.gridx = 0;
        c.gridy = 3;
        c.fill = 0;
        c.fill = 0;
        top.add(userLabel, c);
        c.gridx = 1;
        c.gridy = 3;
        c.fill = GridBagConstraints.HORIZONTAL;
        top.add(username, c);
        c.gridx = 0;
        c.gridy = 4;
        c.fill = 0;
        c.fill = 0;
        top.add(passLabel, c);
        c.gridx = 1;
        c.gridy = 4;
        c.fill = GridBagConstraints.HORIZONTAL;
        top.add(pass, c);
        //add the driver and url to the comboboxes
        c.gridx = 2;
        c.gridy = 0;
        commandPane.add(sqlLabel, c);
        c.gridx = 2;
        c.gridy = 1;
        c.ipadx = 150;
        c.ipady = 75;       //sql text area for command 
        c.fill = GridBagConstraints.FIRST_LINE_END;
        c.fill = GridBagConstraints.HORIZONTAL;
        commandPane.add(command, c);

        c.insets = new Insets(0, 0, 0, 20);

        c.ipadx = 9;
        c.ipady = 1;
        c.gridx = 0;
        c.gridy = 0;
        //buttons
        label.add(connectionLabel, c);
        c.gridx = 1;
        c.gridy = 0;
        //c.insets = new Insets(0, 0, 0, 50);

        buttons.add(connect, c);
        connect.addActionListener(this);
        c.gridx = 2;
        buttons.add(clr, c);
        clr.addActionListener(this);
        c.gridx = 3;
        buttons.add(exeSql, c);
        exeSql.addActionListener(this);
        //adding the label and buttons above and below the tabel. 
        c.gridx = 0;
        c.gridy = 1;
        buttom.add(exeLabel, c);
        c.gridx = 0;
        c.gridy = 2;
        //-----------------------------------------------------------------Table here 
        table = new JTable();
        scrollPane = new JScrollPane(table);
        table.setPreferredSize(new Dimension(200, 100));
        c.fill = GridBagConstraints.HORIZONTAL;
        buttom.add(table, c);
        buttom.add(scrollPane);
        c.gridx = 0;
        c.gridy = 3;
        buttom.add(clrRes, c);

        c.weightx = 2;
        c.weighty = 2;
        c.gridx = 0;
        c.gridy = 0;
        frame.setLayout(new GridLayout(3, 2));
        frame.add(top);
        c.gridx = 2;
        c.gridy = 1;
        frame.add(commandPane);
        frame.add(label);
        frame.add(buttons);

        frame.add(buttom, BorderLayout.SOUTH);

        //adding the content panel to the jframe. 
        frame.pack();
        frame.setSize(1000, 550);
        frame.setVisible(true);
        //adding items to both of the combo boxes. 
        driverSelect.addItem("com.mysql.jdbc.Driver");
        url.addItem("jdbc:mysql://localhost:3306/project3");

    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == connect) {

            sql.connect(this);
        } else if (e.getSource() == clr) {
            command.setText("");

        } else if (e.getSource() == exeSql) {
            sql.exeCommand(this);

        }
    }

}

2 个答案:

答案 0 :(得分:2)

    scrollPane = new JScrollPane(table);
    table.setPreferredSize(new Dimension(200, 100));
    c.fill = GridBagConstraints.HORIZONTAL;
    buttom.add(table, c);
    buttom.add(scrollPane);

这里你把表添加两次,直接在第一行和(隐式)以及最后一行的ScrollPane。

在Swing中,这是不可能的。因此,当您将JTable直接添加到bottom面板时,将从第一行的Scrollpane中删除JTable,然后在最后一行添加空滚动窗格,删除之前添加的JTable。

只需删除第一行。

答案 1 :(得分:2)

您无法将表格添加两次:

    table = new JTable();
    scrollPane = new JScrollPane(table);   //here
    table.setPreferredSize(new Dimension(200, 100));
    c.fill = GridBagConstraints.HORIZONTAL;
    buttom.add(table, c);                  //here
    buttom.add(scrollPane);

如果将其添加到scrollPane,只需添加滚动窗格。组件不能有两个父母。

我没有检查您的完整代码,但请尝试

    buttom.add(scrollPane,c);

而不是

    buttom.add(table, c);                  //here
    buttom.add(scrollPane);
相关问题