• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MultiLine JTable

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic