Note that there are some explanatory texts on larger screens.

plurals
  1. POImages in JTable cells off by one pixel?
    text
    copied!<p>So, I'm able to load images into my JTable's cells now, but for some reason the graphics are all shifted to the right by one pixel, allowing me to see the JTable's background. Any ideas? Sorry if my formatting's off; still not entirely used to this markup.</p> <pre><code>public static void main(String[] args) { final int rows = 16; final int columns = 16; final int dimTile = 32; JFrame frame = new JFrame("test"); JTable table = new JTable(rows, columns); table.setIntercellSpacing(new Dimension(0, 0)); table.setShowGrid(false); table.setBackground(Color.cyan); table.setTableHeader(null); table.setRowHeight(dimTile); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setPreferredSize(new Dimension(rows * dimTile, columns * dimTile)); Tile tile = new Tile(0); for(int i = 0; i &lt; rows; i++) { for(int j = 0; j &lt; columns; j++) { table.getColumnModel().getColumn(j).setCellRenderer(new MyRenderer()); table.setValueAy(tile, i, j); } } JScrollPane scrollPane = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setBorder(BorderFactory.createEmptyBorder()); frame.getContentPane().add(scrollPane); frame.setSize(512, 512); frame.setVisible(true); int adjustedSizeX = frame.getInsets().left + frame.getInsets().right + 512; int adjustedSizeY = frame.getInsets().top + frame.getInsets().bottom + 512; frame.setSize(adjustedSizeX, adjustedSizeY); frame.pack(); ... } public class MyRenderer extends DefaultTableCellRenderer { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); Tile tile = (Tile) value; setIcon(tile.getIcon()); return this; } } public class Tile { ImageIcon icon; public Tile(int graphic) { icon = new ImageIcon(PATH/TO/"...test.png"); } public ImageIcon getIcon() { return icon; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload