JTable Ext - GUI problem

Hey,I would like to know if there is a grid/table written in Java that looks like the exceed grid (including clustering, grouping, coluring cells and other stuff)i would like to see some samples, and code...thanks
[241 byte] By [Peleyala] at [2007-11-27 9:10:15]
# 1

I personally dont know of any packages that provide everything that the Xceed Grid component provides, however, everything you need to implement such a component already exists in Java.

Most you basically have to make extensive use of CellRenderers to achieve all the effects Xceed Grid provides. However, here is a starting point. This is a class that provides the grouping functionality for a JTable. Ignore the commented lines. This is actually code from one of my programmes, so you may need to tweak it slightly for it to work. Just set this as you JTable's cell renderer and you are good to go

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.filechooser.*;

import javax.swing.border.*;

import javax.swing.table.*;

import javax.swing.tree.*;

import java.io.*;

import java.sql.*;

import java.util.*;

public class GroupingCellRenderer extends DefaultTableCellRenderer {

JTable table = null;

boolean isOnRow = false;

int currentHighlightRow = -1, sortColumn = 0, titleRow = -1;

Object rowValue = "";

Vector<Integer> sepRows = new Vector<Integer>();

Vector<String> sepTitles = new Vector<String>();

LineSeparator tsep = null;

public GroupingCellRenderer() {

super();

}

public void attachListener() {

table.getTableHeader().addMouseListener( new MouseAdapter() {

public void mouseReleased(MouseEvent e) {

sortColumn = table.columnAtPoint( e.getPoint() );

System.out.println("Sort Column: " + sortColumn);

SwingUtilities.invokeLater( new Runnable() {

public void run() {

setSortColumn(sortColumn);

}

});

}

});

if(table.getRowSorter() == null) {

table.setRowSorter( new TableRowSorter( table.getModel() ) );

}

table.getRowSorter().addRowSorterListener( new RowSorterListener() {

public void sorterChanged(RowSorterEvent e) {

if(e.getType() == RowSorterEvent.Type.SORTED ) {

createRowSeparators();

System.out.println("Seperator Rows Recreated");

} else if(e.getType() == RowSorterEvent.Type.SORT_ORDER_CHANGED ) {

createRowSeparators();

System.out.println("Seperator Rows Recreated");

}

}

});

}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,

boolean hasFocus, int row, int col) {

if(this.table == null) {

this.table = table;

attachListener();

createRowSeparators();

}

String repValue = "";

if(value == null) {

repValue = " ";

} else {

repValue = value.toString();

}

super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);

setText( repValue );

setIcon( new EmptyIcon(1,16) );

setHorizontalAlignment(JLabel.LEFT);

setForeground( table.getForeground() );

if(isOnRow && currentHighlightRow == row) {

setBackground( new Color(220, 240, 250) );

} else {

if(row % 2 == 0) {

setBackground( new Color(232,242,252) );

} else {

setBackground(Color.white);

}

}

if(isSelected) {

setBackground( new Color(51, 153, 255) );

setForeground( Color.white );

}

//if(col == sortColumn && !repValue.equalsIgnoreCase(rowValue.toString())) {

// rowValue = repValue;

// titleRow = row;

//}

if( sepRows.contains(row) ) {

if(col == 0) {

tsep = new LineSeparator( sepTitles.elementAt( sepRows.indexOf(row) ) );

} else {

tsep = new LineSeparator("");

tsep.setBorder( null );

//((JComponent)tsep.getComponent()).setBorder(null);

}

//((JComponent)tsep.getComponent()).setToolTipText( tsep.getTitle() );

//setToolTipText( tsep.getTitle() );

JPanel panel = new JPanel( new BorderLayout() );

panel.setBorder(null);

panel.setBackground(Color.white);

panel.add( tsep );

panel.add( this, BorderLayout.SOUTH );

if(table.getRowHeight(row) != 45) {

table.setRowHeight( row, 45 );

}

return panel;

} else {

if(table.getRowHeight(row) != 20)

table.setRowHeight(row, 20);

}

return this;

}

public void setSortColumn(int column) {

this.sortColumn = column;

createRowSeparators();

}

public void createRowSeparators() {

if(table == null) {

return;

}

sepRows.removeAllElements();

sepTitles.removeAllElements();

Object rv = "";

for(int i = 0; i < table.getRowCount(); i++) {

Object value = table.getValueAt( i, sortColumn);

if(table.getModel().getColumnName(sortColumn).equals("File Path") ) {

value = new File(value.toString()).getParent();

}

if(value != null && !value.equals(rv)) {

sepRows.addElement(i);

sepTitles.addElement(value.toString());

rv = value;

}

}

rv = null;

}

public class LineSeparator extends JPanel {

JLabel label;

UIDefaults defaults = UIManager.getDefaults();

public LineSeparator(String text) {

super( new BorderLayout() );

label = new JLabel(text);

label.setFont( new Font("Verdana", Font.BOLD, 10) );

label.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));

add( label );

setOpaque(false);

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

if(label != null) {

int width = getWidth();

int height = getHeight();

FontMetrics fm = g.getFontMetrics(label.getFont());

int strWidth = (int)fm.getStringBounds(label.getText(), g).getWidth();

//int strWidth = label.getWidth();

int rectWidth = getWidth(), rectHeight = 2;

if(label.getText().length() > 0) {

rectWidth = getWidth() - (strWidth + 10);

}

g.setColor( defaults.getColor("Separator.highlight") );

g.fillRect(width - rectWidth, height/2, rectWidth, 2);

g.setColor( defaults.getColor("Separator.foreground") );

g.fillRect(width - rectWidth, height/2, rectWidth, 1);

}

}

/**

* Overridden for performance reasons.

* See the <a href="#override">Implementation Note</a>

* for more information.

*

* @since 1.5

*/

public void invalidate() {}

public void validate() {}

public void revalidate() {}

public void repaint(long tm, int x, int y, int width, int height) {}

public void repaint(Rectangle r) { }

public void repaint() {

}

}

}

// example application

final GroupingCellRenderer renderer = new GroupingCellRenderer();

renderer.setSortColumn(0);

JTable table = new JTable() {

public TableCellRenderer getCellRenderer(int row, int col) {

return renderer;

}

public boolean isCellEditable(int row, int col) {

return false;

}

};

icewalker2ga at 2007-7-12 21:51:22 > top of Java-index,Desktop,Core GUI APIs...
# 2
can u add other features easily? The most important issue is clustering, and other windows featuers will be great - like draging a column header to change the places of the columons, and other windows styles?
Peleyala at 2007-7-12 21:51:22 > top of Java-index,Desktop,Core GUI APIs...
# 3

> can u add other features easily?

Hey, dont be lazy. ANYTHING can be done in Java.

> The most important issue is clustering

I have no idea what that is and I have no doubt that such a feature can be implemented in just a couple of days of hard work.

> and other windows featuers will be great

You must be using Vista to be talking like this. Besides, Java is not an operating system. It is just a means to an end. If you want windows features that already havent been implemented, then do them your self.

> - like draging a column header to change the places of the columons

Oh and this is already a standard JTable feature. You should read your documentation a bit more often.

> and other windows styles

Finally, Java wasnt designed to be used on only the windows platform, so it doesn't ship with all the necessary goodies you get from third party developers for Windows.

Get to know Java for what it really is and not what you want it to be.

'

ICE

icewalker2ga at 2007-7-12 21:51:22 > top of Java-index,Desktop,Core GUI APIs...