Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JTable Cells spanning multiple columns or rows

1,356 views
Skip to first unread message

Chris Gow

unread,
Aug 21, 1999, 3:00:00 AM8/21/99
to
Hi All:

Can anyone point me to any code, or samples that show how I would merge
a number of cells in a JTable together?

Thanks in Advance

Chris Gow
g...@ocas.on.ca

David Lilljegren

unread,
Aug 21, 1999, 3:00:00 AM8/21/99
to
Hi,

The code below is an example of a TableCellRenderer displaying two values in
one column. I'm not sure if this is what you meant but I hope it give you
some help.

The same technique can be used to display "multi row" cells the only problem
is that JTable doesn't support different heights on different rows so all
rows have to be as high as the highest.

Best Regards
/David Lilljegren

public class FastOrderRenderer extends JPanel implements TableCellRenderer{
protected Color focusBGColor;
protected Border editBorder;

protected JLabel price;
protected JLabel volume;
public FastOrderRenderer(){
file://super(BoxLayout.X_AXIS);
super(new GridLayout(1,2));
this.focusBGColor = TablerCellRenderer.defaultFocusBGColor;
this.editBorder = TablerCellRenderer.defaultEditBorder;
add(price = new JLabel());
add(volume = new JLabel());
file://price.setFont(Settings.getTableFont());
file://volume.setFont(Settings.getTableFont());
price.setHorizontalAlignment(JLabel.RIGHT);
volume.setHorizontalAlignment(JLabel.RIGHT);
}

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


file://System.out.println(value);
CellDisplayControl cellDisplayController = (CellDisplayControl)
table;
setFont(table.getFont());

if (hasFocus)
setBackground(focusBGColor);
else if (isSelected)
setBackground(cellDisplayController.getSelectionBackground(row,column));
else
setBackground(cellDisplayController.getBackgroundColor(row,column));

setValue(value);

Color fColor = cellDisplayController.getCellForegroundColor(row,
column);
setForeground(fColor);

file://Border if the cell is editable
if (table.isCellEditable(row, column)){
price.setBorder(editBorder);
volume.setBorder(editBorder);
}
else{
price.setBorder(null);
volume.setBorder(null);
}

return this;
}


kosul...@elandtech.com

unread,
Aug 26, 1999, 3:00:00 AM8/26/99
to
I think you will find examples of what you want on www.codeguru.com/java
K.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

Uday Prajapati

unread,
Aug 27, 1999, 3:00:00 AM8/27/99
to

kap...@gmail.com

unread,
May 7, 2015, 4:33:55 AM5/7/15
to
21 Ağustos 1999 Cumartesi 10:00:00 UTC+3 tarihinde Chris Gow yazdı:
> Hi All:
>
> Can anyone point me to any code, or samples that show how I would merge
> a number of cells in a JTable together?
>
> Thanks in Advance
>
> Chris Gow
> g...@ocas.on.ca

Merhaba!
Aşağıda verececeğim satır tabanlı hücre birleştirme yapan kod belki işine yarar.
Bu kodu günlerce uğraşarak ayarlayabildim.

kolay gelsin.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicTableUI;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import com.sun.java.swing.plaf.nimbus.TableEditorPainter;

public class hücre_birleştirme_8_1 {
JScrollPane jKayanPencere;
static int k=0;
static JTable jTablo;

static void Bileşenleri_Ayarla(){
JFrame İletişim_Kutusu = new JFrame("Hücreleri Birleştir");
İletişim_Kutusu.setSize(450, 300);
İletişim_Kutusu.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // DISPOSE_ON_CLOSE: Sadece etkin penceriyi kapatacak şekilde ayarla.

Container Taşıyıcı = İletişim_Kutusu.getContentPane();
İletişim_Kutusu.setLocationByPlatform(true);

// İletişim nesnelerini tanımla ve ayarla.
jTablo=new GenişletilmişTablo(new DefaultTableModel(7, 4));
jTablo.setCellSelectionEnabled(true);
jTablo.setPreferredScrollableViewportSize(new Dimension(430,250));

JScrollPane jKayanPencere=new JScrollPane(jTablo);
Taşıyıcı.add(jKayanPencere, BorderLayout.CENTER);

İletişim_Kutusu.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

İletişim_Kutusu.setVisible(true);
}

public static void main(String[] args) {
//Programı olay gönderme kanalında çalıştır.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Bileşenleri_Ayarla();
}
});
}
}


