Java tutorial
/* * Copyright 2012 Eng Kam Hon (kamhon@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.kamhon.ieagle.swing.table; import java.awt.BorderLayout; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import net.kamhon.ieagle.swing.jgoodies.FormBuilder; import net.kamhon.ieagle.swing.jgoodies.KFormBuilder; import net.kamhon.ieagle.swing.test.MyTestPanel; import net.kamhon.ieagle.util.CollectionUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.jgoodies.forms.factories.ButtonBarFactory; import com.jidesoft.dialog.JideOptionPane; public class KTableTestPanel extends MyTestPanel { private static final Log log = LogFactory.getLog(KTableTestPanel.class); private KTable<Student> table; @Override public JPanel createPanel() { JPanel myPanel = new JPanel(new BorderLayout()); myPanel.add(createNorthPanel(), BorderLayout.NORTH); myPanel.add(createCenterPanel(), BorderLayout.CENTER); return myPanel; } private Component createNorthPanel() { FormBuilder builder = new KFormBuilder(); JButton btnSelect = new JButton("Select Checked"); btnSelect.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int[] selectedRows = table.getSelectedRows(); int selectedRow = table.getSelectedRow(); int rowCount = table.getSelectedRowCount(); String s = "selectedRows = " + CollectionUtil.toLog(Arrays.asList(selectedRows)) + "\n" + "selectedRow = " + selectedRow + "\n" + "rowCount = " + rowCount; JideOptionPane.showMessageDialog(null, s); } }); JButton btnRow = new JButton("loop row"); btnRow.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Student student = table.getRecord(0); log.debug("student = " + student); JideOptionPane.showMessageDialog(createPanel(), "First record is " + student.getName()); } }); JPanel btnPanel = ButtonBarFactory.buildCenteredBar(btnSelect, btnRow); builder.addButtonPanel(btnPanel); return builder.getPanel(); } private Component createCenterPanel() { List<Student> students = populateStudents(); KTableColumn[] columns = new KTableColumn[] { // new KTableRowCheckBoxColumn(), // new KTableCheckBoxColumn("name", "Label"), // new KTableTextColumn("name", "Name"), // new KTableTextColumn("age", "Age"), // new KTableTextColumn("weight", "Weight") }; table = new KTable<Student>(columns); table.getModel().addTableModelListener(new TableModelListener() { @Override public void tableChanged(TableModelEvent e) { log.debug("e = " + e); } }); // table.setColumns(Arrays.asList(new KTableTextColumn("name", "Name"), new KTableTextColumn("age", "Age"))); table.setData(students); JScrollPane scrollPane = new JScrollPane(table); return scrollPane; } private List<Student> populateStudents() { List<Student> students = new ArrayList<Student>(); students.add(new Student("Asley", 27, new Date(), 63.0)); students.add(new Student("Ben", 18, new Date(), 45.0)); students.add(new Student("Agnes", 21, new Date(), 47.0)); students.add(new Student("Grace", 27, new Date(), 40.0)); students.add(new Student("June", 28, new Date(), 43.0)); return students; } public static void main(String[] args) { new KTableTestPanel().execute(); } }