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

acruukt9  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(286)

错误图像使用对象我试图在jlist的jtextfield中显示所选元素文本。该列表包含数据库数据和图像,从下面的列表中获取所选值会引发错误。
线程“awt-eventqueue-0”java.lang.classcastexception中出现异常:hive.test.imgsntext无法转换为java.lang.string
我的代码

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

获取数据库数据并以jlist代码显示

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中显示它。

5f0d552i

5f0d552i1#

首先你必须创建一个 ListCellRenderer 用于在中显示图像和文本的 List . 然后打电话给 ListSelectionListener 在捕获列表选择事件的列表上单击。然后获取所选项目并在文本字段中设置所选项目的文本。
更新的代码

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();
  }
}

相关问题