Java - 从 JList 更新文本字段

发布于 2024-08-13 01:39:44 字数 618 浏览 3 评论 0原文

我正在使用 Java 制作一个地址簿 GUI,并且有一个 JList,它显示我的 ArrayList 中的所有人员姓名(由 updateinfo< 填充) /code> 方法如下所述)。我希望这样,当我单击 JList 上的某个项目时,TextField 就会使用该人的详细信息进行更新。之前我只使用按钮,因此使用 actionListeners。我想我明白 JList 必须使用 ListSelectionListener 但我似乎无法实现这一点。我在下面添加了我的代码片段。有人可以帮忙吗?为了与我的 actionlisteners 保持连续性,我希望将其作为内部类,但这并不重要

JList jl;
DefaultListModel list;

list = new DefaultListModel();
this.jl = new JList(this.list);
//add ListSelectionListener????
updateList();
this.add(this.jl, layout);

I am making an address book GUI with Java and I have a JList that displays all the names of the people in my ArrayList (this is populated by the updateinfo method mentioned below). I want it so when I click an item on the JList, the TextFields are then updated with that persons details. Before I have only used buttons and therefore actionListeners. I think I understand that a JList must use ListSelectionListener but I cannot seem to implement this. I have added a snippet of my code below. Can somebody please help?? For continuity with my actionlisteners I would like to have it as an inner class but this is not vital

JList jl;
DefaultListModel list;

list = new DefaultListModel();
this.jl = new JList(this.list);
//add ListSelectionListener????
updateList();
this.add(this.jl, layout);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海蓝天 2024-08-20 01:39:44

您可以添加侦听器,然后只查询当前选定的索引。

我为你做了一个样本,希望你觉得它有用。

这是相关部分:

    private JComponent list() {
        final JList list =  new JList( data);
        list.addListSelectionListener(new ListSelectionListener() {
          public void valueChanged(ListSelectionEvent e) {
            int i = list.getSelectedIndex();
            nameTextField.setText( i >= 0 ? data.get( i ) : "" );
          }
        });
        return new JScrollPane( list );
    }

请记住,这不是唯一的方法,这只是您的一个起点。

这是完整的工作示例:

import java.util.Vector;
import java.util.Arrays;

import java.awt.BorderLayout;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JComponent;

import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

public class JListSample {

    private Vector<String> data = new Vector<String>( 
            Arrays.asList( new String []  {
                    "one", "two", "three"  
            })
    );

    private JTextField nameTextField;

    public static void main( String [] args) {
        JListSample s = new JListSample();
        s.run();
    }
    public  void run() {
        JFrame frame = new JFrame("Selection test");
        frame.add( list(), BorderLayout.WEST );
        frame.add( editPanel() );
        frame.pack();
        frame.setVisible( true );

    }

    private JComponent list() {
        final JList list =  new JList( data);
        list.addListSelectionListener(new ListSelectionListener() {
          public void valueChanged(ListSelectionEvent e) {
            int i = list.getSelectedIndex();
            nameTextField.setText( i >= 0 ? data.get( i ) : "" );
          }
        });
        return new JScrollPane( list );
    }
    private JComponent editPanel() {
        JPanel panel = new JPanel();
        panel.add(  new JLabel("Name:") );
        nameTextField = new JTextField(10);
        panel.add( nameTextField );
        return panel;
    }
}

这是显示的内容:

示例 http://img177.imageshack .us/img177/6294/capturadepantalla200911k.png

You can add the listener and then just query the currently selected index.

I did a sample for you, I hope you find it useful.

This is the relevant section:

    private JComponent list() {
        final JList list =  new JList( data);
        list.addListSelectionListener(new ListSelectionListener() {
          public void valueChanged(ListSelectionEvent e) {
            int i = list.getSelectedIndex();
            nameTextField.setText( i >= 0 ? data.get( i ) : "" );
          }
        });
        return new JScrollPane( list );
    }

Bear in mind that's not the only way to go, this is just an starting point for you.

Here's the complete working sample:

import java.util.Vector;
import java.util.Arrays;

import java.awt.BorderLayout;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JComponent;

import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

public class JListSample {

    private Vector<String> data = new Vector<String>( 
            Arrays.asList( new String []  {
                    "one", "two", "three"  
            })
    );

    private JTextField nameTextField;

    public static void main( String [] args) {
        JListSample s = new JListSample();
        s.run();
    }
    public  void run() {
        JFrame frame = new JFrame("Selection test");
        frame.add( list(), BorderLayout.WEST );
        frame.add( editPanel() );
        frame.pack();
        frame.setVisible( true );

    }

    private JComponent list() {
        final JList list =  new JList( data);
        list.addListSelectionListener(new ListSelectionListener() {
          public void valueChanged(ListSelectionEvent e) {
            int i = list.getSelectedIndex();
            nameTextField.setText( i >= 0 ? data.get( i ) : "" );
          }
        });
        return new JScrollPane( list );
    }
    private JComponent editPanel() {
        JPanel panel = new JPanel();
        panel.add(  new JLabel("Name:") );
        nameTextField = new JTextField(10);
        panel.add( nameTextField );
        return panel;
    }
}

This is what is displayed:

sample http://img177.imageshack.us/img177/6294/capturadepantalla200911k.png

_畞蕅 2024-08-20 01:39:44

您只需将选择侦听器添加到列表中,如下所示:

jl.addSelectionListener(new ListSelectionListener() {

  public void valueChanged(ListSelectionEvent e) {
     // evaluate e if necessary and call a method
     // in your class to write the text in the textfield
     int selectedRow = e.getFirstIndex(); // more complicate for multiselects
     updateTextFieldWithName(selectedRow); // to be implemented
  }
});

使用像这里这样的匿名类是最快的方法。虽然读起来有点困难,但这是一个典型的模式。

(刚刚读到您更喜欢内部类,但我无法在手头没有 IDE 的情况下即时编写代码...)

You just add the selection listener to the list, like that:

jl.addSelectionListener(new ListSelectionListener() {

  public void valueChanged(ListSelectionEvent e) {
     // evaluate e if necessary and call a method
     // in your class to write the text in the textfield
     int selectedRow = e.getFirstIndex(); // more complicate for multiselects
     updateTextFieldWithName(selectedRow); // to be implemented
  }
});

Using an anonymous class like here is the quickest way. It's a bit hard to read but a typical pattern.

(just read you preferred an inner class, but I can't code that here on the fly with no IDE at hand...)

流星番茄 2024-08-20 01:39:44

是的,您将需要为此使用 ListSelectionListener,您可能还希望将列表设置为单选(ListSelectionModel.SINGLE_SELECTION)。这将允许用户仅选择列表中的一项。然后,您可以添加 listSelectionListener,并在事件的 valueChanged 中执行类似以下操作(不准确的语法)。

valueChanged(ListSelectionEvent e){
   int idx = e.getFirstIndex();
   int idx2 = e.getLastIndex();  //idx and idx2 should be the same if you set SingleSel
   if(idx==idx2){
     //here you can get the person detail however you have them stored.  You can get   them from the model like so,
      Object personObj = MYLIST.getModel().getElementAt(int index); 
   }


 }

Yes you will want to use a ListSelectionListener for this, you will also probably want to set the list to single selection(ListSelectionModel.SINGLE_SELECTION). This will allow the user to only select one item in the list. You can then add you listSelectionListener, and in the valueChanged of the event do something like the following(not exact syntax).

valueChanged(ListSelectionEvent e){
   int idx = e.getFirstIndex();
   int idx2 = e.getLastIndex();  //idx and idx2 should be the same if you set SingleSel
   if(idx==idx2){
     //here you can get the person detail however you have them stored.  You can get   them from the model like so,
      Object personObj = MYLIST.getModel().getElementAt(int index); 
   }


 }
明月夜 2024-08-20 01:39:44

我想我明白 JList 必须
使用 ListSelectionListener 但我不能
似乎实现了这个

好了,那么就从阅读JList API开始吧。您将找到有关“如何使用列表”的 Swing 教程的链接,其中包含一个工作示例。

另外,在本教程中,您将找到有关“如何编写列表选择侦听器”的部分,其中包含第二个示例。

从教程开始解决您的基本问题。

I think I understand that a JList must
use ListSelectionListener but I cannot
seem to implement this

Well, then start by reading the JList API. You will find a link to the Swing tutorial on "How to Use Lists", which contains a working example.

Also in the tutorial you will find a section on "How to Write a List Selection Listener" which contains a second example.

Start with the tutorial for your basic questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文