如果jtextfield为空载入jdialog框,则从其他类的jtextfield获取数据

时间:2016-06-16 09:41:16

标签: java swing

我有一个功能在哪里我正在实现rmi服务器接口(SchoolInterfaceImpl)我想检查jtextfield(textField_6)是否为空,如果空加载Jdialog框(FindStudent)我是怎么做的

import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Date;


public class SchoolInterfaceImpl implements SchoolInterface {

    @Override
    public void find(String str) throws RemoteException, SQLException {
        // TODO Auto-generated method stub

    }


import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.FlowLayout;
import java.awt.GridBagLayout;

import javax.swing.JLabel;

import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JTextField;
import javax.swing.JComboBox;

import com.toedter.calendar.JDateChooser;

import javax.swing.JButton;

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


public class StudentProfile extends JFrame {

    String noOfClasses;
    private JPanel contentPane;
    private JTextField textField;
    private JLabel lblFatherguardianName;
    private JTextField textField_1;
    private JLabel lblGender;
    private JComboBox comboBox;
    private JLabel lblDateOfBirth;
    private JDateChooser dateChooser;
    private JLabel lblCurrentAddress;
    private JLabel lblPermenantAddress;
    private JLabel lblPhoneNo;
    private JLabel lblMobileNo;
    private JLabel lblClass;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;
    private JComboBox comboBox_1;
    private JLabel lblRegistrationNo;
    private JTextField textField_6;
    private JButton btnFind;
    private JButton btnSave;
    private JButton btnEdit;
    private JButton btnDelete;
    private JButton btnCancel;
    private JLabel label;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    StudentProfile frame = new StudentProfile();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public StudentProfile() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setTitle("Student Profile");
        setBounds(100, 100, 525, 375);
        setLocation(100, 200);
        setResizable(false);
        setAlwaysOnTop(true);
        setVisible(true);
        contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);
        String str = "Female";

        lblRegistrationNo = new JLabel("Registration No.");
        GridBagConstraints gbc_lblRegistrationNo = new GridBagConstraints();
        gbc_lblRegistrationNo.anchor = GridBagConstraints.WEST;
        gbc_lblRegistrationNo.insets = new Insets(0, 0, 5, 5);
        gbc_lblRegistrationNo.gridx = 1;
        gbc_lblRegistrationNo.gridy = 2;
        contentPane.add(lblRegistrationNo, gbc_lblRegistrationNo);

        textField_6 = new JTextField();
        GridBagConstraints gbc_textField_6 = new GridBagConstraints();
        gbc_textField_6.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_6.insets = new Insets(0, 0, 5, 5);
        gbc_textField_6.gridx = 3;
        gbc_textField_6.gridy = 2;
        contentPane.add(textField_6, gbc_textField_6);
        textField_6.setColumns(10);

