Search code examples
javaswingjtablegraphics2dtablecellrenderer

in JTable remove space between images


In the picture below you see a JTable with two columns.

UPDATE
**The columns are hidden. The red behind the selected row is
background color red only for demonstration purpose
. **

Each row have two pictures.

First picture is a png image looking like a checkbox. Followed by
a png image that the name of the friend is written onto.

The images should look like one image but there is a space.

Is is possible to remove this space?
The images does not have the space

Maybe it's not doable using JTable.
In that case what other swing "list" can do this.

The code that creates the table

        jTableSpriidFriends = new JTable();
        jTableSpriidFriends.setRowMargin(0);
        jTableSpriidFriends.setIntercellSpacing(new Dimension(0, 0));
        jTableSpriidFriends.setShowHorizontalLines(false);
        jTableSpriidFriends.setShowVerticalLines(false);
        jTableSpriidFriends.setShowGrid(false);
        m_adapterSpriidFriends = new AbstractTableModelJTableSpriidFriends();
        rendererSpriidFriends = new CellRendererJtableSpriidFriends();
        jTableSpriidFriends.setModel(m_adapterSpriidFriends);
        jTableSpriidFriends.setDefaultRenderer(ImageIcon.class,
rendererSpriidFriends );
        jTableSpriidFriends.setRowSelectionAllowed(true);
        jTableSpriidFriends.addMouseListener(this);
        jTableSpriidFriends.getSelectionModel().addListSelectionListener(this);
        jTableSpriidFriends.setTableHeader(null);

        scrollPaneSpriidFriends = new JScrollPane();
        scrollPaneSpriidFriends.setBounds(11, 55, 176, 264);
        panelSpriid.add(scrollPaneSpriidFriends);
        scrollPaneSpriidFriends.setViewportView(jTableSpriidFriends);

    for (int i=0; i<m_adapterSpriidFriends.getColumnCount(); i++) {
        TableColumn column = jTableSpriidFriends.getColumnModel().getColumn(i);
        if (i==0) column.setPreferredWidth(50);
        if (i==1) column.setPreferredWidth(120);

    }

Here the DefaultTableCellRenderer

case 0:// type

    if(friend.selected){
       image = new ImageIcon(getClass().getResource
       ("/resources/friendlist/checkbox_checked.png")).getImage();
       this.setIcon( new ImageIcon(image));
    }else{
       image = new ImageIcon(getClass().getResource("/resources
       /friendlist/checkbox_unchecked.png")).getImage();
       this.setIcon( new ImageIcon(image));
    }

    break;
case 1:// files

    BufferedImage old = null;
    try {

         if(friend.deviceType.equals(Consts.DEVICE_TYPE_DEVICE))
        old = ImageIO.read(getClass().getResource
        ("/resources/friendlist/row_with_device.png"));
          else
        old = ImageIO.read(getClass().getResource
        ("/resources/friendlist/row_with_pc.png"));

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

      int w = old.getWidth();
      int h = old.getHeight();
      BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

      Graphics2D g2d = img.createGraphics();
      g2d.drawImage(old, 0, 0, null);
      g2d.setPaint(Color.WHITE);
      //g2d.setFont(new Font("Serif", Font.BOLD, 20));

      String s = friend.name;
      FontMetrics fm = g2d.getFontMetrics();
      int x = 10 ;
      int y = 20;
      g2d.drawString(s, x, y);
      g2d.dispose();


    this.setIcon( new ImageIcon(img));
    break;              
}

enter image description here


Solution

  • Maybe you can just use a JList with custom renderer. The renderer would then take the spriid friend objects and construct appropriate labels using a simple JPanel with GridLayout.