Most simple code to populate JTable from ResultSet

前端 未结 10 1027
慢半拍i
慢半拍i 2020-11-22 11:22

I googled the whole day and no luck. I call getnPrintAllData() method after pressing OK button. So the code is:

public class DatabaseSQLiteConne         


        
10条回答
  •  被撕碎了的回忆
    2020-11-22 12:13

    The JTable constructor accepts two arguments 2dimension Object Array for the data, and String Array for the column names.

    eg:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    
    public class Test6 extends JFrame {
    
        public Test6(){     
            this.setSize(300,300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Mpanel m = new Mpanel();
            this.add(m,BorderLayout.CENTER);        
        }
    
    
        class Mpanel extends JPanel {       
    
            JTable mTable;
            private Object[][] cells = {{"Vivek",10.00},{"Vishal",20.00}};
            private String[] columnNames = { "Planet", "Radius" };
            JScrollPane mScroll;
    
            public Mpanel(){
                this.setSize(150,150);
                this.setComponent();
            }
    
            public void setComponent(){
                mTable = new JTable(cells,columnNames);
                mTable.setAutoCreateRowSorter(true);
                mScroll = new JScrollPane(mTable);
    
                this.add(mScroll);
            }
        }
    
        public static void main(String[] args){     
            new Test6().setVisible(true);
        }
    }
    

提交回复
热议问题