4
I need to do something like this:
Right now I have a table like image 1, but I wanted to create a table like image 2. How can I create something like this? Any ideas?
The closest example I saw was: http://www.java2s.com/Code/Java/Swing-Components/MultiLineCellExample.htm
[EDIT]
To create the multi-lines I used this class "Attributivecelltablemodel" found on the site: http://www.java2s.com/Code/Java/Swing-Components/MultiSpanCellTableExample.htm
With this code, I was able to implement multi-lines, using a cycle that runs through a Map<String,Map<String,Obejct>>
, the first key corresponds to the first column and the second map corresponds to the rest.
The "Attributivecelltablemodel" and a Defaulttablemodel.
Code:
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");
}
}
}
}
That returns me this table:
Now I’d like to add two things:
- Add the column names
- And remove the vertical lines
Someone can help me?
I’d tried those codes before, but I tried them again, and I still couldn’t. I think when it comes to vertical lines the problem is that the Attributivecelltablemodel class implements the Paint method to draw the cells I think that should be it...
– jsantos1991
As for the name of the columns all the examples I saw have code like yours but still I could not solve
– jsantos1991
So I will have to analyze the code you put in the link, because there are some modifications behind the scenes that should be causing these undesirable effects. If your code isn’t too big you could post it full in the body of your question, if it’s big you can leave the link the way it is.
– Math
It is still a little big yes, if you can try to take a look it was very good, but if it is necessary I put, because I am having many problems, I really wanted to put in my program a table like this but it is not easy.
– jsantos1991