Error Image Using Object 我试图在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-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中显示它。
答案 0 :(得分:0)
首先,您必须创建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();
}
}