再次返回卡后如何修复JTable不显示任何数据

时间:2019-04-30 23:12:32

标签: java jframe jtable jpanel actionevent

我在一个主面板(cardlayout)中有多个面板。在一个面板中,我有一个JTable,它根据来自搜索字段的条目显示sql数据库中的数据。当我第一次进入“搜索面板”时,单击“搜索”按钮,它可以正常工作,但是当我单击“后退”按钮,然后(在同一运行的应用程序中)再次回到搜索面板,在字段中输入一些数据,然后单击“搜索”按钮,注意到发生了;只有字段会重置(如他们预期的那样),但表不会显示。

我希望您能理解情况,因为我会尽力将其弄清楚,并非常感谢您的帮助。

这是Main.java的代码:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.CardLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;

import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;

import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.event.ActionEvent;

public class Main extends JFrame implements ActionListener {

static Main window = new Main();
private JPanel homePanel;
private JPanel addPanel;
private JPanel searchPanel;
private JButton addPageBtn, searchPageBtn, felonPageBtn, civilPageBtn, conservativePageBtn, laborPageBtn, backBtn, searchBtn;
private JLabel headImg;
private JLabel credits;

//SEARCH PANEL VARIABLES START
private JTextField nameTxt;
private JTextField fileNumTxt;
private JTextField cprTxt;
private JComboBox caseTypeTxt;
//SEARCH PANEL VARIABLES END

ImageIcon imgIcon = new ImageIcon("img\\saad-alshamlan-logo2.png");
Image img = imgIcon.getImage();

JPanel mainPanel = new JPanel(new CardLayout());
CardLayout mainLayout = (CardLayout)mainPanel.getLayout();

private JTable table = new JTable();
private DefaultTableModel model = (DefaultTableModel)table.getModel();
String column[] = {"الاسم", "الرقم الشخصي", "رقم الملف", "نوع الدعوة", "نوع القضية"};

Connection connection = sqliteConnection.dbConnector();
ResultSet rs, rs1;
Statement smt, smt1;
PreparedStatement pst;

static int current = 0;     //This static integer created to keep track of current panel/page

public Main() {
    getContentPane().setLayout(null);

    homePanel = new JPanel();
    addPanel = new JPanel();
    searchPanel = new JPanel();

    addPageBtn = new JButton();
    searchPageBtn = new JButton();
    felonPageBtn = new JButton();
    civilPageBtn = new JButton();
    conservativePageBtn = new JButton();
    laborPageBtn = new JButton();
    backBtn = new JButton();
    searchBtn = new JButton("ابحث");

    addPageBtn.addActionListener(this);
    searchPageBtn.addActionListener(this);
    felonPageBtn.addActionListener(this);
    civilPageBtn.addActionListener(this);
    conservativePageBtn.addActionListener(this);
    laborPageBtn.addActionListener(this);
    backBtn.addActionListener(this);
    searchBtn.addActionListener(this);



    headImg = new JLabel();
    credits = new JLabel("Powered by NineNine Soft Tech");

    ///////THIS IS THE MAIN PANEL THAT CONTAINS ALL SUB PANELS/ CARDS
    mainPanel.setBounds(0, 0, 800, 600);
    mainPanel.add(homePanel, "homePage");
    mainPanel.add(addPanel, "addPage");
    mainPanel.add(searchPanel, "searchPage");


    getContentPane().add(mainPanel);

    HomePage();
}

public void DisplayData()
{
    String txtName;
    int txtFileNum;
    int txtCpr;
    String txtCase;
    try
    {
        txtName = nameTxt.getText();
        txtFileNum = Integer.parseInt(fileNumTxt.getText());
        txtCpr = Integer.parseInt(cprTxt.getText());
        txtCase = caseTypeTxt.getSelectedItem().toString();
    }
    catch (NumberFormatException m)
    {
        txtFileNum = 0;
        txtCpr = 0;
        txtName = "";
        txtCase = "";
        //fileNumTxt.setText("0");
        //cprTxt.setText("0");
    }

    try
    {
        String query = "SELECT * FROM client WHERE name LIKE '"+txtName+"' OR cpr LIKE '"+txtCpr+"' OR filenumber LIKE '"+txtFileNum+"' OR mctype LIKE '"+txtCase+"';";
        pst = connection.prepareStatement(query);
        rs = pst.executeQuery();
        //if (searchDone == false)
        //{
            //int i = 0;
            while (rs.next())                   
            {   ////////ADD DATA TO THE TABLE BASED ON THE SELECTED FROM THE SQL QUERY ABOVE
                String name = rs.getString("name");
                int cpr = rs.getInt("cpr");
                int fileNum = rs.getInt("filenumber");
                String mC = rs.getString("mctype");
                String sC = rs.getString("sctype");
                //table.setModel(DbUtils.resultSetToTableModel(rs));
                model.setColumnIdentifiers(column);
                model.addRow(new Object[] {name, cpr, fileNum, mC, sC});
                table.setModel(model);
            }
        //  searchDone = true;
        //}
    }
    catch (Exception e)
    {
        JOptionPane.showMessageDialog(null, "Unable to load data: ");
    }

    DefaultTableCellRenderer cR = new DefaultTableCellRenderer();
    cR.setHorizontalAlignment(JLabel.CENTER);
    try
    {   ////CENTER ALL TEXT CELLS INSIDE THE TABLE (EXCLUDING THE TABLE HEADER)
        table.getColumnModel().getColumn(0).setCellRenderer(cR);
        table.getColumnModel().getColumn(1).setCellRenderer(cR);
        table.getColumnModel().getColumn(2).setCellRenderer(cR);
        table.getColumnModel().getColumn(3).setCellRenderer(cR);
        table.getColumnModel().getColumn(4).setCellRenderer(cR);
    }
    catch (ArrayIndexOutOfBoundsException e)
    {
        JOptionPane.showMessageDialog(null, "لا يمكن البحث بدون إدخال أي بيانات");
    }
}

public void HomePage()
{
    current = 1;
    homePanel.setLayout(null);
    homePanel.setBounds(0, 0, 800, 600);
    searchPageBtn.setBounds(200, 350, 150, 50);
    searchPageBtn.setText("أبحث");
    searchPageBtn.setFont(new Font("Arial", Font.BOLD, 24));
    searchPageBtn.setBorder(new RoundedBorder(20));
    homePanel.add(searchPageBtn);
    addPageBtn.setBounds(400, 350, 150, 50);
    addPageBtn.setText("إضافة");
    addPageBtn.setFont(new Font("Arial", Font.BOLD, 24));
    addPageBtn.setBorder(new RoundedBorder(20));
    homePanel.add(addPageBtn);
    Image newImg = img.getScaledInstance(230, 208, java.awt.Image.SCALE_SMOOTH);
    imgIcon = new ImageIcon(newImg);
    headImg.setIcon(imgIcon);
    headImg.setBounds(250, 100, 250, 250);
    homePanel.add(headImg);
    credits.setBounds(250, 500, 300, 50);
    credits.setFont(new Font("Arial", Font.PLAIN, 18));
    homePanel.add(credits);
}

public void AddPage()
{
    current = 2;
    addPanel.setLayout(null);
    addPanel.setBounds(0, 0, 800, 600);
    felonPageBtn.setBounds(50, 300, 150, 50);
    felonPageBtn.setText("جنائي");
    felonPageBtn.setFont(new Font("Arial", Font.BOLD, 24));
    felonPageBtn.setBorder(new RoundedBorder(20));
    addPanel.add(felonPageBtn);
    civilPageBtn.setBounds(230, 300, 150, 50);
    civilPageBtn.setText("مدني");
    civilPageBtn.setFont(new Font("Arial", Font.BOLD, 24));
    civilPageBtn.setBorder(new RoundedBorder(20));
    addPanel.add(civilPageBtn);
    conservativePageBtn.setBounds(410, 300, 150, 50);
    conservativePageBtn.setText("شرعي");
    conservativePageBtn.setFont(new Font("Arial", Font.BOLD, 24));
    conservativePageBtn.setBorder(new RoundedBorder(20));
    addPanel.add(conservativePageBtn);
    laborPageBtn.setBounds(590, 300, 150, 50);
    laborPageBtn.setText("عمالي");
    laborPageBtn.setFont(new Font("Arial", Font.BOLD, 24));
    laborPageBtn.setBorder(new RoundedBorder(20));
    addPanel.add(laborPageBtn);
    headImg.setBounds(250, 50, 250, 250);
    addPanel.add(headImg);
    backBtn.setBounds(600, 50, 100, 50);
    backBtn.setText("رجوع");
    backBtn.setFont(new Font("Arial", Font.BOLD, 24));
    backBtn.setBorder(new RoundedBorder(20));
    addPanel.add(backBtn);
    credits.setBounds(250, 500, 300, 50);
    credits.setFont(new Font("Arial", Font.PLAIN, 18));
    addPanel.add(credits);
}

public void SearchPage()
{
    current = 3;
    searchPanel.setLayout(null);
    searchPanel.setBounds(0, 0, 800, 600);
    JLabel title = new JLabel("البحث");
    title.setFont(new Font("Tahoma", Font.BOLD, 25));
    title.setHorizontalAlignment(SwingConstants.CENTER);
    title.setBounds(313,11,175,62);
    searchPanel.add(title);
    JLabel nameLbl = new JLabel("الاسم");
    nameLbl.setHorizontalAlignment(SwingConstants.CENTER);
    nameLbl.setFont(new Font("Tahoma", Font.PLAIN, 18));
    nameLbl.setBounds(676, 130, 70, 34);
    searchPanel.add(nameLbl);
    nameTxt = new JTextField();
    nameTxt.setBounds(520, 140, 116, 22);
    searchPanel.add(nameTxt);
    nameTxt.setColumns(10);
    JLabel fileNumLbl = new JLabel("رقم الملف");
    fileNumLbl.setHorizontalAlignment(SwingConstants.CENTER);
    fileNumLbl.setFont(new Font("Tahoma", Font.PLAIN, 18));
    fileNumLbl.setBounds(400, 130, 95, 34);
    searchPanel.add(fileNumLbl);
    fileNumTxt = new JTextField("0");
    fileNumTxt.setColumns(10);
    fileNumTxt.setBounds(270, 140, 116, 22);
    searchPanel.add(fileNumTxt);
    JLabel cprLbl = new JLabel("رقم الشخصي");
    cprLbl.setHorizontalAlignment(SwingConstants.CENTER);
    cprLbl.setFont(new Font("Tahoma", Font.PLAIN, 18));
    cprLbl.setBounds(130, 130, 125, 34);
    searchPanel.add(cprLbl);
    cprTxt = new JTextField("0");
    cprTxt.setColumns(10);
    cprTxt.setBounds(10, 140, 116, 22);
    searchPanel.add(cprTxt);
    JLabel caseTypeLbl = new JLabel("نوع الدعوة");
    caseTypeLbl.setHorizontalAlignment(SwingConstants.CENTER);
    caseTypeLbl.setFont(new Font("Tahoma", Font.PLAIN, 18));
    caseTypeLbl.setBounds(660, 180, 95, 34);
    searchPanel.add(caseTypeLbl);
    caseTypeTxt = new JComboBox();
    caseTypeTxt.setModel(new DefaultComboBoxModel(new String[] {"", "جنائي", "مدني", "شرعي", "عمالي"}));
    caseTypeTxt.setBounds(520, 190, 116, 22);
    searchPanel.add(caseTypeTxt);
    searchBtn.setFont(new Font("Tahoma", Font.PLAIN, 18));
    searchBtn.setBounds(270, 190, 116, 25);
    searchPanel.add(searchBtn);
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(12, 232, 758, 268);
    searchPanel.add(scrollPane);
    table.setFont(new Font("Tahoma", Font.PLAIN, 15));
    scrollPane.setViewportView(table);
    table.setEnabled(false);
    table.setRowSelectionAllowed(true);
    table.setRowHeight(30);
    table.getTableHeader().setFont(new Font(null, Font.BOLD, 21));
    backBtn.setBounds(600, 50, 100, 50);
    backBtn.setText("رجوع");
    backBtn.setFont(new Font("Arial", Font.BOLD, 24));
    backBtn.setBorder(new RoundedBorder(20));
    searchPanel.add(backBtn);
    credits.setBounds(250, 500, 300, 50);
    credits.setFont(new Font("Arial", Font.PLAIN, 18));
    searchPanel.add(credits);
}

public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == addPageBtn)
    {
        mainLayout.show(mainPanel, "addPage");
        AddPage();
    }

    if(e.getSource() == backBtn && (current == 2 || current == 3))
    {
        mainLayout.show(mainPanel, "homePage");
        HomePage();
    }

    if(e.getSource() == searchPageBtn)
    {
        mainLayout.show(mainPanel, "searchPage");
        SearchPage();
    }

    if(e.getSource() == searchBtn && current == 3)
    {
        model.setRowCount(0);               //Resets table to ZERO rows
        DisplayData();
        //TEXTFIELDS RESET CODES START
        cprTxt.setText("0");
        fileNumTxt.setText("0");
        nameTxt.setText("");
        caseTypeTxt.setSelectedIndex(0);
        //TEXTFIELDS RESET CODES END
    }
}

public static void main(String[] args) {

    window.setSize(new Dimension(800,600));
    window.setTitle("Alshamlan Attorney & Legal Consultations");
    window.setVisible(true);
    window.setLocationRelativeTo(null);
    window.setResizable(false);

}

}

链接到数据库文件(如果需要): https://drive.google.com/open?id=1qxRPe6ziqoPaGV0VmcDutCPXsyDeQbo0

编辑:怎么样? ENTIRE类文件,现在好吗?

0 个答案:

没有答案