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:

Mulitple columns editable in JAVA JTable

The following code will set multiple columns in table editable

//All copyright reserved by www.kidslovepc.com
//No redistribution is allowed without author persion and a back link to www.kidslovepc.com
// Example from http://www.kidslovepc.com
//www.kidslovepc.com is not responsible for your data loss or any damage.

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

class RowHeaderRenderer extends JLabel implements ListCellRenderer {
//customize row header.
RowHeaderRenderer(JTable table) {
JTableHeader header = table.getTableHeader();
setOpaque(true);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(CENTER);
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}

public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
setText((value == null) ? "" : value.toString());
return this;
}
}
class MyTableModel extends DefaultTableModel
{
int[] whichColumn;
public MyTableModel(int rowcount,int columncount,int []whichColumn)
{
super(rowcount,columncount);
this.whichColumn =whichColumn;
}
public boolean isCellEditable(int row, int column)
{
int i=0;
boolean isCOL=false;
for(i=0;i<whichColumn.length ;i++)
{
if(column==whichColumn[i])
{
isCOL=true;
break;
}
}
return isCOL;
}
}
public class ColumnEditable extends JList{

public ColumnEditable(String[] headers,JTable table) {
super(headers);

setFixedCellWidth(50);
setFixedCellHeight(table.getRowHeight());
setCellRenderer(new RowHeaderRenderer(table));
}

//set certain column editable

//Row header example
public static void main(String[] args) {
JFrame frame=new JFrame("Row Header Example");
frame.setSize(300, 150);

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

//create a table
String headers[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
int[] colEdited={1,3};
MyTableModel dm = new MyTableModel(headers.length , 4, colEdited);
JTable table = new JTable(dm);

//table header
RowHeaderAll rowHeader=new RowHeaderAll (headers,table);
JScrollPane scroll = new JScrollPane(table);
scroll.setRowHeaderView(rowHeader);
String[] corner={"Order"};

//set corder
scroll.setCorner(JScrollPane.UPPER_LEFT_CORNER ,new RowHeaderAll(corner,table));
frame.getContentPane().add(scroll, BorderLayout.CENTER);
frame.setVisible(true);
}
}


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