Jtable, add multi-lines[Cell span] +Format table

Asked

Viewed 588 times

4

I need to do something like this:

jtable

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:

tabela

Now I’d like to add two things:

  • Add the column names
  • And remove the vertical lines

Someone can help me?

1 answer

1

To hide the vertical lines of your table just use the method setShowVerticalLines() of your Jtable object passing the argument false.

tabela.setShowVerticalLines(false);

To show the header you must place Jtable inside a Jscrollpane.

scroll = new JScrollPane(tabela);
contentPane.add(scroll, BorderLayout.CENTER);

For the above examples consider that I created the attributes at class level, ie like this:

private JScrollPane scroll;
private JPanel contentPane;
private JTable tabela;
  • 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...

  • As for the name of the columns all the examples I saw have code like yours but still I could not solve

  • 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.

  • 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.