/*
* Created on Jul 13, 2004
*
*/
package org.jmatlab.swing.ui;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
/**
* @author Ali
*
*/
public class TableDialog extends JDialog implements ActionListener {
private JFrame frame;
private static TableDialog dialog;
private static JTable table;
private static JScrollPane tableScroller;
private static JMatlabTableModel tableModel;
public static Object[][] showDialog(Component frameComp, Properties entries, String title, String[] columnNames) {
getDialog(frameComp, entries, title, columnNames);
dialog.setLocation(200, 200);
dialog.setVisible(true);
return tableModel.getStringArray();
}
private static TableDialog getDialog(Component frameComp, Properties entries, String title, String[] columnNames) {
if (dialog == null) {
Frame frame = JOptionPane.getFrameForComponent(frameComp);
tableModel = new JMatlabTableModel(entries);
tableModel.setColumnNames(columnNames);
dialog = new TableDialog(frame, title);
} else {
tableModel.setEntries(entries);
tableModel.setColumnNames(columnNames);
}
return dialog;
}
private TableDialog(Frame frame, String title) {
super(frame, title, true);
getRootPane().setPreferredSize(new Dimension(500, 230));
table = new JTable(tableModel);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableScroller = new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JButton addButton = new JButton("Add");
addButton.setActionCommand("Add");
JButton removeButton = new JButton("Remove");
removeButton.setActionCommand("Remove");
JButton closeButton = new JButton("Close");
closeButton.setActionCommand("Close");
addButton.addActionListener(this);
removeButton.addActionListener(this);
closeButton.addActionListener(this);
getRootPane().setDefaultButton(closeButton);
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new GridLayout(1, 3, 10, 10));
buttonPane.add(addButton);
buttonPane.add(removeButton);
buttonPane.add(closeButton);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(tableScroller, BorderLayout.CENTER);
// contentPane.add(table, BorderLayout.CENTER);
contentPane.add(buttonPane, BorderLayout.SOUTH);
pack();
}
public void actionPerformed(ActionEvent e) {
if ("Add".equals(e.getActionCommand())) {
tableModel.addRow();
} else if ("Remove".equals(e.getActionCommand())) {
tableModel.removeRow(table.getSelectedRow());
} else {
TableDialog.dialog.setVisible(false);
}
}
}
|