2016-10-16 2 views
0

Я пытаюсь получить JScrollPane для отображения на моем JTable. Я передал таблицу в scrollpane, когда создал экземпляр компонента. Но пока это еще не показалось на моем столе.JScrollPane не отображается на JTable

Я не знаю, как я могу решить эту проблему, я не могу показаться, чтобы найти проблему, которая бы привести к сбою. Вот остальная часть кода 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
scrollPane = new JScrollPane(table); 
    table.setPreferredSize(new Dimension(200, 100)); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    buttom.add(table, c); 
    buttom.add(scrollPane); 

здесь вы добавляете таблицу дважды, прямо в первой строке и (неявно) вместе с ScrollPane в последней строке.

В Swing это невозможно. Поэтому JTable удаляется из Scrollpane в первой строке, когда вы добавляете его непосредственно на панель bottom, а в свою очередь на последней строке добавляется пустая панель прокрутки, удаляемая ранее JTable.

просто удалите первую строку.

+0

ок это работало. Я удалил buttom.add (таблица, c) и заменил его добавлением scrollpane, и он работает! –

+0

_ "и заменил его добавлением scrollpane" _ у вас уже было это в последней строке, которую я скопировал из вашего кода. –

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, просто добавьте ScrollPane. Компонент не может иметь двух родителей.

я не проверял свой полный код, но попробовать

buttom.add(scrollPane,c); 

вместо этого

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

 Смежные вопросы

  • Нет связанных вопросов^_^