2016-10-16 7 views
0

Ich versuche eine JScrollPane auf meiner JTable erscheinen zu lassen. Ich habe die Tabelle an den Scrollbereich übergeben, als ich eine Instanz der Komponente erstellt habe. Aber es hat sich noch nicht auf meinem Tisch gezeigt.JScrollPane erscheint nicht auf JTable

Ich weiß nicht, wie ich dieses Problem beheben kann, kann ich nicht scheinen, um ein Problem zu finden, die es verursachen würden scheitern. Hier ist der Rest des GUI-Codes. Es ist sehr lang. Das Hinzufügen der jtable zu einem JPanel beginnt bei Zeile 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); 

     } 
    } 

} 

Antwort

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

hier Sie die Tabelle zweimal hinzufügen, befindet sich direkt in der ersten Zeile und (implizit) zusammen mit der ScrollPane in der letzten Zeile.

In Swing ist dies nicht möglich. Daher wird die JTable in der ersten Zeile aus dem Bildlauffeld entfernt, wenn Sie sie direkt dem Feld bottom hinzufügen, und in der letzten Zeile wird ein leeres Bildlauffeld hinzugefügt, das die zuvor hinzugefügte JTable entfernt.

entfernen Sie einfach die erste Zeile.

+0

ok das funktionierte. Ich entfernte den Buttom.add (Tabelle, c) und ersetzte ihn durch den Scrollpane-Zusatz und es funktioniert! –

+0

_ "und ersetzte es durch den Scrollpane-Zusatz" _ Sie hatten es bereits in der letzten Zeile, die ich aus Ihrem Code kopiert habe. –

2

Sie können die Tabelle zweimal nicht hinzufügen:

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); 

Wenn Sie es zum scrollPane hinzufügen, fügen Sie einfach den scrollpane. Eine Komponente kann nicht zwei Eltern haben.

Ich habe überprüfen Sie Ihre vollständigen Code nicht, sondern versuchen,

buttom.add(scrollPane,c); 

anstelle dieses

buttom.add(table, c);     //here 
    buttom.add(scrollPane); 
Verwandte Themen