Java 从JList中选择项目后,如何填写文本字段
我有一个家庭作业的问题,但我仍然试图把我的头围绕图形用户界面逻辑。我已经创建了所有文本字段(contactType、姓名、地址、城市、州等),并创建了一个actionListener,以便在我从JMenu中单击open时用名称填充我的JList。当我从列表中选择名称时,在填写相应的文本字段时遇到了问题。我试着把它打印到控制台上,看看它是否能打印出至少一个名字,但这根本不起作用。任何帮助都会很好,谢谢 以下是我到目前为止的一些代码:Java 从JList中选择项目后,如何填写文本字段,java,swing,jlabel,jlist,jmenu,Java,Swing,Jlabel,Jlist,Jmenu,我有一个家庭作业的问题,但我仍然试图把我的头围绕图形用户界面逻辑。我已经创建了所有文本字段(contactType、姓名、地址、城市、州等),并创建了一个actionListener,以便在我从JMenu中单击open时用名称填充我的JList。当我从列表中选择名称时,在填写相应的文本字段时遇到了问题。我试着把它打印到控制台上,看看它是否能打印出至少一个名字,但这根本不起作用。任何帮助都会很好,谢谢 以下是我到目前为止的一些代码: public class AddressBookGUI exte
public class AddressBookGUI extends JFrame
{
private final int WIDTH = 450;
private final int HEIGHT = 300;
private JLabel currentlySelected;
private JTextField contactTypeTextField;
private JTextField nameTextField;
private JTextField streetAddressTextField;
private JTextField cityTextField;
private JTextField stateTextField;
private JTextField zipTextField;
private JTextField phoneTextField;
private JTextField emailTextField;
private JTextField photoTextField;
private JList nameList ;
private DefaultListModel model;
private JTextArea statusTextArea;
private AddressBook addressBook;
private JButton addButton;
private JButton editButton;
private JButton sortByZipButton;
private JMenuItem addItem;
private JMenuItem openItem;
private JMenuItem saveItem;
private JMenuItem exitItem;
private JMenuItem editContactItem;
private JMenuItem aboutItem;
private JMenuItem deleteItem;
private JComboBox<String> jComboStates;
private JComboBox <Enum> jComboContactType;
private String [] readStates()
{
ArrayList<String> array = new ArrayList<>();
try
{
FileInputStream fStream = new FileInputStream ("States.txt");
BufferedReader buffer = new BufferedReader (new InputStreamReader (fStream));
String strLine;
while ((strLine = buffer.readLine()) != null)
{
String line = buffer.readLine();
String [] state = line.split ("\n");
array.add(strLine);
}
buffer.close();
}
catch (Exception e)
{
}
return array.toArray(new String[array.size()]);
}
private ArrayList<String> readContacts()
{
File cFile = new File ("Contacts.txt");
BufferedReader buffer = null;
ArrayList <String> contact = new ArrayList<String>();
try
{
buffer = new BufferedReader (new FileReader (cFile));
String text;
String sep;
while ((sep = buffer.readLine()) != null)
{
String [] name = sep.split (",");
text = name[1];
contact.add(text);
}
}
catch (FileNotFoundException e)
{
System.out.print ("error");
}
catch (IOException k)
{
System.out.print ("error);
}
return contact;
}
{
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu ("File");
openItem = new JMenuItem ("Open...");
fileMenu.add (openItem);
fileMenu.addSeparator();
saveItem = new JMenuItem ("Save...");
fileMenu.add (saveItem);
fileMenu.addSeparator();
exitItem = new JMenuItem ("Exit");
fileMenu.add (exitItem);
JMenu editMenu = new JMenu ("Edit");
editContactItem = new JMenuItem ("Edit Contact");
editMenu.add (editContactItem);
addItem = new JMenuItem ("Add Contact");
editMenu.add (addItem);
deleteItem = new JMenuItem ("Delete Contact");
editMenu.add (deleteItem);
JMenu helpMenu = new JMenu ("Help");
aboutItem = new JMenuItem ("About");
helpMenu.add(aboutItem);
menuBar.add (fileMenu);
menuBar.add (editMenu);
menuBar.add (helpMenu);
setJMenuBar (menuBar);
//code that creates new text fields for all attributes
//code that sets textfields to uneditable
JPanel topPanel = new JPanel()
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.add (currentlySelected, BorderLayout.CENTER);
leftPanel.add (new JScrollPane(), BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
JPanel infoPanel = new JPanel (new BorderLayout());
GridLayout layout = new GridLayout (9,1);
JPanel labelsPanel = new JPanel (layout);
JPanel valuesPanel = new JPanel (layout);
// code that added lables and values to panel
infoPanel.add (labelsPanel, BorderLayout.WEST);
infoPanel.add (valuesPanel, BorderLayout.CENTER);
centerPanel.add (infoPanel);
setLayout (new BorderLayout());
add (topPanel, BorderLayout.NORTH);
add (leftPanel, BorderLayout.WEST);
add (centerPanel, BorderLayout.CENTER);
addressBook = new AddressBook();
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible (true);
openItem.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
readContacts();
for (String name :readContacts())
{
model.addElement(name);
}
nameList = new JList (model);
add(nameList);
nameList.setVisibleRowCount(10);
nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
nameList.setFixedCellHeight (20);
nameList.setFixedCellWidth (130);
JPanel left = new JPanel(new BorderLayout());
left.add (new JScrollPane(nameList), BorderLayout.NORTH);
add (left, BorderLayout.WEST);
nameList.setBorder (BorderFactory.createLineBorder(Color.BLACK,1));
}
});
editContactItem.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
nameTextField.setEditable (true);
streetAddressTextField.setEditable (true);
cityTextField.setEditable (true);
jComboStates.setEditable (true);
zipTextField.setEditable (true);
phoneTextField.setEditable (true);
emailTextField.setEditable (true);
photoTextField.setEditable (true);
}
});
saveItem.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
addressBook.Save ( );
}
});
nameList = new JList ();
nameList.addListSelectionListener (new ListSelectionListener()
{
public void valueChanged (ListSelectionEvent e)
{
if (e.getValueIsAdjusting ( ) == false)
{
List <String> string = nameList.getSelectedValuesList();
System.out.print (string);
}
}
});
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int index = nameList.locationToIndex(e.getPoint());
System.out.println("Double clicked on Item " + index);
}
}
};
nameList.addMouseListener(mouseListener);
}
使用上述代码向JList添加数据
但后来你会:
nameList = new JList ();
nameList.addListSelectionListener (new ListSelectionListener()
{
public void valueChanged (ListSelectionEvent e)
{
if (e.getValueIsAdjusting ( ) == false)
{
List <String> string = nameList.getSelectedValuesList();
System.out.print (string);
}
}
});
使用上述代码向JList添加数据
但后来你会:
nameList = new JList ();
nameList.addListSelectionListener (new ListSelectionListener()
{
public void valueChanged (ListSelectionEvent e)
{
if (e.getValueIsAdjusting ( ) == false)
{
List <String> string = nameList.getSelectedValuesList();
System.out.print (string);
}
}
});
我支持罗布·卡米克写的所有东西。此外,您只将名称字符串添加到列表中,而没有添加完整的地址对象,因此密钥信息丢失。要获得它,请将您的JList设置为地址对象列表,为其提供一个仅显示名称的自定义单元格渲染器,但这将允许您选择的对象拥有您需要的所有数据。我支持Rob Camick编写的所有内容。此外,您只将名称字符串添加到列表中,而没有添加完整的地址对象,因此密钥信息丢失。要获得它,请将JList设置为地址对象列表,为其提供一个仅显示名称的自定义单元格渲染器,但这将允许所选对象具有所需的所有数据。我知道为名称列表声明新JList是错误的,但否则我将获得NullPointerException。谢谢你澄清这一点。我知道为名称列表声明一个新的JList是错误的,但否则我会得到NullPointerException。谢谢你澄清。这会取代JList,还是完全不同?@ChelseaH:不,你会使用相同的JList,但将其设置为
JList
(假设你有地址类或类似的东西),而不是JList
。搜索有关如何使用JLists的Java Swing教程,它将帮助您创建自定义单元格渲染器,该渲染器将在JList中显示名称字符串。是的,创建自定义地址对象,一旦您从列表中选择名称,就可以轻松填充文本字段。(+1)。由于我已经解决了JList问题,我将继续阅读有关此单元渲染器的内容。小步骤。这会取代JList,还是完全不同?@ChelseaH:不,您会使用相同的JList,但将其设置为JList
(假设您有地址类或类似的内容),而不是JList
。搜索有关如何使用JLists的Java Swing教程,它将帮助您创建自定义单元格渲染器,该渲染器将在JList中显示名称字符串。是的,创建自定义地址对象,一旦您从列表中选择名称,就可以轻松填充文本字段。(+1)。由于我已经解决了JList问题,我将继续阅读有关此单元渲染器的内容。小台阶。交叉标示:交叉标示:
nameList = new JList (model);
nameList.addListSelectionListener(...);