.
KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

Best books about JAVA programming:

JAVA JTable JList

This example presents how to insert JList into JTable column and also how to adjust row height according to JList's size.

JList uses first column data in Jtable. JTable set new cell editor for first column to JList. Use new cell renderer to adjust row height in JTable.

import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;

import java.util.*;

import java.io.*;
public class jtablejlist //extends JFrame
{

public void init()
{
Vector data = new Vector();
Vector tmp = new Vector();

String[] sdata ={"test1","test2"};
tmp.addElement (sdata);
tmp.addElement ("This is test1");
data.addElement (tmp);

tmp = new Vector();
String[] sdata2 ={"stock1","stock2","stock3","stock4"};
tmp.addElement (sdata2);
tmp.addElement ("This is stocks");
data.addElement (tmp);


Vector cols = new Vector();
cols.addElement ("col1");
cols.addElement ("col2");

JTable table = new JTable(data,cols);
TableColumn column = table.getColumnModel().getColumn(0);
MyListEditor area=new MyListEditor();
// ROWHIGHT=table.getRowHeight() +table.getRowMargin()+1;
column.setCellEditor(area);
column.setCellRenderer(new MyCellRenderer ());

JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(table));
frame.pack();
frame.setVisible(true);
}

class MyListEditor extends AbstractCellEditor implements TableCellEditor
{
JList mlist;

public MyListEditor()
{
mlist = new JList();
}

public Object[] getCellEditorValue()
{
return ((DefaultListModel)mlist.getModel()).toArray();
}

// This method is called when a cell value is edited by the user.
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row,int column)
{
mlist.setListData((Object[])value);
return mlist;
}

}

class MyCellRenderer extends JList implements TableCellRenderer
{
public MyCellRenderer(){
super();
}
public Component getTableCellRendererComponent(JTable mtable, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {

if(isSelected)
setBackground(mtable.getSelectionBackground() );
else
setBackground(Color.white );
String stmp;
StringTokenizer st;
int rowHight=mtable.getRowHeight() +mtable.getRowMargin()+1;
int fold;

String[] mlist = (String[]) value;


fold = mlist.length ;

//adjust row height
if(mtable.getRowHeight(rowIndex)!=fold*rowHight) //otherwise, the save dialog couldn't show normally
mtable.setRowHeight(rowIndex, fold*rowHight);

setListData((String[])value);

return this;
}
}

public static void main(String[] args)
{
jtablejlist jj =new jtablejlist();
jj.init();
}

}


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