は、それが文字列項目のリストを表示し、これを試してみてください立ち往生しています。ユーザーは、それらをオンにするかキャンセルすることができます。
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*/}
}
}
あなたが何を求めているかを具体的に説明してください。 –