KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones
java,jtable,jtree,swing Return to JAVA table list

Best books about JAVA programming:

JAVA JTable multiple editor in cells (JCheckBox,JComboBox,JTextField)

The following code add multiple editor in cells (JCheckBox,JComboBox,JTextField). This program involes 3 JAVA file:

MultiComponentTable2.java, EachRowEditor.java and EachRowRenderer.java

//Example modified from http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples2.html
//Original example doesn't work
/**
* @version 1.0 12/03/98
*/


/* (swing1.1beta3) */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.util.EventObject;

/**
* @version 1.0 11/09/98
*/
class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {

CheckBoxRenderer() {
setHorizontalAlignment(JLabel.CENTER);
}

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
//super.setBackground(table.getSelectionBackground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelected((value != null && ((Boolean)value).booleanValue()));
return this;
}
}

public class MultiComponentTable2 extends JFrame {

public MultiComponentTable2(){
super("MultiComponent Table");

DefaultTableModel dm = new DefaultTableModel() {
public boolean isCellEditable(int row, int column) {
if (column == 0) {
return true;
}
return false;
}
};
dm.setDataVector(
new Object[][]{
{"true" ,"String" ,"JLabel" ,"JComboBox"},
{"false" ,"String" ,"JLabel" ,"JComboBox"},
{new Boolean(true) ,"Boolean" ,"JCheckBox","JCheckBox"},
{new Boolean(false) ,"Boolean" ,"JCheckBox","JCheckBox"},
{new Boolean(true) ,"Boolean" ,"JCheckBox","JCheckBox"},
{"true" ,"String" ,"JLabel" ,"JTextField"},
{"false" ,"String" ,"JLabel" ,"JTextField"}},
new Object[]{"Component","Data","Renderer","Editor"});

CheckBoxRenderer checkBoxRenderer = new CheckBoxRenderer();
EachRowRenderer rowRenderer = new EachRowRenderer();
rowRenderer.add(2, checkBoxRenderer);
rowRenderer.add(3, checkBoxRenderer);
rowRenderer.add(4, checkBoxRenderer);
JComboBox comboBox = new JComboBox();
comboBox.addItem("true");
comboBox.addItem("false");
JCheckBox checkBox = new JCheckBox();
checkBox.setHorizontalAlignment(JLabel.CENTER);
DefaultCellEditor comboBoxEditor = new DefaultCellEditor(comboBox);
DefaultCellEditor checkBoxEditor = new DefaultCellEditor(checkBox);

JTable table = new JTable(dm);
table.getColumn("Component").setCellRenderer(rowRenderer);

EachRowEditor rowEditor = new EachRowEditor(table);
rowEditor.setEditorAt(0, comboBoxEditor);
rowEditor.setEditorAt(1, comboBoxEditor);
rowEditor.setEditorAt(2, checkBoxEditor);
rowEditor.setEditorAt(3, checkBoxEditor);
rowEditor.setEditorAt(4, checkBoxEditor);
table.getColumn("Component").setCellEditor(rowEditor);
JScrollPane scroll = new JScrollPane(table);
getContentPane().add( scroll );
setSize( 400, 160 );
setVisible(true);
}

public static void main(String[] args) {
MultiComponentTable2 frame = new MultiComponentTable2();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
}
}

 


Sponsored link
Java JTabbedPane
JTabbedPane Preview List
Java JComboBox
JComboBox Preview List
Java JTooltip
JTooltip Preview List
Java JList
JList Preview List
Java JTree
JTree Preview List 1
JTree Preview List 2
JAVA JTable
JTable Preview List 1
JTable Preview List 2
JTable Preview List 3
JTable Preview List 4
JTable Preview List 5
JTable Preview List 6
JTable Preview List 7
JAVA Jslide
JSlide Preview List

 

 

Best books about JAVA programming from Amazon
Professional Java, JDK 5 Edition (Paperback Simply Java Programming: An Application-Driven Tutorial Approach (Paperback) Effective Java Programming Language Guide (Paperback) Head First Java, 2nd Edition (Paperback) Java : An Introduction to Problem Solving and Programming (4th Edition)

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer