简体   繁体   中英

Java - Why isnt CardLayout Working? Having bugs

UPDATED CODE TO SHOW ALL CLASSES

Person class has setters and getters because I will eventually have user input.(Hard coding for now) Here is some code creating an address book. I have 4 panels so far that I want to switch between depending on the buttons I press. I was originally using GridBagLayout along with BorderLayout , but decided CardLayout is the proper way to go? I tried implementing this CardLayout but am getting an error.

Exception in thread "main": java.lang.NullPointerException
    at JTableSortingPerson.<init>(JTableSortingPerson.java:30)    
    at AddressBook.main(AddressBook.java:100)

Line 30 is just me setting the CardLayout to MainPanel .

Line 100 is me creating a class object in my main to set the layout visible, etc...

I am also having trouble sizing each panel so that they fit or auto resize with JFrame . I am a beginner/student of Computer Science. Any help would be appreciated. ALSO, this is my first time using this site, sorry.

Class AddressBook :

import java.util.*;
import javax.swing.JFrame;

public class AddressBook{

    public static void main(String[] args) {
        JTableSortingPerson p = new JTableSortingPerson();
        p.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        p.setSize(800, 450);
        p.setVisible(true);
    }
}

Class Person :

public class Person {
    String FirstName;
    String MiddleName;
    String LastName;
    String StreetAddress;
    int AptNumber;
    String City;
    String State;
    int ZipCode;
    String PhoneNumber; //change back to long after trialList
    String Email;
    String Group;
    String SpecialDate;
    String Comments;

    public Person(String fn, String mn, String ln, String st, int apt, String City, String State, int zip, String phone, String email, String group, String date, String comments){
        FirstName = fn;
        MiddleName = mn;
        LastName = ln;
        StreetAddress = st;
        AptNumber = apt;
        this.City = City;
        this.State = State;
        ZipCode = zip;
        PhoneNumber = phone;
        this.Email = email;
        this.Group = group;
        SpecialDate = date;
        this.Comments = comments;
    }

    public String toString(){
        return "First Name: " + FirstName + 
            " \nMiddle Name: " + MiddleName + 
            " \nLast Name: " + LastName + 
            " \nStreet Address: " + StreetAddress + 
            " \nApt: " + AptNumber + 
            " \nCity: " + City + 
            " \nState: " + State + 
            " \nZip: " + ZipCode + 
            " \nPhone: " + PhoneNumber +
            " \nEmail: " + Email + 
            " \nGroup: " + Group + 
            " \nDates: " + SpecialDate + 
            " \nComments " + "***" + Comments + "***\n\n";
    }
    public String getFirstName() {
        return FirstName;
    }
    public void setFirstName(String firstName) {
        this.FirstName = firstName;
    }
    public String getMiddleName() {
        return MiddleName;
    }
    public void setMiddleName(String middleName) {
        this.MiddleName = middleName;
     }
    public String getLastName() {
        return LastName;
    }
    public void setLastName(String lastName) {
        this.LastName = lastName;
    }
    public String getStreetAddress() {
        return StreetAddress;
    }
    public void setStreetAddress(String streetAddress) {
        this.StreetAddress = streetAddress;
    }
    public int getAptNumber() {
        return AptNumber;
    }
    public void setAptNumber(int aptNumber) {
        this.AptNumber = aptNumber;
    }
    public String getCity() {
        return City;
    }
    public void setCity(String city) {
        this.City = city;
    }
    public String getState() {
        return State;
    }
    public void setState(String state) {
        this.State = state;
    }
    public int getZipCode() {
        return ZipCode;
    }
    public void setZipCode(int zipCode) {
        this.ZipCode = zipCode;
    }
    public String getPhoneNumber() {
        return PhoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.PhoneNumber = phoneNumber;
    }
    public String getEmail() {
        return Email;
    }
    public void setEmail(String email) {
        this.Email = email;
    }
    public String getGroup() {
        return Group;
    }
    public void setGroup(String group) {
        this.Group = group;
    }
    public String getSpecialDate() {
        return SpecialDate;
    }
    public void setSpecialDate(String specialDate) {
        this.SpecialDate = specialDate;
    }
    public String getComments() {
        return Comments;
    }
    public void setComments(String comments) {
        this.Comments = comments;
    }

}

