javaswingjtablejscrollpaneevent-dispatching

JTable Columns not appearing


I am trying to display a simple jtable in frame but It is not displaying the columns of the table, I followed the example from the oracle tutorials but there is something I am not doing right. The program is compiling and running. Here is the code:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class SimpleTableDemo extends JPanel 
{

    SimpleTableDemo()
    {
        String[] columnNames = {"#",
                "Repayment Dates",
                "Principle Amount",
                "Interest",
                "Comission fee",
                "Total installement amount", 
                "Principle balance"};

//      BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN);
        //each line of the two dimentional array is a line in the table
        Object[][] data = {
                {new Integer(1), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(2), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(3), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(4), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(5), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(6), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(7), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(8), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(9), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(10), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(11), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(12), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(13), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(14), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(15), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(16), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(17), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
                {new Integer(18), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
            };


        JTable table = new JTable(data, columnNames);

//      JScrollPane scrollPane = new JScrollPane(table);
//      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//      scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);    
        table.setFillsViewportHeight(true);
        this.add(table);
    }

    public static void main(String[] args)
    {
        JFrame application = new JFrame();
        SimpleTableDemo panel = new SimpleTableDemo();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//      application.setSize(730,650);
        application.pack();
        application.setLocationRelativeTo(panel);
//      application.setResizable(false);
        application.setVisible(true);
    }
}

Solution

  • Table columns are only automatically displayed if the JTable is inside a JScrollPane. Instead of this.add(table);, try this.add(new JScrollPane(table));

    Actually, you seem to have some scrollpane-related code lying about already - just uncomment JScrollPane scrollPane = new JScrollPane(table); and change this.add(table); to this.add(scrollpane);