简体   繁体   English

动态地将jtable添加到jpanel

[英]Dynamically add a jtable to jpanel

I am new to Java. 我是Java新手。

I'm trying to build a CRUD program. 我正在尝试构建一个CRUD程序。 Now, I try to read the data from the database and display them in a jtable . 现在,我尝试从数据库中读取数据并将其显示在jtable So every time I will create a new table and add it to the jpanel . 所以每次我都会创建一个新表并将其添加到jpanel But the program does not show the table when update. 但是程序在更新时不会显示该表。 Can someone help me? 有人能帮我吗?

package com.accsoftware.ui;

import java.sql.Date;
import java.util.List;

import javax.swing.JTable;

import com.accsoftware.common.Table1;
import com.accsoftware.util.DataHandler;


public class SearchAndDisplayPanel extends javax.swing.JPanel {


    private static final long serialVersionUID = -9078860779973523031L;

    public SearchAndDisplayPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        chooseTableButtonGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        date = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        date1 = new com.toedter.calendar.JDateChooser();
        displayPanel = new javax.swing.JPanel();
        generateReport = new javax.swing.JButton();
        Search = new javax.swing.JButton();
        chooseTablePanel = new javax.swing.JPanel();
        table1RadioButton = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();

        jLabel1.setText("Start:");

        jLabel2.setText("End:");

        javax.swing.GroupLayout displayPanelLayout = new javax.swing.GroupLayout(displayPanel);
        displayPanel.setLayout(displayPanelLayout);
        displayPanelLayout.setHorizontalGroup(
            displayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        displayPanelLayout.setVerticalGroup(
            displayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 173, Short.MAX_VALUE)
        );

        generateReport.setText("Generate Report");
        generateReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateReportActionPerformed(evt);
            }
        });

        Search.setText("Search");
        Search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchActionPerformed(evt);
            }
        });

        chooseTableButtonGroup.add(table1RadioButton);
        table1RadioButton.setText("Table1");

        chooseTableButtonGroup.add(jRadioButton2);
        jRadioButton2.setText("Table2");

        javax.swing.GroupLayout chooseTablePanelLayout = new javax.swing.GroupLayout(chooseTablePanel);
        chooseTablePanel.setLayout(chooseTablePanelLayout);
        chooseTablePanelLayout.setHorizontalGroup(
            chooseTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chooseTablePanelLayout.createSequentialGroup()
                .addComponent(table1RadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(jRadioButton2)
                .addContainerGap())
        );
        chooseTablePanelLayout.setVerticalGroup(
            chooseTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chooseTablePanelLayout.createSequentialGroup()
                .addGroup(chooseTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(table1RadioButton)
                    .addComponent(jRadioButton2))
                .addGap(0, 6, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(displayPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(date, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(date1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(chooseTablePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(generateReport)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Search)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(date1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(date, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(displayPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(generateReport)
                        .addComponent(Search, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(chooseTablePanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>

    private void generateReportActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void SearchActionPerformed(java.awt.event.ActionEvent evt) {
        Date start = dateToString(date);
        Date end = dateToString(date1);
        String table = table1RadioButton.isSelected()?"Table1":"Table2";
        List result = DataHandler.readAll(table, start, end);

        int col = table1RadioButton.isSelected()?7:10;

        Object[][] data = new Object[result.size()][col];

        if(table.equalsIgnoreCase("Table1")){
            int id = 0;
            for(Object element:result){
                Table1 table1 = (Table1) element;
                data[id][0] = table1.getDate();
                data[id][1] = table1.getItem();
                data[id][2] = table1.getFee();
                data[id][3] = table1.getGst();
                data[id][4] = table1.getVehicle();
                data[id][5] = table1.getWages();
                data[id][6] = table1.getStationary();
                id++;           

            }
            String[] columnNames = { "Date", "Reference/Item", "Cost", "GST",
                    "Vehicle", "Wages", "Stationary" };

            JTable displayTable = new JTable(data, columnNames);
            this.displayPanel.add(displayTable);
            this.displayPanel.revalidate();
            this.displayPanel.repaint();            
        }
    }

    @SuppressWarnings("deprecation")
    private Date dateToString(com.toedter.calendar.JDateChooser date){
        return new Date(date.getDate().getYear(),
                date.getDate().getMonth(), date.getDate().getDay());
    } 
    // Variables declaration - do not modify
    private javax.swing.JButton Search;
    private javax.swing.ButtonGroup chooseTableButtonGroup;
    private javax.swing.JPanel chooseTablePanel;
    private com.toedter.calendar.JDateChooser date;
    private com.toedter.calendar.JDateChooser date1;
    private javax.swing.JPanel displayPanel;
    private javax.swing.JButton generateReport;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JRadioButton table1RadioButton;
    private javax.swing.JRadioButton jRadioButton2;
    // End of variables declaration
}

You're using GroupLayout which requires horizontal and vertical sequential groups when adding components. 您正在使用GroupLayout ,在添加组件时需要水平和垂直顺序组。 This type of work is best left to UI builders. 这类工作最好留给UI构建者。

Create a single JTable when the application is initialized rather than adding multiple tables. 在初始化应用程序时创建单个JTable ,而不是添加多个表。 Update the TableModel when data retrieval is required. 需要数据检索时更新TableModel Use the mutable DefaultTableModel . 使用mutable DefaultTableModel

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

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