Preciso de fazer algo deste género:
Neste momento tenho uma tabela como a da imagem 1, mas queria criar uma tabela como a da imagem 2. Como posso criar algo desse género? Alguma ideia?
O exemplo mais parecido que vi foi: http://www.java2s.com/Code/Java/Swing-Components/MultiLineCellExample.htm
[EDIT]
Para criar as multi-linhas usei a esta class "AttributiveCellTableModel" que se encontra no site: http://www.java2s.com/Code/Java/Swing-Components/MultiSpanCellTableExample.htm
Com esse código, consegui implementar o multi-linhas, usando um ciclo que percorre um Map<String,Map<String,Obejct>>
, a primeira chave corresponde a primeira coluna e o segundo map corresponde as restantes.
O "AttributiveCellTableModel" e um DefaultTableModel.
Codigo:
AttributiveCellTableModel ml=new AttributiveCellTableModel(cont,4);
final MultiSpanCellTable table = new MultiSpanCellTable( ml );
table.setShowGrid(false);
table.setIntercellSpacing(new Dimension(2,2));
table.setBackground(Color.white);
table.setSize(400, 400);
table.setLocation(10, 10);
this.getContentPane().add(new JScrollPane(table));
this.add(table);
final CellSpan cellAtt =(CellSpan)ml.getCellAttribute();
int LineATUAL=0;
int LineTOTAL=0;
for (Map.Entry<String,Map<String,LinhasOnline>> entry : mapTudo.entrySet()) {
int INTERVALO=0;
for (Map.Entry<String, LinhasOnline> produtos : entry.getValue().entrySet()) {
INTERVALO++;
ml.setValueAt(produtos.getValue().getLinha(), LineATUAL, 0);
ml.setValueAt(produtos.getValue().getProduto(), LineATUAL, 1);
ml.setValueAt(produtos.getValue().getcont(), LineATUAL, 2);
ml.setValueAt(produtos.getValue().getmax(), LineATUAL, 3);
System.out.println("linha:" +LineATUAL);
LineATUAL++;
}
mapSpanCell.put(entry.getKey(), INTERVALO);
}
for (Map.Entry<String, Integer> entry : mapSpanCell.entrySet()) {
System.out.println("nome linha entry" +entry.getKey());
}
for (Map.Entry<String, Integer> entry : mapSpanCell.entrySet()) {
//usadp para mandar parar todos os ciclos em baixo
breakcycle:
for (int i = 0; i < ml.getRowCount(); i++) {
if(ml.getValueAt(i, 0).equals(entry.getKey())){
if(entry.getValue()>1){
System.out.println("vorrrraasd: "+entry.getValue());
int[] intervalo = new int[entry.getValue()];
for(int xx=0, y=i; y<i+entry.getValue();y++, xx++){
intervalo[xx]=y;
System.out.println("valor do Y" +y);
}
int[] columnss = {0};
// int[] rowss = intervalo;
cellAtt.combine(intervalo, columnss);
break breakcycle;
}
else {
System.out.println(" ola");
}
}
}
}
Que me retorna esta tabela:
Agora queria adicionar duas coisas:
- Adicionar o nome das colunas
- E remover as linhas verticais
Alguém me consegue ajudar?