gpt4 book ai didi

java - 如何在JTable中获取图像图标

转载 作者:行者123 更新时间:2023-12-02 00:31:06 26 4
gpt4 key购买 nike

我已经创建了 JTable。我从 DB 获取值到 JTable 。我在单击行时遇到问题,它显示图像图标路径。无法显示图像。我如何设置图像。

还有一件事,我有两个图像 1. 指针图像 2. 空白图像 在运行时,默认情况下第一行中的指针图像,之后我单击了第五行,第一行应设置为空白图像,第5行应该设置指针图像。我不知道它在 DefaultCellRender 中是否可能。

package ex10012012;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;


public class frame10012012 extends javax.swing.JFrame {

private DefaultTableModel TmpFlxMdl;
private TableColumn TmpFlxColumn;
private int TmpRow,TmpFlxDRow;
private TableColumn TmpFlxCol;
private Statement TmpFlxSt=null;
private ResultSet TmpFlxDRs=null;
GENLMODL TmpGenModl= new GENLMODL();
FrmM01011 TmpFrmM0101 = new FrmM01011();

public frame10012012() {
initComponents();
}

private void FlxDMouseClicked(java.awt.event.MouseEvent evt) {

if( evt.getClickCount()==2)
{
setVisible(false);
FlxD.setRowSelectionAllowed(true);
int row=FlxD.getSelectedRow();
int modelRow = FlxD.convertRowIndexToModel(row);
Object oc= FlxD.getModel().getValueAt(modelRow, 1);
String TempStr=oc.toString();
TmpFlxMdl =(DefaultTableModel)FlxD.getModel();
try{
}
catch(Exception e){
System.out.println(e);
}
TmpFrmM0101.setVisible(true);

}
else{
FRM_FLXD_PTR_DATA_ASSGN(FlxD.getSelectedRow());
}
}

public void Frm_FlxD_Database() {
try{
TmpRow=0;
TmpGenModl.PRJ_DB_CONNECTION_ASSGN();
TmpFlxMdl =(DefaultTableModel)FlxD.getModel();
TmpFlxDRow = 2;

TmpFlxSt=TmpGenModl.GContcn.createStatement();
TmpFlxDRs=TmpFlxSt.executeQuery("SELECT * from ActivityMaster");
PRJ_FLX_DEFTL_ASSGN(FlxD, "BEGIN");
TmpFlxDRs.first();
do {
FlxD.setValueAt(TmpFlxDRs.getString("ActvtyDesc"), TmpRow,1);
FlxD.setValueAt(TmpFlxDRs.getString("ActvtyCode"), TmpRow,2);
FlxD.setValueAt(TmpFlxDRs.getString("ActvtyDispStatus"), TmpRow,3);
FlxD.setValueAt(TmpFlxDRs.getString("ActvtyId"), TmpRow,4);
TmpFlxMdl.addRow(new Object[]{""});
TmpRow = TmpRow + 1;
}while(TmpFlxDRs.next());
FRM_FLXD_PTR_DATA_ASSGN(TmpFlxDRow);
}
catch(Exception e){
System.out.println(e);
}
}

private void PRJ_FLX_DEFTL_ASSGN(JTable PFlx, String POptnStr){
int TmpRow;
DefaultTableModel TmpFlxMdl = new DefaultTableModel();
TmpFlxMdl = (DefaultTableModel) PFlx.getModel();

if (POptnStr == "BEGIN") {
for (TmpRow=2; TmpRow<=TmpFlxMdl.getRowCount(); TmpRow++) {
TmpFlxMdl.removeRow(TmpRow);
}

}else if(POptnStr == "END"){
TmpRow = TmpFlxMdl.getRowCount();
TmpFlxMdl.removeRow(TmpRow);
}
}

private void FRM_FLXD_PTR_DATA_ASSGN(int PFlxRow) {
//System.out.println(TmpFlxDRow);
TmpFlxDRow = PRJ_FLX_PTR_ASSGN(FlxD, PFlxRow, TmpFlxDRow);
}

private int PRJ_FLX_PTR_ASSGN(JTable PFlx, int PCurRow, int PPrvRow){
ImageIcon TmpIcon;
System.out.println(PCurRow);
System.out.println(PPrvRow);

if (PCurRow != PPrvRow){
TmpIcon = new ImageIcon(getClass().getResource("Blank.gif"));
PFlx.setValueAt(PCurRow,0);
}
TmpIcon = new ImageIcon(getClass().getResource("Pointer.gif"));
PFlx.setValueAt(TmpIcon, PCurRow,0);

return(PCurRow);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
frame10012012 TmpFrame= new frame10012012();
TmpFrame.Frm_FlxD_Database();
TmpFrame.setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton CmdAdd;
private javax.swing.JButton CmdBack;
private javax.swing.JButton CmdDelete;
private javax.swing.JTable FlxD;
private javax.swing.JLabel Lbl01;
private javax.swing.JLabel Lbl02;
private javax.swing.JRadioButton Rbtn01;
private javax.swing.JTextField Txt01;
private javax.swing.JTextField Txt02;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}

最佳答案

您可以输入 Icon/ImageIcon直接到单元格,因为 JTable知道 ColumnClass 中此对象的类型,与String、Number、Boolean 和 e.i. 相同。

关于java - 如何在JTable中获取图像图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9062747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com