简体   繁体   中英

JScrollPane not displaying

I'm trying to display a JscrollPane using this code. but it's displaying a blank frame with just the "close" button displayed. Can't figure out why it wouldn't display. Any help would be greatly appreciated! :)

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import edu.pitt.bank.Account;
import edu.pitt.bank.Transaction;
import edu.pitt.utilities.DbUtilities;
import edu.pitt.utilities.MySqlUtilities;

public class TransactionUI {
    private JFrame  frame;
    private JScrollPane transactionPane;
    private JTable tblTransactions;

    public TransactionUI(Account userAccount) {
        frame = new JFrame();
        frame.setTitle("Account Transactions");
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        transactionPane = new JScrollPane();
        frame.getContentPane().add(transactionPane);
        DbUtilities db = new MySqlUtilities();
        String [] cols = {"Type", "Amount", "Date"};
        String sql = "SELECT type, amount, transactionDate FROM srp63_bank1017.transaction;";

        try {
            System.out.println("use getDataTable()");
            DefaultTableModel transactionList = db.getDataTable(sql, cols);
            System.out.println("getDataTable() used");
            tblTransactions = new JTable(transactionList);
            tblTransactions.setFillsViewportHeight(true);
            tblTransactions.setShowGrid(true);
            tblTransactions.setGridColor(Color.BLACK);
            transactionPane.getViewport().add(tblTransactions);
        } catch (SQLException e) {
        e.printStackTrace();
    }       
        JButton btnClose = new JButton("Close");
        btnClose.setBounds(323, 212, 89, 23);           
        btnClose.setBounds(284, 214, 73, 23);
        frame.getContentPane().add(btnClose);
    }

    public JFrame getFrame() {
        return frame;
    } 
}

I use this to call the above frame from another class:

public void actionPerformed(ActionEvent arg0) { 
                if(userAccount.getAccountID() != null){
                    TransactionUI tUI = new TransactionUI(userAccount);
                    tUI.getFrame().setVisible(true);
                } else {
                    System.out.println("Account object must not be null");
                }
            }       
        });

Here is the getDataTable method...

public DefaultTableModel getDataTable(String sqlQuery, String[] customColumnNames) throws SQLException{
        ResultSet rs = getResultSet(sqlQuery);
        /* Metadata object contains additional information about a ResulSet, 
         * such as database column names, data types, etc...
         */
        ResultSetMetaData metaData = rs.getMetaData();

        // Get column names from the metadata object and store them in a Vector variable
        Vector<String> columnNames = new Vector<String>();
        for(int column = 0; column < customColumnNames.length; column++){
            columnNames.add(customColumnNames[column]);
        }

        // Create a nested Vector containing an entire table from the ResultSet
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while(rs.next()){
            Vector<Object> vector = new Vector<Object>();
            for(int columnIndex = 1; columnIndex <= metaData.getColumnCount(); columnIndex++){
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }
        return new DefaultTableModel(data, columnNames);
    }

I received no errors

Problem #1

frame.getContentPane().setLayout(null);

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

See Laying Out Components Within a Container for more details

Problem #2

transactionPane.getViewport().add(tblTransactions);

Don't use ad with JScrollPane or JViewport , use

transactionPane.getViewport().setView(tblTransactions);   

or

transactionPane.setViewportView(tblTransactions);   

instead

See How to Use Scroll Panes for more details

滚动窗格

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JTable table = new JTable(new DefaultTableModel(100, 100));
                table.setGridColor(Color.LIGHT_GRAY);
                JScrollPane scrollPane = new JScrollPane();
                scrollPane.setViewportView(table);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(scrollPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

Problem #3

The use of multiple windows, see The Use of Multiple JFrames, Good/Bad Practice? for a more in-depth discussion

I think what you really want is some kind of modal dialog. See How to Make Dialogs for more details

Your code without modifications

(Except removing the database code)

在此处输入图片说明

This is how your code looks on my PC, take a good hard look at the button...

Your code modified to use layout managers...

布局经理

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                TransactionUI ui = new TransactionUI();
                JFrame frame = ui.getFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TransactionUI {

        private JFrame frame;
        private JScrollPane transactionPane;
        private JTable tblTransactions;

        public TransactionUI() {
            frame = new JFrame();
            frame.setTitle("Account Transactions");
            frame.setBounds(100, 100, 450, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(new BorderLayout());

            transactionPane = new JScrollPane();
            frame.getContentPane().add(transactionPane);
            String[] cols = {"Type", "Amount", "Date"};
            String sql = "SELECT type, amount, transactionDate FROM srp63_bank1017.transaction;";

            System.out.println("use getDataTable()");
            DefaultTableModel transactionList = new DefaultTableModel(100, 100);
            System.out.println("getDataTable() used");
            tblTransactions = new JTable(transactionList);
            tblTransactions.setFillsViewportHeight(true);
            tblTransactions.setShowGrid(true);
            tblTransactions.setGridColor(Color.BLACK);
            transactionPane.setViewportView(tblTransactions);

            JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            JButton btnClose = new JButton("Close");
            buttons.add(btnClose);
            frame.getContentPane().add(buttons, BorderLayout.SOUTH);
        }

        public JFrame getFrame() {
            return frame;
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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