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 fixed column

The following code demos how to fix the first 2 columns in JTtable.


//The example in following link doesn't work
//Example is modified from http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples1.html
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;

public class FixedColumnExample extends JFrame {
Object[][] data;
Object[] column;
JTable fixedTable,table;

public FixedColumnExample() {
super( "Fixed Column Example" );
setSize( 400, 150 );

data = new Object[][]{
{"Tom","Swa","A","","","","",""},
{"Ludi","Mond","","B","","","",""},
{"OKie","Dokie","","","C","","",""},
{"Sport","Kid","","","","D","",""},
{"Fetch","Bring","","","","","E",""},
{"Potter","Harry","","","","","","F"}};
column = new Object[]{"FirstFixed","LastFixed","a","b","c","d","e","f"};

AbstractTableModel fixedModel = new AbstractTableModel() {
public int getColumnCount() { return 2; }
public int getRowCount() { return data.length; }
public String getColumnName(int col) {
return (String)column[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
};
AbstractTableModel model = new AbstractTableModel() {
public int getColumnCount() { return column.length -2; }
public int getRowCount() { return data.length; }
public String getColumnName(int col) {
return (String)column[col +2];
}
public Object getValueAt(int row, int col) {
return data[row][col+2];
}
public void setValueAt(Object obj, int row, int col) {
data[row][col +2] = obj;
}
public boolean CellEditable(int row, int col) {
return true;
}
};

fixedTable = new JTable( fixedModel );

table = new JTable( model );

fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

JScrollPane scroll = new JScrollPane( table );
JViewport viewport = new JViewport();
viewport.setView(fixedTable);
viewport.setPreferredSize(fixedTable.getPreferredSize());
scroll.setRowHeaderView(viewport);
scroll.setCorner(JScrollPane.UPPER_LEFT_CORNER,fixedTable.getTableHeader());

getContentPane().add(scroll, BorderLayout.CENTER);
}

public static void main(String[] args) {
FixedColumnExample frame = new FixedColumnExample();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
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