aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Adding a ScrollBar to JTable cells Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding a ScrollBar to JTable cells" Watch "Adding a ScrollBar to JTable cells" New topic
Author

Adding a ScrollBar to JTable cells

Rames Raja
Greenhorn

Joined: Jan 27, 2003
Posts: 7
Hi all,
I am desperate and I need your help asap.
I have a JTable that uses JTextPane as Cell Renderer. This is what I want:
1. I want to have multiline text inside JTextPane
2. I want a scrollbar to scroll down if my text size exceeds my cell height.

Right now, my scroll bar is not moving. Can anyone help me with this ??
Here I am posing all my classes ( 4 of them ). If you compile and run it, you will see JTable cells with scrollbars, but ScrollBar not move when you try to scroll down or up.
Your help is very much appreciated in advance.
Ramesh
This is my main program
-----------------------
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
public class JTableTextPane extends JFrame {

// Member variables
private JPanel topPanel;
private JTable table;
private JScrollPane scrollPane;

/** Creates a new instance of TableTest */
public JTableTextPane() {


// set the frame characteristics
setSize( 300 , 200 );

// Create a panel to hold other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );

// Create a table
table = new JTable( new TextPaneModel() );
//table = new JTable ( dm );
table.setRowHeight( 40 );
table.setEnabled( true );

//table.setEnabled( false );
table.addMouseListener(new JTextPaneMouseListener( table ));

addRenderer();
// add the table to scrollpane
scrollPane = table.createScrollPaneForTable( table );
topPanel.add( scrollPane, BorderLayout.CENTER );

}

private void addRenderer(){

TableColumn c = table.getColumnModel().getColumn( 0 );
c.setCellRenderer( new JTextPaneCellRenderer() );


}

public static void main( String args[] ){

JTableTextPane jtableTextPane = new JTableTextPane();
jtableTextPane.setVisible( true );

}

}

This is my table cell renderer class
-------------------------------------

//Imports
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
import javax.swing.JTextPane.*;
public class JTextPaneCellRenderer
extends JTextPane
implements TableCellRenderer {
/** Creates a new instance of ModifyScheduleCellRenderer */
public JTextPaneCellRenderer() {
}

public java.awt.Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {

setText( value.toString() );
JScrollPane jp = new JScrollPane( this );
return jp;

}


}

This is my mouse Listener class
--------------------------------
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
public class JTextPaneMouseListener implements MouseListener{
private JTable __table;

/** Creates a new instance of JTextPaneMouseListener */
public JTextPaneMouseListener( JTable table ) {

__table = table;
}

private void __forwardEventToButton(MouseEvent e) {
TableColumnModel columnModel = __table.getColumnModel();
int column = columnModel.getColumnIndexAtX(e.getX());
int row = e.getY() / __table.getRowHeight();
Object value;
JTextPane textPane;
MouseEvent textPaneEvent;
MouseEvent jsEvent;
JScrollPane js ;
if(row >= __table.getRowCount() || row < 0 ||
column >= __table.getColumnCount() || column < 0)
return;

value = __table.getCellRenderer( row, column );
if(!(value instanceof JTextPane ))
return;
textPane = (JTextPane)value;
textPaneEvent =
(MouseEvent)SwingUtilities.convertMouseEvent( __table, e, textPane );
textPane.dispatchEvent( textPaneEvent);

// This is necessary so that when a button is pressed and released
// it gets rendered properly. Otherwise, the button may still appear
// pressed down when it has been released.
__table.repaint();

}

public void mouseClicked( MouseEvent e ) {

__forwardEventToButton(e);
}

public void mouseEntered( MouseEvent e ) {

__forwardEventToButton(e);
}

public void mouseExited( MouseEvent e ) {

__forwardEventToButton(e);
}

public void mousePressed( MouseEvent e ) {

__forwardEventToButton(e);
}

public void mouseReleased( MouseEvent e ) {

__forwardEventToButton(e);
}

}

This is my table model class
-----------------------------
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author Administrator
*/
class TextPaneModel extends AbstractTableModel {
private Object[][] __rows = {
{ "One\ntwo\nthree\nOne\ntwo\nthree\nOne\ntwo\nthree\nOne\ntwo\nthree\n"},
{ "One\ntwo\nthree\n" },
{ "One\ntwo\nthree\n"},
{ "One\ntwo\nthree\n" }
};
private String[] __columns = { "ONE" };
public String getColumnName(int column) {
return __columns[column];
}
public int getRowCount() {
return __rows.length;
}
public int getColumnCount() {
return __columns.length;
}
public Object getValueAt(int row, int column) {
return __rows[row][column];
}
public boolean isCellEditable(int row, int column) {
return false;
}
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
}
ali rafiq
Ranch Hand

Joined: Jan 09, 2003
Posts: 75
createScrollPaneForTable
public static JScrollPane createScrollPaneForTable(JTable aTable)
Deprecated. As of Swing version 1.0.2, replaced by new JScrollPane(aTable).
Equivalent to new JScrollPane(aTable).
-------------------------------------------
i got this at the api..in here
so this method has been deprecated now u can use it like this... its api said...
scrollPane = new JScrollPane(table);
//scrollPane = table.createScrollPaneForTable( table );
topPanel.add( scrollPane, BorderLayout.CENTER );


if u learn to break the rock .. next day u will be looking to Mountain.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Adding a ScrollBar to JTable cells