Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes MultiLine JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MultiLine JTable" Watch "MultiLine JTable" New topic
Author

MultiLine JTable

Robin Gray
Greenhorn

Joined: Aug 28, 2002
Posts: 8
Hi there ,
I am despertaly trying to implement a MultiLine JTable.
I found some code on http://share.runtime-collective.com/~guillaume/java/multilineCells/MultilineCells.html which works great except if you initailise the table with text that is longer than the cell width .
Can anyone see why this is happening, also I have noticed that if I click on the first cell and insert a SPACE it displays the rest of the text (but only if you click on that cell first and not on any others)
Thanks in advance
Robin

Source Code :
// MultLineCellExample.java

public class MultiLineCellExample {
public static void main(String[] args) {
JFrame f = new JFrame();
DefaultTableModel dm = new DefaultTableModel() {
public Class getColumnClass(int columnIndex) {
return String.class;
}
};
dm.setDataVector(new Object[][]{{"aa TEST TEST TEST TEST TEST TEST TEST TEST END","bb","cc"},
{"A\nA","B\nB","C\nC"}},
new Object[]{"1","2","3"});
JTable table = new JTable( dm );
MultiLineCellEditor editor = new MultiLineCellEditor(table);
table.setDefaultEditor(String.class,editor);
dm.fireTableRowsInserted(0,0);
JScrollPane scroll = new JScrollPane( table );
f.getContentPane().add( scroll );
f.setSize( 400, 400 );
f.setVisible(true);
}
}
// MultiLine Cell Editor

public class MultiLineCellEditor extends AbstractCellEditor implements TableCellEditor {
MyTextArea textArea;
JTable table;
public MultiLineCellEditor(JTable ta) {
super();
table = ta;
// this component relies on having this renderer for the String class
MultiLineCellRenderer renderer = new MultiLineCellRenderer();
table.setDefaultRenderer(String.class,renderer);
textArea = new MyTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
for(int i=0;i<table.getRowCount();i++) updateRow(i);
}
/** This method determines the height in pixel of a cell given the text it contains */
private int cellHeight(int row,int col) {
if (row == table.getEditingRow() && col == table.getEditingColumn())
return textArea.getPreferredSize().height;
else
return table.getDefaultRenderer(String.class).getTableCellRendererComponent(table,
table.getModel().getValueAt(row,col),false,false,row,col).getPreferredSize().height;
}
void cellGrewEvent(int row,int column) {
updateRow(row);
}
void cellShrankEvent(int row,int column) {
updateRow(row);
}
void updateRow(int row) {
int maxHeight = 0;
for(int j=0;j<table.getColumnCount();j++) {
int ch;
if ((ch = cellHeight(row,j)) > maxHeight) {
maxHeight = ch;
}
}
table.setRowHeight(row,maxHeight);
}
public Object getCellEditorValue() {
return textArea.getText();
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
int row, int column) {
textArea.setText(table.getValueAt(row,column).toString());
textArea.rowEditing = row;
textArea.columnEditing = column;
textArea.lastPreferredHeight = textArea.getPreferredSize().height;
return textArea;
}
class MyTextArea extends JTextArea implements KeyListener {
int lastPreferredHeight = 0;
int rowEditing;
int columnEditing;
MyTextArea() {
addKeyListener(this);
// This is a fix to Bug Id 4256006
addAncestorListener( new AncestorListener(){
public void ancestorAdded(AncestorEvent e){
requestFocus();
}
public void ancestorMoved(AncestorEvent e){}
public void ancestorRemoved(AncestorEvent e){}
});
}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {
if (getPreferredSize().getHeight() > lastPreferredHeight) {
lastPreferredHeight = getPreferredSize().height;
cellGrewEvent(rowEditing,columnEditing);
// this will trigger the addition of extra lines upon the cell growing and
// prevent all the text being lost when the cell grows to the point of requiring
// scrollbars
table.setValueAt(getText(),rowEditing,columnEditing);
}
else if (getPreferredSize().getHeight() < lastPreferredHeight) {
lastPreferredHeight = getPreferredSize().height;
cellShrankEvent(rowEditing,columnEditing);
}
else if (table.getValueAt(rowEditing,columnEditing).equals(""))
table.setValueAt(getText(),rowEditing,columnEditing);
}
}
}
// MultiLine Cell Render

public class MultiLineCellRenderer extends JTextArea implements TableCellRenderer {
public MultiLineCellRenderer() {
setEditable(false);
setLineWrap(true);
setWrapStyleWord(true);
}
public Component getTableCellRendererComponent(JTable table,Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof String) {
setText((String)value);
// set the table's row height, if necessary
//updateRowHeight(row,getPreferredSize().height);
}
else
setText("");
return this;
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MultiLine JTable
 
Similar Threads
MultiLine JTable
Jtable Cell and ENTER key
jtable
JTable Columns
problem in JTable