Class PersonTable :

import javax.swing.table.AbstractTableModel;
import java.util.*;

public class PersonTable extends AbstractTableModel {
    private static final int COLUMN_FIRST_NAME = 0;
    private static final int COLUMN_LAST_NAME = 1;
    private static final int COLUMN_PHONE_NUMBER = 2;
    private static final int COLUMN_CITY = 3;
    private String[] columnNames = {"First Name", "Last Name", "Phone  Number", "City"};
    private List<Person> listPerson;

    public PersonTable(List<Person> listPerson){
        this.listPerson = listPerson;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return listPerson.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Person person = listPerson.get(rowIndex);
        Object retVal = null;
        switch(columnIndex){
        case COLUMN_FIRST_NAME: retVal = person.getFirstName(); break;
        case COLUMN_LAST_NAME: retVal = person.getLastName(); break;
        case COLUMN_PHONE_NUMBER: retVal = person.getPhoneNumber(); break;
        case COLUMN_CITY: retVal = person.getCity(); break;
        default: throw new IllegalArgumentException("Invalid column index.");
        }
        return retVal;
    }

    @Override 
    public String getColumnName(int columnIndex){
        return columnNames[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex){
        if(listPerson.isEmpty()) return Object.class;
        else return getValueAt(0, columnIndex).getClass();
    }
}

Class PersonTable2 :

import javax.swing.table.AbstractTableModel;
import java.util.*;

public class PersonTable2 extends AbstractTableModel{
    private static final int COLUMN_FIRST_NAME = 0;
    private static final int COLUMN_MIDDLE_NAME = 1;
    private static final int COLUMN_LAST_NAME = 2;
    private static final int COLUMN_PHONE_NUMBER = 3;
    private static final int COLUMN_CITY = 4;
    private static final int COLUMN_STATE = 5;
    private static final int COLUMN_EMAIL = 6;
    private static final int COLUMN_GROUP = 7;
    private String[] columnNames = {"First Name", "Middle Name", "Last Name", "Phone Number", "City", "State", "Email", "Group"};
    private List<Person> listPerson;

    public PersonTable2(List<Person> listPerson){
        this.listPerson = listPerson;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return listPerson.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Person person = listPerson.get(rowIndex);
        Object retVal = null;
        switch(columnIndex){
        case COLUMN_FIRST_NAME: retVal = person.getFirstName(); break;
        case COLUMN_MIDDLE_NAME: retVal = person.getMiddleName(); break;
        case COLUMN_LAST_NAME: retVal = person.getLastName(); break;
        case COLUMN_PHONE_NUMBER: retVal = person.getPhoneNumber(); break;
        case COLUMN_CITY: retVal = person.getCity(); break;
        case COLUMN_STATE: retVal = person.getState(); break;
        case COLUMN_EMAIL: retVal = person.getEmail(); break;
        case COLUMN_GROUP: retVal = person.getGroup(); break;
        default: throw new IllegalArgumentException("Invalid column index.");
        }
        return retVal;
    }

    @Override 
    public String getColumnName(int columnIndex){
        return columnNames[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex){
        if(listPerson.isEmpty()) return Object.class;
        else return getValueAt(0, columnIndex).getClass();
    }
}

Class JTableSortingPerson :

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List; //important
import java.util.*;
import javax.swing.*;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

@SuppressWarnings("serial")
public class JTableSortingPerson extends JFrame{
    private JPanel mainPanel, panel, panel2, introPanel, newUserPanel, editPanel;
    private JTextArea textArea;
    private JTextArea textArea2;
    private JTextField searchInput, userName, passWord, newUser, newPass, confirmPassword;
    private JTextField searchInput2;
    private JLabel statusBar, user, pass, newUserName, newPassword, newConfirmPassword;
    private JLabel statusBar2;
    private JTable table, table2;
    private JButton DetailButton, SearchButton, AllButton, LogInButton, NewButton, saveButton, cancelButton, addButton, removeButton, editButton;
    private JButton DetailButton2, SearchButton2, goBack, LogInButton2, NewButton2, saveButton2, cancelButton2, addButton2, removeButton2, editButton2;
    private CardLayout cardLayout = new CardLayout();
    List<Person> listPerson = createListPerson();

