Skip to main content

how get Image icon in JTable



i have created JTable. i get the value from DB to JTable . i am having problem while click on row it show the image icon path. cannot show the image. how i can set the image.





one more thing i am having two images 1. Pointer image 2. Blank Image At the run time Pointer image in a first row by defaulty, After that i clicked in the 5th row, the first row should set with Blank Image, and the 5th rows should be set pointer image. and i don't know its possible in 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

}




Comments

  1. You can put Icon/ImageIcon directly to the cell, because JTable knows this Object's type in the ColumnClass, same as String, Number, Boolean and e.i.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex