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

Posted on

Question :

I need to do something like this:


ThemostsimilarexampleIsawwas: link


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

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.


 AttributiveCellTableModel ml=new AttributiveCellTableModel(cont,4);
    final MultiSpanCellTable table = new MultiSpanCellTable( ml );

    table.setIntercellSpacing(new Dimension(2,2));

    table.setSize(400, 400);
    table.setLocation(10, 10);
    this.getContentPane().add(new JScrollPane(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()) {

                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);
         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
         for (int i = 0; i < ml.getRowCount(); i++) {

             if(ml.getValueAt(i, 0).equals(entry.getKey())){
                     System.out.println("vorrrraasd: "+entry.getValue());
                     int[] intervalo = new int[entry.getValue()];
                     for(int xx=0, y=i; y<i+entry.getValue();y++, xx++){
                         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?


Answer :

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


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;


Leave a Reply

Your email address will not be published. Required fields are marked *