0

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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Erik
  • 5,039
  • 10
  • 63
  • 119
  • Have you tried `myTable.setShowVerticalLines(false);`? Also check out `JTable.setIntercellSpacing(Dimension intercellSpacing)`. – Torious Apr 15 '12 at 18:11
  • The setShowVerticalLines was already set to false but the setIntercellSpacing i forgot. setting setIntercellSpacing(new Dimension(0, 0)); ....does not make any difference. – Erik Apr 15 '12 at 19:04
  • 1
    for better help sooner edit your question with a [SSCCE](http://sscce.org/), for icons you can simply to use `UIManager.getIcon("OptionPane.questionIcon")` – mKorbel Apr 15 '12 at 19:54
  • @mKorbel I dont know what you talking about concerning "OptionPane.questionIcon". Can you clarify? – Erik Apr 15 '12 at 21:30
  • adding SSCCE thanks to @mKorbel – Erik Apr 15 '12 at 22:03

2 Answers2

3

The default cell renderer extends JLabel, and uses itself as component to display the cell. A JLabel has icon and text with a gap in between. Maybe that is the cause:

table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (comp instanceof JLabel) {
            JLabel label = (JLabel)comp;
            label.setIconTextGap(0);
        }
        return comp;
    }

});

Alternatively:

table.setDefaultRenderer(ImageIcon.class, new DefaultTableCellRenderer() {
    {
        setIconTextGap(0);
    }
});
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • interesting will test some..My getColumnClass() in the AbstractTableModel is returning ImageIcon.class – Erik Apr 15 '12 at 23:20
  • could i return something else from the getTableCellRendererComponent and how? The method ends with this.setText(""); ... – Erik Apr 15 '12 at 23:28
  • It is good to realize, that one single component is reused for more than one cell; therefore properties as text and color are set every time again. You should do likewise, but the component is irrelevant. For the code in my answer I will give an alternative. – Joop Eggen Apr 16 '12 at 08:48
3

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.

Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48