简体   繁体   中英

how can i change this to be used in joptionpane?

i have no idea where to start..any tips?

 public static void search(String searchString, String type, int numberOfContacts, Person[] contacts) {

int found = 0;
int[] results = new int[numberOfContacts];

if (type.equals("name")) {
    // name
    for (int x = 0; x < numberOfContacts; x++) {
        if (contacts[x].getName().contains(searchString)) {
            results[found] = x;
            found++;
        }
    }
} else {
    // number
    for (int x = 0; x < numberOfContacts; x++) {
        if (contacts[x].getPhone().contains(searchString)) {
            results[found] = x;
            found++;
        }
    }
}

System.out.println("\n\t**************");
System.out.println("\tSearch Results");
System.out.println("\t**************");
System.out.println("Found " + found + " results containing \"" + searchString + "\":");
System.out.println();

if (found > 0) {
    for (int x = 0; x < found; x++) {
        System.out.println(contacts[results[x]].getName() + "\t" + contacts[results[x]].getPhone());
    }
}

System.out.println("\n\n\n");

I am stuck

Try this, it displays a list of String items. The user can select on of them or cancel.

if (found > 0) {
        //prepare items
        String[] persons = new String[found];
        int defaultSelection = 0;

        for (int x = 0; x < found; x++) {
            persons[x] = contacts[results[x]].getName() + "\t" + 
                    contacts[results[x]].getPhone();
        }

        //show JOptionPane
        String selection = (String) JOptionPane.showInputDialog(
                null, "Please choose", "Title", JOptionPane.QUESTION_MESSAGE, 
                null, persons, persons[defaultSelection]);

        if (selection == null) {
            System.out.println("No item selected");
        } else {
            System.out.println("User selected : " + selection);
        }
    }

You may want to implement a text field for searching contacts and display the search result in a list. In this cas I sugest you to use a Window instead of a JOptionPan.

public class Window extends JFrame implements ActionListener{

private JList<String> listOfResult;
private JTextField searchInput;
private JButton submit;

public Window() {
    //prepar window
    setTitle("Title");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 600);

    //prepar search field
    searchInput = new JTextField("search persion here");
    submit = new JButton("Search");
    submit.addActionListener(this);

    //init list
    listOfResult = new JList<String>();

    //organise components with panels and layouts and add them to the window
    this.getContentPane().setLayout(new BorderLayout());

    JPanel northPanel = new JPanel();
    northPanel.add(searchInput);
    northPanel.add(submit);

    this.add(northPanel, BorderLayout.NORTH);
    this.add(listOfResult, BorderLayout.CENTER);

    //show window
    setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    //this method is called on 'submit' button click
    Person[] searchResult = doSearch(searchInput.getText());
    PersonListModel personList = new PersonListModel(searchResult);
    listOfResult.setModel(personList);
}

public Person[] doSearch(String searchedString) {//implement your code here}

public class PersonListModel implements ListModel<String>{

    private Person[] contacts;

    public PersonListModel(Person[] contacts) {
        this.contacts = contacts;
    }

    @Override
    public int getSize() {
        return contacts.length;
    }

    @Override
    public String getElementAt(int index) {
        return contacts[index].getName() + "\t" + contacts[index].getPhone();
    }

    @Override
    public void addListDataListener(ListDataListener l) {/*not implemented*/}
    @Override
    public void removeListDataListener(ListDataListener l) {/*not implemented*/}
}
}

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