210 likes | 659 Views
JTable. Prepared by Petar Agov. Jtable – The Basics. Jtable is a GUI element in Java, extending the JPane class . It’s basic purpose is to present data into a table, much like Microsoft Excel Jtable does not store or cache the data, it simply displays it.
E N D
JTable Prepared by PetarAgov
Jtable – The Basics • Jtable is a GUI element in Java, extending the JPane class. • It’s basic purpose is to present data into a table, much like Microsoft Excel • Jtable does not store or cache the data, it simply displays it. • By default, the user can edit the data.(e.g. change entries, move columns around)
How to do it? public Table(){ setLayout(new FlowLayout()); String[] colNames = {"First Name", "Last Name", "ID#", "Standing"}; Object[][] dataset ={ {"Petar", "Agov", "100079581", "Junior"}, {"Pavel", "Agov", "100096587", "Junior"}, {"Elzem", "Emin", "100052369", "Sophomore"}, {"Ivan", "Ivanov", "100075834", "Senior"}, {"Victor","Jubirca", "100029854", "Freshman"}, };
How to do it? (Cont.) table = new JTable(dataset, colNames); - create an instance of the table table.setPreferredScrollableViewportSize(new Dimension(300,50)); table.setFillsViewportHeight(true); JScrollPanescrollPane = new JScrollPane(table); add(scrollPane); }
How to do it? (Cont.) public static void main(String args[]){ Table MyTable = new Table(); MyTable.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyTable.setSize(400,400); MyTable.setVisible(true); }
Special Mentions • The FlowLayout class puts components in a row, sized at their preferred size. • The JScrollPane constructor is invoked with an argument that refers to the table object. This creates a scroll pane as a container for the table; the table is automatically added to the container. • JTable.setFillsViewportHeight is invoked to set the fillsViewportHeight property. When this property is true the table uses the entire height of the container, even if the table doesn't have enough rows to use the whole vertical space. This makes it easier to use the table as a drag-and-drop target.
Special Mentions (cont.) • The scroll pane automatically places the table header at the top of the viewport. The column names remain visible at the top of the viewing area when the table data is scrolled. • If you are using a table without a scroll pane, then you must get the table header component and place it yourself. • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) – ends the project when the table is closed.