📄 modifiabletable.java
字号:
/*
* 07/28/2005
*
* ModifiableTable.java - A table modified by Add, Remove, and Modify buttons.
* Copyright (C) 2005 Robert Futrell
* email@address.com
* www.website.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.fife.ui.modifiabletable;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.ListSelectionModel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.EventListenerList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;
import javax.swing.table.DefaultTableModel;
import org.fife.ui.RButton;
import org.fife.ui.RScrollPane;
/**
* A table with "Add," "Remove," and possibly "Modify" buttons.<p>
*
* Basically, the caller provides the table model for the data, along
* with a "handler" that is called when adding/editing/removing data
* from the model. When you want to retrieve the data back from the
* table, you can get it via {@link #getDataVector}.
*
* @author Robert Futrell
* @version 1.0
* @see ModifiableTableListener
*/
public class ModifiableTable extends JPanel {
/**
*
*/
private static final long serialVersionUID = -3404146382025380474L;
public static final int ADD = 1;
public static final int REMOVE = 2;
public static final int MODIFY = 4;
public static final int ADD_REMOVE = ADD|REMOVE;
public static final int ADD_REMOVE_MODIFY = ADD|REMOVE|MODIFY;
public static final String TOP = BorderLayout.NORTH;
public static final String BOTTOM = BorderLayout.SOUTH;
public static final String LEFT = BorderLayout.WEST;
public static final String RIGHT = BorderLayout.EAST;
private JTable table;
private RButton addButton;
private RButton removeButton;
private RButton modifyButton;
private RowHandler rowHandler;
private Listener listener;
private EventListenerList listenerList;
private static final String ADD_COMMAND = "AddCommand";
private static final String REMOVE_COMMAND = "RemoveCommand";
private static final String MODIFY_COMMAND = "ModifyCommand";
private static final String BUNDLE_NAME =
"org.fife.ui.modifiabletable.ModifiableTable";
/*****************************************************************************/
/**
* Creates a <code>ModifiableTable</code> with <code>Add</code>,
* <code>Remove</code>, and <code>Modify</code> buttons below the table.
*
* @param model The model to use for the table.
* @see #setRowHandler
*/
public ModifiableTable(DefaultTableModel model) {
this(model, null);
}
/*****************************************************************************/
/**
* Creates a <code>ModifiableTable</code> with <code>Add</code>,
* <code>Remove</code>, and <code>Modify</code> buttons below the table.
*
* @param model The model to use for the table.
* @param columnNames Objects to use for names of the columns. This value
* can be <code>null</code> if the model already takes care of its
* column names.
* @see #setRowHandler
*/
public ModifiableTable(DefaultTableModel model, Object[] columnNames) {
this(model, columnNames, BOTTOM, ADD_REMOVE_MODIFY);
}
/*****************************************************************************/
/**
* Constructor.
*
* @param model The model to use for the table.
* @param buttonLocation The location of the buttons, relative to the
* table.
* @param buttons A bit flag representing what buttons to display.
* @see #setRowHandler
*/
public ModifiableTable(DefaultTableModel model, String buttonLocation,
int buttons) {
this(model, null, buttonLocation, buttons);
}
/*****************************************************************************/
/**
* Constructor.
*
* @param model The model to use for the table.
* @param columnNames Objects to use for names of the columns. This value
* can be <code>null</code> if the model already takes care of its
* column names.
* @param buttonLocation The location of the buttons, relative to the
* table.
* @param buttons A bit flag representing what buttons to display.
* @see #setRowHandler
*/
public ModifiableTable(DefaultTableModel model, Object[] columnNames,
String buttonLocation, int buttons) {
listener = new Listener();
this.table = createTable(model, columnNames);
listenerList = new EventListenerList();
setLayout(new BorderLayout());
add(new RScrollPane(table));
add(createButtonPanel(buttonLocation, buttons), buttonLocation);
}
/*****************************************************************************/
/**
* Adds a listener to this modifiable table.
*
* @param l The listener to add.
* @see #removeModifiableTableListener
*/
public void addModifiableTableListener(ModifiableTableListener l) {
listenerList.add(ModifiableTableListener.class, l);
}
/*****************************************************************************/
/**
* Gets information from the user on a new row to add and adds it to
* the table. This method is called whenever the user presses the
* <code>Add</code> button.
*
* @see #modifyRow
* @see #removeRow
*/
protected void addRow() {
if (rowHandler!=null) {
Object[] newData = rowHandler.getNewRowInfo(null);
if (newData!=null) {
((DefaultTableModel)table.getModel()).addRow(newData);
fireModifiableTableEvent(ModifiableTableChangeEvent.ADDED,
table.getRowCount()-1);
}
}
}
/*****************************************************************************/
/**
* Returns the panel of buttons for modifying the table.
*
* @param buttonLocation The location of the buttons, relative to the
* table.
* @param buttons A bit flag representing what buttons to display.
* @return The panel of buttons.
*/
protected JPanel createButtonPanel(String buttonLocation, int buttons) {
// Get panel and spacing ready.
JPanel panel = new JPanel();
JPanel buttonPanel = new JPanel(new BorderLayout());
String buttonLoc2 = null;
if (RIGHT.equals(buttonLocation)) {
panel.setLayout(new GridLayout(3,1, 5,5));
buttonLoc2 = BorderLayout.NORTH;
buttonPanel.setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
}
else if (LEFT.equals(buttonLocation)) {
panel.setLayout(new GridLayout(3,1, 5,5));
buttonLoc2 = BorderLayout.NORTH;
buttonPanel.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
}
else if (TOP.equals(buttonLocation)) {
panel.setLayout(new GridLayout(1,3, 5,5));
buttonLoc2 = BorderLayout.WEST;
buttonPanel.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
}
else { // BOTTOM, or invalid value.
panel.setLayout(new GridLayout(1,3, 5,5));
buttonLoc2 = BorderLayout.WEST;
buttonPanel.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
}
ResourceBundle msg = ResourceBundle.getBundle(BUNDLE_NAME);
// Gather the desired buttons.
if ((buttons&ADD)==ADD) {
addButton = new RButton(msg.getString("Button.Add"));
addButton.setActionCommand(ADD_COMMAND);
addButton.addActionListener(listener);
panel.add(addButton);
}
if ((buttons&REMOVE)==REMOVE) {
removeButton = new RButton(msg.getString("Button.Remove"));
removeButton.setActionCommand(REMOVE_COMMAND);
removeButton.addActionListener(listener);
removeButton.setEnabled(false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -