简体   繁体   English

Actionlistener将Jlist信息输出到textarea

[英]Actionlistener outputing Jlist info to textarea

Could someone help me with the following problem? 有人可以帮我解决以下问题吗? I want to implement a actionlistener that outputs info about the highlighted Jlist object into the already present textarea. 我想实现一个动作侦听器,该动作侦听器将有关突出显示的Jlist对象的信息输出到已经存在的textarea中。

Here is the code i've managed to cook up: 这是我设法制作的代码:

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;

  public class ListModel extends JFrame {

   private JList list;
   private DefaultListModel model;
   private JTextField inputf;
   private JTextField inpute;
   private JTextArea text;
   private JPanel p1;
   private JPanel p2;

    public ListModel() {
      setLayout(new FlowLayout());
      model = new DefaultListModel();
      list = new JList(model);
      JButton addButton = new JButton( "Lägg till" );
      model.addElement("Kajsa Åslund");
      model.addElement("Erik Carlsson");
      model.addElement("John Åkesson");
      model.addElement("Per-arne Ingvarsson");
      model.addElement("Rebecka Asp");
      model.addElement("Linnéa Åslund");
      model.addElement("Åsa-Nisse Strong");
      model.addElement("Super Lasse");
      model.addElement("Alexander Ahl");
      model.addElement("Ann Ahl");
      model.addElement("Bo Sten");
     addButton.addActionListener(
     new ActionListener() {
        public void actionPerformed( ActionEvent event )
                     {
                     final String name=inputf.getText() + " " + inpute.getText();

                     model.addElement( name );
                 }
             }
             );
     JButton removeButton =
     new JButton( "Ta bort" );

     removeButton.addActionListener(
         new ActionListener() {

         public void actionPerformed( ActionEvent event )
             {
             setTitle("Borttagning");
             try
            {
            Thread.sleep(1000);
            }
            catch(InterruptedException e)
            {
            e.printStackTrace();
            }
             model.removeElement(list.getSelectedValue());
             setTitle("Personer");
         }
     }
     );

     text = new JTextArea(5, 20);

     inputf = new JTextField();
     inputf.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }

    });
     inpute = new JTextField();
     inpute.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }

    });
     list.setSelectionMode(
     ListSelectionModel.SINGLE_SELECTION );

     inpute.setBounds(5, 5, 100, 100);
     inpute.setPreferredSize(new Dimension(120,20));
     inputf.setBounds(10, 10, 150, 150);
     inputf.setPreferredSize(new Dimension(120,20));
     JScrollPane scroll = new JScrollPane(list);
     scroll.setPreferredSize(new Dimension(200,200));

     JPanel p2 = new JPanel();
     p2.add(text);
     p2.add(inputf);
     p2.add(inpute);
     p2.add(addButton);
     p2.add(removeButton);
     p2.setLayout(new BoxLayout(p2,BoxLayout.Y_AXIS));

     JPanel p1 = new JPanel();
     p1.add(scroll);


     Container container = getContentPane();
     container.add(p1);
     container.add(p2);
     container.setLayout(new FlowLayout());

     setDefaultCloseOperation( EXIT_ON_CLOSE );
     setSize( 500, 250 );
     setVisible( true );
 }

   public static void main( String args[] )
         {
         new ListModel();
     }
   }

Thanks in advance :) 提前致谢 :)

Right now only data you can read from list is name and surname, you can achieve by using ListSelectionListener : 现在,您只能从list读取的数据是名称和姓氏,您可以使用ListSelectionListener来实现:

list.addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        text.setText(list.getSelectedValue().toString());
    }
});

add this listener to JList object 将此侦听器添加到JList对象

list.addListSelectionListener(new ListSelectionListener() {

    @Override
    public void valueChanged(ListSelectionEvent e) {
        text.setText((String) list.getSelectedValue());
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM