-1

I have some difficulty developing the following result (as below image)

enter image description here

I want to make this table dynamically / automatically, where I have a list based on a model class and dynamically generate the columns exactly as the image shows, however, listing the days all of a given month.

I am using the codes in this link: http://www.swebb99.f2s.com/GroupableHeader/

Below the modified code where generates the result of the image.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Geizon T.
  • 11
  • 5
  • StackOverflow is not a code writing service. You haven't even asked a question. – Kayaman Sep 09 '19 at 11:51
  • Sorry, friend. My question is how can I change the "dm.setDataVector" snippet to be generated from a list? Thank you – Geizon T. Sep 09 '19 at 11:59
  • Don't paste code as an answer. [Edit the question](https://stackoverflow.com/posts/57853460/edit) and include the **relevant** pieces of code as text in the question itself. – Kayaman Sep 09 '19 at 12:06

1 Answers1

0
package javaapplication4;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

// Steve Webb 16/09/04 swebb99_uk@hotmail.com

public class GroupableColumnExample extends JFrame {

GroupableColumnExample() {
    super( "Multi-Width Header Example" );

    DefaultTableModel dm = new DefaultTableModel();

    dm.setDataVector(new Object[][]{
        {"119","9,99","0,00", "99,00", "77,00"},
        {"911","325,00","59,99", "125,00", "650,00"}},
        new Object[]{"Detalhes","VALOR A","VALOR B", "VALOR A", "VALOR B"});

        // Setup table
        JTable table = new JTable( /*dm, new GroupableTableColumnModel()*/);
        table.setColumnModel(new GroupableTableColumnModel());
        table.setTableHeader(new GroupableTableHeader((GroupableTableColumnModel)table.getColumnModel()));
        table.setModel(dm);

        // Setup Column Groups
        GroupableTableColumnModel cm = (GroupableTableColumnModel)table.getColumnModel();

        ColumnGroup g_token = new ColumnGroup(new GroupableTableCellRenderer(), "01/09/2019");
        g_token.add(cm.getColumn(1));
        g_token.add(cm.getColumn(2));

        ColumnGroup g_token2 = new ColumnGroup(new GroupableTableCellRenderer(), "02/09/2019");
        g_token2.add(cm.getColumn(3));
        g_token2.add(cm.getColumn(4));



        GroupableTableHeader header = (GroupableTableHeader)table.getTableHeader();
        cm.addColumnGroup(g_token);
        cm.addColumnGroup(g_token2);

        // Finish off gui
        JScrollPane scroll = new JScrollPane( table );
        getContentPane().add( scroll );
        setSize( 800, 200 );
}

public static void main(String[] args) {
    GroupableColumnExample frame = new GroupableColumnExample();
    frame.addWindowListener( new WindowAdapter() {
        public void windowClosing( WindowEvent e ) {
            System.exit(0);
        }
    });
    frame.setVisible(true);
}
}

/**
 * Demo renderer just to prove they can be used.
 */
 class GroupableTableCellRenderer extends DefaultTableCellRenderer {
/**
 *
 * @param table
 * @param value
 * @param selected
 * @param focused
 * @param row
 * @param column
 * @return
 */
public Component getTableCellRendererComponent(JTable table, Object value,
boolean selected, boolean focused, int row, int column) {
    JTableHeader header = table.getTableHeader();
    if (header != null) {
        setForeground(Color.WHITE);
        setBackground(Color.RED);
    }
    setHorizontalAlignment(SwingConstants.CENTER);
    setText(value != null ? value.toString() : " ");
    setBorder(UIManager.getBorder("TableHeader.cellBorder"));
    return this;
}
}
Geizon T.
  • 11
  • 5