4 Replies - 2863 Views - Last Post: 17 August 2012 - 04:37 AM

#1 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

How to display JComponent in JTable

Posted 22 July 2010 - 04:07 PM

Description: Asked many times in the forum. How to display whatever you want in a JTable. Usually it will be JLabel. If you make them setOpaque(true) you can control their background color. You can always control the foreground color, add an icon, .... Also good example on how a TableModel worksUsing a CellRenderer to display whatever you want in a JTable
import java.awt.*;
import java.util.Random;

import javax.swing.*;
import javax.swing.table.*;

public class LabelInTable extends JFrame {

	LabelInTable() {
		setLayout(new BorderLayout());
		JScrollPane sp = new JScrollPane(new MyTable()); 
		add(sp, BorderLayout.CENTER);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	// tester  to display the JTable
	public static void main(String[] argv) {
		LabelInTable lit = new LabelInTable();
		lit.setBounds(10, 10, 350, 100);
		lit.setVisible(true);
	}

	// my class that extends JTable. I need to extend JTable because I also need to implement TableCellRendere
	class MyTable extends JTable implements TableCellRenderer {
		MyTable() {
			// call my father and pass to it my TableModel
			super(new MyModel());
		}

		// this method tell the JTable drawer that it has to call me back to provide
		// the JComponent that will be used to draw the cell
		public TableCellRenderer getCellRenderer(int row, int col) {
			// Call me back for all rows/columns
			return this;
		}

		// this is called to get the Component that will be used to display the cell
		// the 3rd argument says if the cell has the focus or not
		public Component getTableCellRendererComponent(JTable arg0,
				Object component, boolean focus, boolean arg3, int arg4, int arg5) {
			
			return (Component) component;
		}
	}

	// the TableModel for the JTable
	class MyModel extends AbstractTableModel {

		// and array of JComponet that will be the object returned by the tableModel
		JComponent[][] comp = new JComponent[2][2];

		MyModel() {
			// just put some different JComponent in our array
			JLabel label = new JLabel("This is a JLabel");
			JButton button = new JButton("This is a JButton");
			JCheckBox checkBox = new JCheckBox("This is a JCheckBox");
			JRadioButton radio = new JRadioButton("This a JRadioButton");
			// fill our array with different JComponent
			comp[0][0] = label;
			comp[0][1] = button;
			comp[1][0] = checkBox;
			comp[1][1] = radio;
		}
		// return the size of the JLabel array for the number of column
		public int getColumnCount() {
			return comp[0].length;
		}
		// returns the array size for the number of rows
		public int getRowCount() {
			return comp.length;
		}
        // the object displayed at row: X column: Y is the JLabel from my array
		public Object getValueAt(int row, int count) {
			return comp[row][count];
		}

	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to display JComponent in JTable

#2 Mercurial   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 178
  • Joined: 06-November 09

Re: How to display JComponent in JTable

Posted 07 August 2010 - 03:15 AM

Good one! I was wondering why components aren't editable(can't click the button, check the checkbox...)?
Was This Post Helpful? 0
  • +
  • -

#3 Morongo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-August 10

Re: How to display JComponent in JTable

Posted 19 November 2010 - 03:21 AM

I good example of cellrenderer!! It helped me a lot! Morongo
Was This Post Helpful? 0
  • +
  • -

#4 mickyy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-August 12

Re: How to display JComponent in JTable

Posted 15 August 2012 - 09:31 PM

the components are not editable...:O why is that so?
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: How to display JComponent in JTable

Posted 17 August 2012 - 04:37 AM

This snippet is about how to dsplay them not how to edit them. Another snippet would be required for that purpose. The goal of a snippet is to be as short and consice as possible only showing one feature.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1