        btnFind = new JButton("Find");
        btnFind.setBackground(Color.WHITE);
        btnFind.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            FindStudent fs = new FindStudent();
            fs.setModal(true);
            fs.setVisible(true);
            }
        });
        GridBagConstraints gbc_btnFind = new GridBagConstraints();
        gbc_btnFind.anchor = GridBagConstraints.WEST;
        gbc_btnFind.insets = new Insets(0, 0, 5, 5);
        gbc_btnFind.gridx = 4;
        gbc_btnFind.gridy = 2;
        contentPane.add(btnFind, gbc_btnFind);

        label = new JLabel("Picture");
        GridBagConstraints gbc_label = new GridBagConstraints();
        gbc_label.gridheight = 5;
        gbc_label.gridwidth = 3;
        gbc_label.insets = new Insets(0, 0, 5, 5);
        gbc_label.gridx = 5;
        gbc_label.gridy = 2;
        contentPane.add(label, gbc_label);

        JLabel lblStudentName = new JLabel("Student Name");
        GridBagConstraints gbc_lblStudentName = new GridBagConstraints();
        gbc_lblStudentName.anchor = GridBagConstraints.WEST;
        gbc_lblStudentName.gridwidth = 2;
        gbc_lblStudentName.insets = new Insets(0, 0, 5, 5);
        gbc_lblStudentName.gridx = 1;
        gbc_lblStudentName.gridy = 3;
        contentPane.add(lblStudentName, gbc_lblStudentName);

        textField = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.insets = new Insets(0, 0, 5, 5);
        gbc_textField.gridx = 3;
        gbc_textField.gridy = 3;
        contentPane.add(textField, gbc_textField);
        textField.setColumns(20);

        lblFatherguardianName = new JLabel("Father/Guardian Name");
        GridBagConstraints gbc_lblFatherguardianName = new GridBagConstraints();
        gbc_lblFatherguardianName.anchor = GridBagConstraints.WEST;
        gbc_lblFatherguardianName.insets = new Insets(0, 0, 5, 5);
        gbc_lblFatherguardianName.gridx = 1;
        gbc_lblFatherguardianName.gridy = 4;
        contentPane.add(lblFatherguardianName, gbc_lblFatherguardianName);

        textField_1 = new JTextField();
        GridBagConstraints gbc_textField_1 = new GridBagConstraints();
        gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_1.insets = new Insets(0, 0, 5, 5);
        gbc_textField_1.gridx = 3;
        gbc_textField_1.gridy = 4;
        contentPane.add(textField_1, gbc_textField_1);
        textField_1.setColumns(20);

        lblGender = new JLabel("Gender");
        GridBagConstraints gbc_lblGender = new GridBagConstraints();
        gbc_lblGender.anchor = GridBagConstraints.WEST;
        gbc_lblGender.insets = new Insets(0, 0, 5, 5);
        gbc_lblGender.gridx = 1;
        gbc_lblGender.gridy = 5;
        contentPane.add(lblGender, gbc_lblGender);

        comboBox = new JComboBox();
        comboBox.setBackground(Color.WHITE);
        GridBagConstraints gbc_comboBox = new GridBagConstraints();
        gbc_comboBox.anchor = GridBagConstraints.WEST;
        comboBox.setPrototypeDisplayValue(str);
        comboBox.setMaximumRowCount(2);
        gbc_comboBox.insets = new Insets(0, 0, 5, 5);
        gbc_comboBox.gridx = 3;
        gbc_comboBox.gridy = 5;
        contentPane.add(comboBox, gbc_comboBox);

        lblDateOfBirth = new JLabel("Date of Birth");
        GridBagConstraints gbc_lblDateOfBirth = new GridBagConstraints();
        gbc_lblDateOfBirth.anchor = GridBagConstraints.WEST;
        gbc_lblDateOfBirth.insets = new Insets(0, 0, 5, 5);
        gbc_lblDateOfBirth.gridx = 1;
        gbc_lblDateOfBirth.gridy = 6;
        contentPane.add(lblDateOfBirth, gbc_lblDateOfBirth);

        dateChooser = new JDateChooser();
        dateChooser.getCalendarButton().setBackground(Color.WHITE);
        GridBagConstraints gbc_dateChooser = new GridBagConstraints();
        gbc_dateChooser.fill = GridBagConstraints.HORIZONTAL;
        gbc_dateChooser.anchor = GridBagConstraints.NORTH;
        gbc_dateChooser.insets = new Insets(0, 0, 5, 5);
        gbc_dateChooser.gridx = 3;
        gbc_dateChooser.gridy = 6;
        contentPane.add(dateChooser, gbc_dateChooser);

        lblCurrentAddress = new JLabel("Current Address");
        GridBagConstraints gbc_lblCurrentAddress = new GridBagConstraints();
        gbc_lblCurrentAddress.anchor = GridBagConstraints.WEST;
        gbc_lblCurrentAddress.insets = new Insets(0, 0, 5, 5);
        gbc_lblCurrentAddress.gridx = 1;
        gbc_lblCurrentAddress.gridy = 7;
        contentPane.add(lblCurrentAddress, gbc_lblCurrentAddress);
        String str1 = "Computer Sciences";

        textField_2 = new JTextField();
        GridBagConstraints gbc_textField_2 = new GridBagConstraints();
        gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_2.insets = new Insets(0, 0, 5, 5);
        gbc_textField_2.gridx = 3;
        gbc_textField_2.gridy = 7;
        contentPane.add(textField_2, gbc_textField_2);
        textField_2.setColumns(20);

        lblPermenantAddress = new JLabel("Permenant Address");
        GridBagConstraints gbc_lblPermenantAddress = new GridBagConstraints();
        gbc_lblPermenantAddress.anchor = GridBagConstraints.WEST;
        gbc_lblPermenantAddress.insets = new Insets(0, 0, 5, 5);
        gbc_lblPermenantAddress.gridx = 1;
        gbc_lblPermenantAddress.gridy = 8;
        contentPane.add(lblPermenantAddress, gbc_lblPermenantAddress);

        textField_3 = new JTextField();
        GridBagConstraints gbc_textField_3 = new GridBagConstraints();
        gbc_textField_3.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_3.insets = new Insets(0, 0, 5, 5);
        gbc_textField_3.gridx = 3;
        gbc_textField_3.gridy = 8;
        contentPane.add(textField_3, gbc_textField_3);
        textField_3.setColumns(20);

        lblPhoneNo = new JLabel("Phone No.");
        GridBagConstraints gbc_lblPhoneNo = new GridBagConstraints();
        gbc_lblPhoneNo.anchor = GridBagConstraints.WEST;
        gbc_lblPhoneNo.insets = new Insets(0, 0, 5, 5);
        gbc_lblPhoneNo.gridx = 1;
        gbc_lblPhoneNo.gridy = 9;
        contentPane.add(lblPhoneNo, gbc_lblPhoneNo);

        textField_4 = new JTextField();
        GridBagConstraints gbc_textField_4 = new GridBagConstraints();
        gbc_textField_4.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_4.insets = new Insets(0, 0, 5, 5);
        gbc_textField_4.gridx = 3;
        gbc_textField_4.gridy = 9;
        contentPane.add(textField_4, gbc_textField_4);
        textField_4.setColumns(20);

        lblMobileNo = new JLabel("Mobile No.");
        GridBagConstraints gbc_lblMobileNo = new GridBagConstraints();
        gbc_lblMobileNo.anchor = GridBagConstraints.WEST;
        gbc_lblMobileNo.insets = new Insets(0, 0, 5, 5);
        gbc_lblMobileNo.gridx = 1;
        gbc_lblMobileNo.gridy = 10;
        contentPane.add(lblMobileNo, gbc_lblMobileNo);

        textField_5 = new JTextField();
        GridBagConstraints gbc_textField_5 = new GridBagConstraints();
        gbc_textField_5.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_5.insets = new Insets(0, 0, 5, 5);
        gbc_textField_5.gridx = 3;
        gbc_textField_5.gridy = 10;
        contentPane.add(textField_5, gbc_textField_5);
        textField_5.setColumns(20);

        lblClass = new JLabel("Class");
        GridBagConstraints gbc_lblClass = new GridBagConstraints();
        gbc_lblClass.anchor = GridBagConstraints.WEST;
        gbc_lblClass.insets = new Insets(0, 0, 5, 5);
        gbc_lblClass.gridx = 1;
        gbc_lblClass.gridy = 11;
        contentPane.add(lblClass, gbc_lblClass);

        comboBox_1 = new JComboBox();
        comboBox_1.setBackground(Color.WHITE);
        GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
        comboBox_1.setPrototypeDisplayValue(str);
        comboBox_1.setMaximumRowCount(10);
        gbc_comboBox_1.anchor = GridBagConstraints.WEST;
        gbc_comboBox_1.insets = new Insets(0, 0, 5, 5);
        gbc_comboBox_1.gridx = 3;
        gbc_comboBox_1.gridy = 11;
        contentPane.add(comboBox_1, gbc_comboBox_1);

        btnSave = new JButton("Save");
        btnSave.setBackground(Color.WHITE);
        GridBagConstraints gbc_btnSave = new GridBagConstraints();
        gbc_btnSave.anchor = GridBagConstraints.EAST;
        gbc_btnSave.insets = new Insets(0, 0, 5, 5);
        gbc_btnSave.gridx = 3;
        gbc_btnSave.gridy = 13;
        contentPane.add(btnSave, gbc_btnSave);

        btnEdit = new JButton("Edit");
        btnEdit.setBackground(Color.WHITE);
        GridBagConstraints gbc_btnEdit = new GridBagConstraints();
        gbc_btnEdit.insets = new Insets(0, 0, 5, 5);
        gbc_btnEdit.gridx = 4;
        gbc_btnEdit.gridy = 13;
        contentPane.add(btnEdit, gbc_btnEdit);

        btnDelete = new JButton("Delete");
        btnDelete.setBackground(Color.WHITE);
        GridBagConstraints gbc_btnDelete = new GridBagConstraints();
        gbc_btnDelete.insets = new Insets(0, 0, 5, 5);
        gbc_btnDelete.gridx = 5;
        gbc_btnDelete.gridy = 13;
        contentPane.add(btnDelete, gbc_btnDelete);

        btnCancel = new JButton("Cancel");
        btnCancel.setBackground(Color.WHITE);
        GridBagConstraints gbc_btnCancel = new GridBagConstraints();
        gbc_btnCancel.insets = new Insets(0, 0, 5, 5);
        gbc_btnCancel.gridx = 6;
        gbc_btnCancel.gridy = 13;
        contentPane.add(btnCancel, gbc_btnCancel);

    }

}




