Boa tarde pessoal…
poderiam me ajudar com este código? eu encontrei em uma página de um fórum da Sun se não me engano.
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.plaf.basic.*;
import java.awt.*;
public class CTest
{
public static void main(String args[])
{
JFrame jf=new JFrame("Table with cell spanning");
CMap m=new CMap1();
TableModel tm=new DefaultTableModel(15,20);
jf.getContentPane().add(new JScrollPane(new CTable(m,tm)));
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setSize(500,500);
jf.setVisible(true);
}
}
interface CMap
{
int span (int row, int column);
int visibleCell(int row, int column);
}
class CTable extends JTable {
public CMap map;
public CTable(CMap cmp, TableModel tbl) {
super(tbl);
map=cmp;
setUI(new CTUI());
setValueAt(" Daniel",3,3);
setValueAt("l",5,5);
}
public Rectangle getCellRect(int row, int column, boolean includeSpacing){
// required because getCellRect is used in JTable constructor
if (map==null) return super.getCellRect(row,column, includeSpacing);
// add widths of all spanned logical cells
int sk=map.visibleCell(row,column);
Rectangle r1=super.getCellRect(row,sk,includeSpacing);
if (map.span(row,sk)!=1)
for (int i=1; i<map.span(row,sk); i++){
r1.width+=getColumnModel().getColumn(sk+i).getWidth();
}
return r1;
}
public int columnAtPoint(Point p) {
int x=super.columnAtPoint(p);
// -1 is returned by columnAtPoint if the point is not in the table
if (x<0)
return x;
int y=super.rowAtPoint(p);
return map.visibleCell(y,x);
}
}
class CMap1 implements CMap
{
public int span(int row, int column) {
if ((row==3) &&(column==3)) return 6;
if ((row==4)&& (column==7)) return 2;
if ((row==9)&&(column==5)) return 3;
return 1;
}
public int visibleCell(int row, int column) {
if ((row==3)&& (column>=3)&&(column<9))
return 3;
if ((row==4)&&(column>=7)&&(column <9))
return 7;
if ((row==9)&&(column>=5)&&(column<8))
return 5;
return column;
}
}
class CTUI extends BasicTableUI
{
public void paint(Graphics g, JComponent c) {
Rectangle r=g.getClipBounds();
int firstRow=table.rowAtPoint(new Point(0,r.y));
int lastRow=table.rowAtPoint(new Point(0,r.y+r.height));
// -1 is a flag that the ending point is outside the table
if (lastRow<0)
lastRow=table.getRowCount()-1;
for (int i=firstRow; i<=lastRow; i++)
paintRow(i,g);
}
private void paintRow(int row, Graphics g)
{
Rectangle r=g.getClipBounds();
for (int i=0; i<table.getColumnCount();i++)
{
Rectangle r1=table.getCellRect(row,i,true);
if (r1.intersects(r)) // at least a part is visible
{
int sk=((CTable)table).map.visibleCell(1,1);
paintCell(row,sk,g,r1);
// increment the column counter
i+=((CTable)table).map.span(row,sk)-1;
}
}
}
private void paintCell(int row, int column, Graphics g,Rectangle area)
{
int verticalMargin = table.getRowMargin();
int horizontalMargin = table.getColumnModel().getColumnMargin();
Color c = g.getColor();
g.setColor(table.getGridColor());
g.drawRect(area.x,area.y,area.width-1,area.height-1);
g.setColor(c);
area.setBounds(area.x + horizontalMargin/2,
area.y + verticalMargin/2,
area.width - horizontalMargin,
area.height - verticalMargin);
if (table.isEditing() && table.getEditingRow()==row && table.getEditingColumn()==column)
{
Component component = table.getEditorComponent();
component.setBounds(area);
component.validate();
}
else
{
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component component = table.prepareRenderer(renderer, row, column);
if (component.getParent() == null)
rendererPane.add(component);
rendererPane.paintComponent(g, component, table, area.x, area.y,
area.width, area.height, true);
}
}
}
Ele faz o merge das células, porém não inseri o valor no designer.
acredito que o problema seja algo na classe CTUI.
Alguém poderia me ajudar por favor??
Obrigado!