2016-03-30 23 views
-1

どこから始めるべきか分かりません。これをjoptionpaneで使用するように変更するにはどうすればよいですか?

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"); 

私は

+0

あなたが何を求めているかを具体的に説明してください。 –

答えて

0

は、それが文字列項目のリストを表示し、これを試してみてください立ち往生しています。ユーザーは、それらをオンにするかキャンセルすることができます。

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); 
     } 
    } 

連絡先を検索して検索結果を一覧に表示するためのテキストフィールドを実装することができます。このカスでは、JOptionPanの代わりにWindowを使うことをお勧めします。

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*/} 
} 
} 
関連する問題