import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.GridBagLayout;

import javax.swing.JTextField;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JTable;

import java.util.Vector;

import javax.swing.JLabel;

import java.awt.Color;


public class FindStudent extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField textField;
    private JTextField textField_1;
    private JTable table;
    Connection conn = null;
    Statement stmt = null;
    static Vector<Vector<String>> data = new Vector<Vector<String>>();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            FindStudent dialog = new FindStudent();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public FindStudent() {
        Vector<Vector<String>> data = new Vector<Vector<String>>();
        Vector<String> columnNames = new Vector<String>();
       columnNames.add("RegNo");
       columnNames.add("StudentName");
       columnNames.add("FatherName");
       columnNames.add("Class");

        String query = "Select RegNo, StudentName, FatherName, Class from SchoolDB.dbo.StudentProfile";
        try{
            conn = DriverManager.getConnection("jdbc:sqlserver:" + "//" +
                    "localhost;1433" + "Database=SchooDB"+";integratedSecurity=true;");
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            stmt = conn.createStatement();
            //data.clear();
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {

                Vector<String> vstring = new Vector<String>();


                vstring.add(rs.getString("RegNo"));
                vstring.add(rs.getString("StudentName"));
                vstring.add(rs.getString("FatherName"));
                vstring.add(rs.getString("Class"));
                data.add(vstring);
        }
        }
        catch (Exception e){
            e.printStackTrace();
        }
        /*catch (SQLException e) {
            e.printStackTrace();
            }*/
        finally{
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }

        setBounds(100, 100, 430, 350);
        this.setAlwaysOnTop(true);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBackground(Color.WHITE);
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        GridBagLayout gbl_contentPanel = new GridBagLayout();
        gbl_contentPanel.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        gbl_contentPanel.rowHeights = new int[]{0, 0, 35, 0};
        gbl_contentPanel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
        contentPanel.setLayout(gbl_contentPanel);
        {
            textField = new JTextField();
            GridBagConstraints gbc_textField = new GridBagConstraints();
            gbc_textField.anchor = GridBagConstraints.NORTH;
            gbc_textField.fill = GridBagConstraints.HORIZONTAL;
            gbc_textField.gridwidth = 25;
            gbc_textField.insets = new Insets(0, 0, 10, 5);
            gbc_textField.gridx = 1;
            gbc_textField.gridy = 0;
            contentPanel.add(textField, gbc_textField);
            textField.setColumns(10);
        }
        {
            textField_1 = new JTextField();
            GridBagConstraints gbc_textField_1 = new GridBagConstraints();
            gbc_textField_1.gridwidth = 25;
            gbc_textField_1.insets = new Insets(0, 0, 10, 5);
            gbc_textField_1.anchor = GridBagConstraints.NORTH;
            gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
            gbc_textField_1.gridx = 1;
            gbc_textField_1.gridy = 1;
            contentPanel.add(textField_1, gbc_textField_1);
            textField_1.setColumns(10);
        }
        {
            table = new JTable();
            GridBagConstraints gbc_table = new GridBagConstraints();
            gbc_table.insets = new Insets(0, 0, 0, 5);
            gbc_table.anchor = GridBagConstraints.NORTH;
            gbc_table.gridwidth = 3;
            gbc_table.gridx = 1;
            gbc_table.gridy = 2;

             DefaultTableModel model = new DefaultTableModel(data, columnNames);
                final JTable table = new JTable(model){/**
                     * 
                     */
//                  private static final long serialVersionUID = 1L;

                public boolean isCellEditable(int row, int column) {                
                    return false;               
                };
            };
                table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                table.setPreferredScrollableViewportSize(new Dimension(425, 200));
                table.setFillsViewportHeight(true);
                table.getColumnModel().getColumn(0).setPreferredWidth(90);
                table.getColumnModel().getColumn(1).setPreferredWidth(120);
                table.getColumnModel().getColumn(2).setPreferredWidth(120);
                table.getColumnModel().getColumn(3).setPreferredWidth(40);

                JScrollPane jsp = new JScrollPane(table);
                GridBagConstraints jsp_scroll = new GridBagConstraints();
                jsp_scroll.insets = new Insets(0, 0, 0, 5);
                jsp_scroll.fill = GridBagConstraints.BOTH;
                jsp_scroll.gridwidth = 25;
                jsp_scroll.gridx = 1;
                jsp_scroll.gridy = 3;

                contentPanel.add(jsp, jsp_scroll);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setBackground(Color.WHITE);
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.setBackground(Color.WHITE);
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setBackground(Color.WHITE);
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }

}

2 个答案:

答案 0 :(得分:0)

我想,如果点击&#34;查找&#34; 按钮,而不是检查SchoolInterfaceImpl中 textField_6 的空白,则需要执行此操作:

btnFind.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String strTextField_6 = textField_6.getText();

            if ("".equals(strTextField_6)) {
                FindStudent fs = new FindStudent();
                fs.setModal(true);
                fs.setVisible(true);
            }

            SchoolInterfaceImpl sii = new SchoolInterfaceImpl();
            sii.find(strTextField_6);

        }
    });

答案 1 :(得分:0)

我不确定你的意思是因为我现在不在家,但你可以试试这个:

if (textField_6.getText().equals("")) {
    Jdialog findStudent = new Jdialog(with the arguments needed);
    findStudent.setVisible(true);
}