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

4

I need to do something like this:

AtthispointIhaveatablelikeImage1,butIwantedtocreateatablelikeImage2.HowcanIcreatesomethinglikethis?Anyideas?

ThemostsimilarexampleIsawwas: link

[EDIT]

To create the multi-lines I used this class "AttributiveCellTableModel" that is in the site: link

With this code, I was able to implement the multi-lines, using a cycle that runs through 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 wanted to add two things:

  • Add column names
  • And remove vertical lines

Can someone help me?

    
asked by anonymous 30.07.2014 / 10:38

1 answer

1

To hide the vertical lines of your table, simply use the setShowVerticalLines() method of your JTable object passing the false argument.

tabela.setShowVerticalLines(false);

To show the header you should put the JTable inside a JScrollPane.

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

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

private JScrollPane scroll;
private JPanel contentPane;
private JTable tabela;
    
06.08.2014 / 13:10