    public JTableSortingPerson() {
        super();

        mainPanel.setLayout(cardLayout);

        //***********************************************
        //LOG IN PANEL 
        //***********************************************

        introPanel = new JPanel();
        GridBagLayout GridLayout = new GridBagLayout();
        introPanel.setLayout(GridLayout);
        GridBagConstraints c = new GridBagConstraints();
        //add(introPanel, BorderLayout.CENTER);
        mainPanel.add(introPanel, "Log In");

        NewButton = new JButton("New User");
        NewButton.addActionListener(new ButtonListener());
        LogInButton = new JButton("Log In");
        LogInButton.addActionListener(new ButtonListener());
        userName = new JTextField();
        passWord = new JTextField();
        user = new JLabel("Username:");
        pass = new JLabel("Password:");

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        introPanel.add(user, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 3;
        introPanel.add(pass, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1000;
        introPanel.add(userName, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 1000;
        introPanel.add(passWord, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 5;
        c.gridwidth = 1000;
        introPanel.add(LogInButton, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 6;
        c.gridwidth = 1000;
        introPanel.add(NewButton, c);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        //***********************************************
        //MAIN TABLE PANEL
        //***********************************************

        panel = new JPanel();
        GridBagLayout GridLayout2 = new GridBagLayout();
        panel.setLayout(GridLayout2);
        GridBagConstraints c2 = new GridBagConstraints();

        PersonTable tableModel = new PersonTable(listPerson);
        table = new JTable(tableModel);
        table.setAutoCreateRowSorter(true);

        DetailButton = new JButton("Details");
        DetailButton.addActionListener(new ButtonListener());
        SearchButton = new JButton("Search");
        SearchButton.addActionListener(new ButtonListener());
        AllButton = new JButton("Display All");
        AllButton.addActionListener(new ButtonListener());
        addButton = new JButton("Add Person");
        addButton.addActionListener(new ButtonListener());
        removeButton = new JButton("Remove Person");
        removeButton.addActionListener(new ButtonListener());
        searchInput = new JTextField();
        textArea = new JTextArea();
        statusBar = new JLabel();

        //add(panel, BorderLayout.CENTER);
        mainPanel.add(panel, "Edit Person Table");
        add(statusBar, BorderLayout.SOUTH);

        panel.add(new JScrollPane(table), c2); 
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 1;
        c2.gridwidth = 1000;
        panel.add(searchInput, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 2;
        c2.gridwidth = 1000;
        panel.add(SearchButton, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 3;
        c2.gridwidth = 1000;
        panel.add(DetailButton, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 4;
        c2.gridwidth = 1000;
        panel.add(AllButton, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 5;
        c2.gridwidth = 1000;
        panel.add(addButton, c2);
        c2.fill = GridBagConstraints.HORIZONTAL;
        c2.gridx = 0;
        c2.gridy = 6;
        c2.gridwidth = 1000;
        panel.add(removeButton, c2);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        panel.setVisible(false);

        //***********************************************
        //CREATE NEW USER PANEL
        //***********************************************

        newUserPanel = new JPanel();
        GridBagLayout GridBagLayout3 = new GridBagLayout();
        newUserPanel.setLayout(GridBagLayout3);
        GridBagConstraints c3 = new GridBagConstraints();
        //add(newUserPanel, BorderLayout.CENTER);
        mainPanel.add(newUserPanel, "New User");

        newUser = new JTextField();
        newPass = new JTextField();
        confirmPassword = new JTextField();
        newUserName = new JLabel("New Username:");
        newPassword = new JLabel("New Password:");
        newConfirmPassword = new JLabel("Confirm Password");
        saveButton = new JButton("Save");
        saveButton.addActionListener(new ButtonListener());
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ButtonListener());

        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 1;
        c3.gridwidth = 1000;
        newUserPanel.add(newUserName, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 2;
        c3.gridwidth = 1000;
        newUserPanel.add(newUser, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 3;
        c3.gridwidth = 1000;
        newUserPanel.add(newPassword, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 4;
        c3.gridwidth = 1000;
        newUserPanel.add(newPass, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 5;
        c3.gridwidth = 1000;
        newUserPanel.add(newConfirmPassword, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 6;
        c3.gridwidth = 1000;
        newUserPanel.add(confirmPassword, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 7;
        c3.gridwidth = 1000;
        newUserPanel.add(saveButton, c3);
        c3.fill = GridBagConstraints.HORIZONTAL;
        c3.gridx = 0;
        c3.gridy = 8;
        c3.gridwidth = 1000;
        newUserPanel.add(cancelButton, c3);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        newUserPanel.setVisible(false);

        //***********************************************
        //MAIN TABLE PANEL2
        //***********************************************

        panel2 = new JPanel();
        GridBagLayout GridLayout4 = new GridBagLayout();
        panel2.setLayout(GridLayout4);
        GridBagConstraints c4 = new GridBagConstraints();

        PersonTable2 tableModel2 = new PersonTable2(listPerson);
        table2 = new JTable(tableModel2);
        table2.setAutoCreateRowSorter(true);

        DetailButton2 = new JButton("Details");
        DetailButton2.addActionListener(new ButtonListener());
        SearchButton2 = new JButton("Search");
        SearchButton2.addActionListener(new ButtonListener());
        goBack = new JButton("Back");
        goBack.addActionListener(new ButtonListener());
        searchInput2 = new JTextField();
        textArea2 = new JTextArea();
        statusBar2 = new JLabel();

        //add(panel2, BorderLayout.CENTER);
        mainPanel.add(panel2, "All Person Table");
        add(statusBar2, BorderLayout.SOUTH);

        panel2.add(new JScrollPane(table2), c4); 
        c4.fill = GridBagConstraints.HORIZONTAL;
        c4.gridx = 0;
        c4.gridy = 1;
        c4.gridwidth = 1000;
        panel2.add(searchInput2, c4);
        c4.fill = GridBagConstraints.HORIZONTAL;
        c4.gridx = 0;
        c4.gridy = 2;
        c4.gridwidth = 1000;
        panel2.add(SearchButton2, c4);
        c4.fill = GridBagConstraints.HORIZONTAL;
        c4.gridx = 0;
        c4.gridy = 3;
        c4.gridwidth = 1000;
        panel2.add(DetailButton2, c4);
        c4.fill = GridBagConstraints.HORIZONTAL;
        c4.gridx = 0;
        c4.gridy = 4;
        c4.gridwidth = 1000;
        panel2.add(goBack, c4);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        panel2.setVisible(false);

        //*********CARD LAYOUT*********

        //mainPanel.add(introPanel, "Log In");
        //mainPanel.add(newUserPanel, "New User");
        //mainPanel.add(panel, "Edit Person Table");
        //mainPanel.add(panel2, "All Person Table");

        this.setContentPane(mainPanel);

        cardLayout.show(mainPanel, "introPanel");

    }

    public List<Person> createListPerson(){
        ArrayList<Person> retVal = new ArrayList<Person>();
        retVal.add(new Person("Nicolas", "Lawrence", "Soto", "10513 Demilo Pl", 110, "Orlando", "FL", 32836, "3052159446", "nsoto0216@hotmail.com", "Self", "02/16/1991", "I am a student."));
        retVal.add(new Person("Sebastian", "Matias", "Soto", "10513 Demilo Pl", 110, "Orlando", "FL", 32836, "3213559306", "ssoto1005@hotmail.com", "Family", "10/05/1992", "My little brother."));
        retVal.add(new Person("Montserrat", "Soto", "Casco", "12401 nw 185 st", 0, "Miami", "FL", 33161, "3052023390", "msoto0331@gmail.com", "Family", "03/31/1987", "My sister."));
        retVal.add(new Person("Marcelo", "Suarez", "Soto", "4420 nw 16 st", 203, "Miami", "FL", 33055, "3213559355", "chefchileno3@hotmail.com", "Family", "10/28/1955", "My Father."));
        retVal.add(new Person("Olga", "Beatriz", "Munizaga", "4420 nw 16 st", 203, "Miami", "FL", 33055, "3213559356", "omunizaga0326@hotmail.com", "Family", "03/26/1959", "My Mother."));
        retVal.add(new Person("Linda", "Cecilia", "Chavez", "3256 nw 12 st", 0, "Miami", "FL", 33056, "3056234456", "lindizee09@hotmail.com", "Friend", "10/10/1991", "ex girlfriend."));
        retVal.add(new Person("Laura", "Perez", "Carrasco", "500 Pinnacle Cove", 203, "Orlando", "FL", 32078, "4072180248", "lcarrasco0325@hotmail.com", "Friend", "03/25/1991", "ex girlfriend."));
        retVal.add(new Person("Jenn", "", "Ortiz", "2034 Lanstar Blvd", 0, "Orlando", "FL", 32724, "8084735466", "jenn456@gmail.com", "Friend", "07/17/1993", "crazy girl"));
        retVal.add(new Person("Cristian", "", "Camacho", "4556 Americana blvd", 308, "Orlando", "FL", 32856, "4076557890", "chrisc176@hotmail.com", "Friend", "12/02/1989", ""));
        retVal.add(new Person("Gabriela", "", "Valdez", "3004 Watercrest ln", 109, "Orlando", "FL", 32747, "4073066789", "gabbyvaldez@gmail.com", "Friend", "07/15/1992", "Moved to California"));
        retVal.add(new Person("Devin", "", "Delaney", "", 0, "Orlando", "FL", 32836, "4074259556", "ddevin64@hotmail.com", "Coworker", "02/23/1993", "my favorite coworker"));
        return retVal;
    }

    public class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == DetailButton) {
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                introPanel.invalidate();
                introPanel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                panel.validate();
                panel.setVisible(true);
                int row = table.getSelectedRow();
                if (row == -1) {
                    statusBar.setText("Make a selection");
                    JOptionPane.showMessageDialog(null, "No Selection was made");
                } else {
                    statusBar.setText("Showing details...");
                    row = table.convertRowIndexToModel(table.getSelectedRow()); 
                    JOptionPane.showMessageDialog(null, listPerson.get(row));
                }
            } else if (e.getSource() == SearchButton) {
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                introPanel.invalidate();
                introPanel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                panel.validate();
                panel.setVisible(true);
                JTextFieldActionPerformed(e);
            } else if (e.getSource() == AllButton) {
                statusBar.setText("Showing all people...");
                /*JTextArea textArea = new JTextArea(listPerson.toString());
                JScrollPane scroll = new JScrollPane(textArea);
                JDialog displayAll = new JDialog();
                displayAll.setVisible(true);
                displayAll.setSize(350, 750);
                displayAll.add(scroll);*/
                statusBar.setText("Done");
                introPanel.invalidate();
                introPanel.setVisible(false);
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                panel.invalidate();
                panel.setVisible(false);
                panel2.validate();
                panel2.setVisible(true);
            } else if (e.getSource() == addButton){

            } else if (e.getSource() == removeButton){
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                introPanel.invalidate();
                introPanel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                panel.validate();
                panel.setVisible(true);
                int row = table.getSelectedRow();
                if (row == -1) {
                    JOptionPane.showMessageDialog(null, "No Selection was made");
                } else {
                    row = table.convertRowIndexToModel(table.getSelectedRow()); 
                    listPerson.remove(row);
                    table.addNotify();
                    //JOptionPane.showMessageDialog(null, listPerson.get(row));
                }
            } else if (e.getSource() == DetailButton2) {
                panel.invalidate();
                panel.setVisible(false);
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                introPanel.invalidate();
                introPanel.setVisible(false);
                panel2.validate();
                panel2.setVisible(true);
                    int row = table2.getSelectedRow();
                    if (row == -1) {
                        statusBar2.setText("Make a selection");
                        JOptionPane.showMessageDialog(null, "No Selection was made");
                    } else {
                        statusBar2.setText("Showing details...");
                        row = table2.convertRowIndexToModel(table2.getSelectedRow()); 
                        JOptionPane.showMessageDialog(null, listPerson.get(row));
                    }
            } else if (e.getSource() == SearchButton2) {
                panel.invalidate();
                panel.setVisible(false);
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                introPanel.invalidate();
                introPanel.setVisible(false);
                panel2.validate();
                panel2.setVisible(true);
                JTextFieldActionPerformed2(e);
            } else if (e.getSource() == goBack) {
                /*JTextArea textArea = new JTextArea(listPerson.toString());
                JScrollPane scroll = new JScrollPane(textArea);
                JDialog displayAll = new JDialog();
                displayAll.setVisible(true);
                displayAll.setSize(350, 750);
                displayAll.add(scroll);*/
                statusBar2.setText("Done");
                introPanel.invalidate();
                introPanel.setVisible(false);
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                panel.validate();
                panel.setVisible(true);
            } else if (e.getSource() == LogInButton){
                introPanel.invalidate();
                introPanel.setVisible(false);
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                panel.validate();
                panel.setVisible(true);
                //pack();
                //JOptionPane.showMessageDialog(null, "You attempted to Log In");
            } else if (e.getSource() == NewButton){
                introPanel.invalidate();
                introPanel.setVisible(false);
                panel.invalidate();
                panel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                newUserPanel.validate();
                newUserPanel.setVisible(true);
                //pack();
                //JOptionPane.showMessageDialog(null, "You attempted to create an account");
            } else if (e.getSource() == saveButton){

            } else if (e.getSource() == cancelButton){
                panel.invalidate();
                panel.setVisible(false);
                panel2.invalidate();
                panel2.setVisible(false);
                newUserPanel.invalidate();
                newUserPanel.setVisible(false);
                introPanel.validate();
                introPanel.setVisible(true);
            }
        }
    }

    public void JTextFieldActionPerformed(java.awt.event.ActionEvent evt)
    {                                            
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(((PersonTable) table.getModel())); 
        sorter.setRowFilter(RowFilter.regexFilter("(?i)" + searchInput.getText())); //"(?i)" for case sensitivity
        table.setRowSorter(sorter);
    } 

    public void JTextFieldActionPerformed2(java.awt.event.ActionEvent evt)
    {                                            
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(((PersonTable2) table2.getModel())); 
        sorter.setRowFilter(RowFilter.regexFilter("(?i)" + searchInput2.getText())); //"(?i)" for case sensitivity
        table2.setRowSorter(sorter);
    } 

    public void newUser(){
        /*try {

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*/
    }

    public void LogIn(String user, String pass){

    }

}

I think I will not be the only guy to stop trying to help you, sorry for that :P

Your code is insane to read, and you clearly need to improve your Java skills. I'm pretty sure that your bug will magically disappear after a good code cleaning !

So to give you some help (and sorry for that, but not the direct solution for your problem) I suggest to you some ideas to refactor your code:

Avoid classes with more than approximatively 10 attributes; use more classes to represent your data !

Secondly, try to use Enums (it's very easy, you will see) instead of that sort of things:

private static final int COLUMN_FIRST_NAME = 0;
private static final int COLUMN_LAST_NAME = 1;
private static final int COLUMN_PHONE_NUMBER = 2;
private static final int COLUMN_CITY = 3;

And finally I will suggest you to break your JTableSortingPerson into many little components instead of having all the basic bricks at the same place. It's like build a tower directly with sand instead of using bricks that was made of this sand.

I can say many and many things to improve your code, but I will vex you first, and make you crazy of all the technical things I will give you !

Good luck refactoring, it's the key I think !

我忘记了“ mainPanel = new JPanel();”

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