class GenişletilmişTablo extends JTable {
protected static int StrSay;
protected static int StnSay;

public GenişletilmişTablo (TableModel bicim){
super(bicim); //Tablo biçimini tanımla.
setUI(new HucreGenisletmeAY()); //Genişletilmiş UI sınıfını belirle.

setRowHeight(25);
setRowHeight(0,45);
setRowMargin(2);
getColumnModel().setColumnMargin(2);
setGridColor(Color.blue);

StrSay=super.getRowCount();
StnSay=super.getColumnCount();
}

//JTable sınıfının getCellRect yöntemini devredışı bırakarak yeniden düzenle.
//Tablo biçimi tanımlandığında kendiliğinden çalışmaktadır.
public Rectangle getCellRect(int str, int stn, boolean kapsam){
Rectangle dortgen;
if ((str<0)||(stn<0)||(getRowCount()<=str)||(getColumnCount()<=stn)){
return dortgen=super.getCellRect(str, stn, kapsam);
}

int es=Etkin_Sütun(str,stn);
dortgen=super.getCellRect(str, es, kapsam);

if (Genişletici(str,es)!=-1){
for (int j=1; j<Genişletici(str,es); j++){
dortgen.width+=getColumnModel().getColumn(es+j).getWidth();
}
}
return dortgen;
}

//Birleşik hücre bölgesini etkinleştiren başlangıç sütun sırasını belirleme yöntemi.
public int Etkin_Sütun(int str, int stn){
if ((str==0)&&(stn>=0)&&(stn<=2)) return 0;
if ((str==1)&&(stn>=1)&&(stn<=2)) return 1;
return stn;
}

//Birleştirilecek hücrelerin başlangıç satır sırasını ve sütun sayılarını belirleme yöntemi.
public int Genişletici(int str, int stn){
if ((str==0)&&(stn==0)) return 3;
if ((str==1)&&(stn==1)) return 2;
return -1;
}
}


class HucreGenisletmeAY extends BasicTableUI {
//BasicTableUI sınıfının paint yöntemini devredışı bırakarak yeniden düzenle.
//Bu yöntemin adı "paint" olmak zorundadır.
public void paint(Graphics g, JComponent c){
Rectangle kesimBoyutlari=g.getClipBounds();

int ilkSatır=table.rowAtPoint(new Point(0, kesimBoyutlari.y));
int sonSatır=table.rowAtPoint(new Point(0, kesimBoyutlari.y+kesimBoyutlari.height));
if (sonSatır<0) sonSatır=table.getRowCount()-1;

for (int i=ilkSatır; i<=sonSatır; i++){
satır_boya(g,i);
}
}

private void satır_boya(Graphics g, int str){
Rectangle dortgen=g.getClipBounds();

for (int j=0; j<table.getColumnCount(); j++){
Rectangle hucreDortgeni=table.getCellRect(str, j, true);

if (hucreDortgeni.intersects(dortgen)){
hucre_boya(g, hucreDortgeni, str, j);
}
}
}

private void hucre_boya(Graphics g, Rectangle boyaAlanı, int str, int stn){
int ArlY=table.getRowMargin();
int ArlX=table.getColumnModel().getColumnMargin();

Color r=g.getColor();
g.setColor(Color.green);
g.drawRect(boyaAlanı.x, boyaAlanı.y, boyaAlanı.width-ArlY, boyaAlanı.height-ArlX);

//g.setColor(Color.blue);
//g.fillRect(boyaAlanı.x+ArlX/2, boyaAlanı.y+ArlY/2, boyaAlanı.width-2*ArlY, boyaAlanı.height-2*ArlX);

boyaAlanı.setBounds(boyaAlanı.x+ArlX/2, boyaAlanı.y+ArlY/2, boyaAlanı.width-ArlX, boyaAlanı.height-ArlY);

if (table.isEditing() && table.getEditingRow()==str &&
table.getEditingColumn()==stn){
Component Bileşen=table.getEditorComponent();
Bileşen.setBounds(boyaAlanı);
Bileşen.validate();
}
else{
TableCellRenderer Görüntüleyici=table.getCellRenderer(str, stn); //Hücre işleyicisini tanımla.
Component Bileşen = table.prepareRenderer(Görüntüleyici, str, stn); //İşleyiciyi hazırla.

if (Bileşen.getParent()==null) rendererPane.add(Bileşen);

rendererPane.paintComponent(g, Bileşen, table, boyaAlanı.x, boyaAlanı.y,
boyaAlanı.width, boyaAlanı.height, true);
}
}
}


Batuhan Şenoğlu

unread,
Mar 9, 2021, 4:10:34 AM3/9/21
to
@kap...@gmail.com
birader yazıcağın kodun anasını sikeyim okurken kör oldum onlar nasıl variable issimleri ananı kızını sikiyim düzgün yaz şunları amq yazdığın kod çalışıyo anlayamıyorum topluluğa böyle mi iş yapılır götünü sikiyim senin
0 new messages