Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Devaka Cooray
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Jeanne Boyarsky
Tim Cooke
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Tim Moores
Mikalai Zaikin
Carey Brown
Bartenders:
Forum:
Swing / AWT / SWT
JTable
Atit Shah
Greenhorn
Posts: 1
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
i want to place a jpg file in a particular cell of a jtable at run time. is this possible and if so how? pls reply at the earliest.
Craig Wood
Ranch Hand
Posts: 1535
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.io.*; import java.net.*; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.table.*; public class TableTest { public static void main(String[] args) { String[] headers = { "column 1", "column 2", "column 3", "column 4" }; int rows = 5, cols = 4; String[][] data = new String[rows][cols]; for(int row = 0; row < rows; row++) for(int col = 0; col < cols; col++) { int index = row * cols + col + 1; data[row][col] = "item " + index; } JTable table = new JTable(data, headers); int colIndex = 1; // show image at (row 1, col 1) TableColumn col = table.getColumnModel().getColumn(colIndex); ImageCellRenderer renderer = new ImageCellRenderer(); col.setCellRenderer(renderer); col.setPreferredWidth(renderer.image.getWidth(null)); table.setRowHeight(renderer.image.getHeight(null)); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new JScrollPane(table)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } class ImageCellRenderer extends JLabel implements TableCellRenderer { Image image; public ImageCellRenderer() { loadImage(); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int rowIndex, int colIndex) { if(rowIndex == 1) { setIcon(new ImageIcon(image)); setText(""); } else { setIcon((ImageIcon)null); setText(value.toString()); } return this; } private void loadImage() { String fileName = "images/duke/T3.gif"; try { URL url = getClass().getResource(fileName); image = ImageIO.read(url); } catch(MalformedURLException mue) { System.err.println(mue.getMessage()); } catch(IOException ioe) { System.err.println(ioe.getMessage()); } } }
Normally trees don't drive trucks. Does this tiny ad have a license?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JTable
Jtable
JTable
jtable
JTable
How to use requestFocus() in JTable
JTable
JTable
JTable & Drag and Drop Related Probelm
More...