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
Win a copy of
Spring Boot in Practice
this week in the
Spring
forum!
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
Tim Cooke
Ron McLeod
Jeanne Boyarsky
Paul Clapham
Sheriffs:
Liutauras Vilda
Henry Wong
Devaka Cooray
Saloon Keepers:
Tim Moores
Stephan van Hulst
Tim Holloway
Al Hobbs
Carey Brown
Bartenders:
Piet Souris
Mikalai Zaikin
Himai Minh
Forum:
Swing / AWT / SWT
JTable
Atit Shah
Greenhorn
Posts: 1
posted 18 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 18 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()); } } }
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
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...