Java by API/javax.swing.table/TableCellRenderer
extends JSlider implements TableCellRenderer
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class MainClass extends JFrame {
public MainClass() {
super("Customer Editor Test");
setSize(600, 160);
setDefaultCloseOperation(EXIT_ON_CLOSE);
MixerModel test = new MixerModel();
JTable jt = new JTable(test);
jt.setDefaultRenderer(Volume.class, new VolumeRenderer());
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp, BorderLayout.CENTER);
}
public static void main(String args[]) {
MainClass mt = new MainClass();
mt.setVisible(true);
}
}
class MixerModel extends AbstractTableModel {
String headers[] = { "Track", "Start", "Stop", "Left Volume", "Right Volume" };
Class columnClasses[] = { String.class, String.class, String.class, Volume.class, Volume.class };
Object data[][] = { { "Bass", "0:00:000", "1:00:000", new Volume(56), new Volume(56) },
{ "Strings", "0:00:000", "0:52:010", new Volume(72), new Volume(52) },
{ "Brass", "0:08:000", "1:00:000", new Volume(99), new Volume(0) },
{ "Wind", "0:08:000", "1:00:000", new Volume(0), new Volume(99) }, };
public int getRowCount() {
return data.length;
}
public int getColumnCount() {
return headers.length;
}
public Class getColumnClass(int c) {
return columnClasses[c];
}
public String getColumnName(int c) {
return headers[c];
}
public boolean isCellEditable(int r, int c) {
return true;
}
public Object getValueAt(int r, int c) {
return data[r][c];
}
public void setValueAt(Object value, int r, int c) {
if (c >= 3) {
((Volume) data[r][c]).setVolume(value);
} else {
data[r][c] = value;
}
}
}
class Volume {
private int volume;
public Volume(int v) {
setVolume(v);
}
public Volume() {
this(50);
}
public void setVolume(int v) {
volume = (v < 0 ? 0 : v > 100 ? 100 : v);
}
public void setVolume(Object v) {
if (v instanceof String) {
setVolume(Integer.parseInt((String) v));
} else if (v instanceof Number) {
setVolume(((Number) v).intValue());
} else if (v instanceof Volume) {
setVolume(((Volume) v).getVolume());
}
}
public int getVolume() {
return volume;
}
public String toString() {
return String.valueOf(volume);
}
}
class VolumeRenderer extends JSlider implements TableCellRenderer {
public VolumeRenderer() {
super(SwingConstants.HORIZONTAL);
setSize(115, 15);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
if (value == null) {
return this;
}
if (value instanceof Volume) {
setValue(((Volume) value).getVolume());
} else {
setValue(0);
}
return this;
}
}
implements TableCellRenderer
import java.awt.BorderLayout;
import java.awt.ruponent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class MainClass {
public static void main(String args[]) {
String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
String headers[] = { "Upper", "Lower" };
JFrame frame = new JFrame("Label Header");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(rows, headers);
JScrollPane scrollPane = new JScrollPane(table);
Border headerBorder = UIManager.getBorder("TableHeader.cellBorder");
JLabel headerLabel1 = new JLabel(headers[0], JLabel.CENTER);
headerLabel1.setBorder(headerBorder);
JLabel headerLabel2 = new JLabel(headers[1], JLabel.CENTER);
headerLabel2.setBorder(headerBorder);
TableCellRenderer renderer = new JComponentTableCellRenderer();
TableColumnModel columnModel = table.getColumnModel();
TableColumn column0 = columnModel.getColumn(0);
TableColumn column1 = columnModel.getColumn(1);
column0.setHeaderRenderer(renderer);
column0.setHeaderValue(headerLabel1);
column1.setHeaderRenderer(renderer);
column1.setHeaderValue(headerLabel2);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
}
}
class JComponentTableCellRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
return (JComponent)value;
}
}