Search code examples
javaswingjtable

How could I minimize the length between columnA1[] and columnA2[] so that it looks like a nested table?


I would like to form a nested table but it looks like there are huge spaces between those columns. Kindly appreciate any hlp or comments

public class NextDayDeliveryTable {



    JFrame frame;
    public NextDayDeliveryTable(){
        frame=new JFrame();


        String columnA1[]={"Document (below 2kg)","Parcel (above 2kg)"};
        String columnA2[]={"First 500gm","Subsequent 250gm","2.001-2.5kg","Subsequent 500gm"};
        String data1[][]={ {"4.90","0.80","10.50","0.50"},
                {"5.40","1.00","16.00","2.00"},{"6.90","1.50","21.00","3.00"},{"7.40","1.50","26.00","3.50"},{"7.90","2.00","31.00","4.00"},
        };
        String data0[][]={};



        JTable jt2=new JTable(data0,columnA1);
        JTable jt=new JTable(data1,columnA2);


        JScrollPane sp2=new JScrollPane(jt2);
        JScrollPane sp=new JScrollPane(jt);

        frame.add(sp2,BorderLayout.NORTH);
        frame.add(sp, BorderLayout.CENTER);
        frame.setSize(600,200);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        new NextDayDeliveryTable();
    }
}

Solution

  • You need to control the preferred size of the first table:

    JTable jt2=new JTable(data0,columnA1);
    jt2.setPreferredScrollableViewportSize(jt2.getPreferredSize());
    

    Now the height of the first table will just be the height of the header.