如何从带有图像图标的jlist中获取选定的元素文本?

How to get selected element text from jlist with image icon?

Error Image Using Object 我试图在 jlist 的 jtextfield 中显示选定的元素文本。该列表包含数据库数据和图像,其中从列表中获取选定值后会引发错误。

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: hive.test.ImgsNText cannot be cast to java.lang.String

我的代码

String index = String.valueOf(jList1.getSelectedIndex());
jTextField1.setText(index);
String s = (String) jList1.getSelectedValue();
jTextField2.setText(s);

获取数据库数据并在jlist-code中显示

try {
        Class.forName("org.apache.hive.jdbc.HiveDriver");
        Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "arunachalam", "");
        Statement st = con.createStatement();
        String sql = "show databases";
        ResultSet rs = st.executeQuery(sql);

        while (rs.next()) {
            String s1 = rs.getString(1);
            dm.addElement(new ImgsNText(s1, new ImageIcon("images/hive_db.png")));
        }
        jList1.setCellRenderer(new Renderer());
        jList1.setModel(dm);
        if(jList1.isSelectedIndex(0))
        {

        }
    } catch (Exception e) {
        showMessageDialog(null, "Exception");
    }

建议我如何从 jlist 中单独获取文本并将其显示在 jtextfield 中。

首先您必须创建一个 ListCellRenderer,用于在 List 中显示图像和文本。然后调用列表上的 ListSelectionListener 捕获列表上的单击事件 selection.Then 获取所选项目并在文本字段中设置所选项目的文本。

更新代码

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ListExample implements ListSelectionListener {

  JList list;
  DefaultListModel listModel;
  JTextField txtField;
  Connection connection;
  PreparedStatement pst;
  ResultSet rs;

  public ListExample() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("LIST");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container con = frame.getContentPane();

    ListRenderar rendrar = new ListRenderar();
    list = new JList();
    txtField = new JTextField(10);

    listModel = new DefaultListModel();
    try {
      Class.forName("com.mysql.jdbc.Driver");
      connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ganesh", "root",
          "admina");
      pst = connection.prepareStatement("show databases");
      rs = pst.executeQuery();
      while (rs.next()) {
        String text = rs.getString(1);
        listModel.addElement(new ImgNText(text, new ImageIcon("/images/help.gif")));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    list.setModel(listModel);

    list.setCellRenderer(rendrar);
    list.addListSelectionListener(this);

    con.add(list);
    con.add(txtField);

    con.setLayout(new FlowLayout());

    frame.setVisible(true);
    frame.pack();
  }

  public class ListRenderar extends JLabel implements ListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index,
                                                  boolean isSelected, boolean cellHasFocus) {

      ImgNText imgNtext = (ImgNText) value;
      ImageIcon icon = imgNtext.getImage();
      String name = imgNtext.getText();

      setIcon(icon);
      setText(name);

      return this;
    }
  }

  public void valueChanged(ListSelectionEvent e) {
    ImgNText obj = (ImgNText) list.getSelectedValue();
    txtField.setText(obj.getText());
  }

  public class ImgNText {

    ImageIcon image;
    String text;

    public ImgNText(String text, ImageIcon image) {
      this.image = image;
      this.text = text;
    }

    public ImageIcon getImage() {
      return image;
    }

    public String getText() {
      return text;
    }
  }

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