我是用jbuilder做的SWING,用JTABLE的abstrctmodel方式实现JTABLE的。我现在想做到的是,在查询出来的结果中,用户先点JTABLE的某条记录,然后点一个“删除"的按钮,之后将其删除,
我看书上用到的是selectionmodel,想在JB中用,但遇到这样的问题,因为JB的代码是这样生成的
public class Query
extends JInternalFrame {
}
class Query_jButton2_actionAdapter
implements ActionListener{
private Query adaptee;
Query_jButton2_actionAdapter(Query adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
这样的,
那么,我要必须implments ListSelectionListener,这句话加在哪里?
我看书上用到的是selectionmodel,想在JB中用,但遇到这样的问题,因为JB的代码是这样生成的
public class Query
extends JInternalFrame {
}
class Query_jButton2_actionAdapter
implements ActionListener{
private Query adaptee;
Query_jButton2_actionAdapter(Query adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
这样的,
那么,我要必须implments ListSelectionListener,这句话加在哪里?
* TableDemo.java is a 1.4 application that requires these files:
* ColorRenderer.java
* ColorEditor.java
*/
package table;import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;/**
* This is like TableDemo, except that it substitutes a
* Favorite Color column for the Last Name column and specifies
* a custom cell renderer and editor for the color data.
*/
public class TableDemo extends JPanel {
private boolean DEBUG = true;
private String[] columnNames = {"First Name",
"Favorite Color",
"Sport",
"# of Years",
"isDelete"};
private Object[][] data = {
{"Mary", new Color(153, 0, 153),
"Snowboarding", new Integer(1), new Boolean(false)},
{"Alison", new Color(51, 51, 153),
"Rowing", new Integer(2), new Boolean(false)},
{"Kathy", new Color(51, 102, 51),
"Knitting", new Integer(3), new Boolean(false)},
{"Sharon", Color.red,
"Speed reading", new Integer(4), new Boolean(false)},
{"Philip", Color.pink,
"Pool", new Integer(5), new Boolean(false)}
};
public TableDemo() {
super(new GridLayout(2, 1));
final MyTableModel model = new MyTableModel(data, columnNames);
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(new Dimension(500, 180)); //Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table); //Set up renderer and editor for the Favorite Color column.
table.setDefaultRenderer(Color.class,
new ColorRenderer(true));
table.setDefaultEditor(Color.class,
new ColorEditor());
JPanel p = new JPanel(new FlowLayout());
JButton b = new JButton("Delete");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
for(int i = 0; i < model.getRowCount(); i++){
if(((Boolean)(model.getValueAt(i, 4))).booleanValue() == true){
model.removeRow(i);
i--;
}
}
System.out.println(model.getDataVector());
}
});
p.add(b);
//Add the scroll pane to this panel.
add(scrollPane);
add(p);
} class MyTableModel extends DefaultTableModel {
MyTableModel(Object[][] arr, Object[] head){
super(arr, head);
}
public Class getColumnClass(int c) {
if(super.dataVector == null || super.dataVector.size() == 0){
return null;
}
Vector v = (Vector)super.dataVector.elementAt(0);
return v.elementAt(c).getClass();
}
public void setValueAt(Object value, int row, int col) {
super.setValueAt(value, row, col);
/*
*如果要更新数据到数据库,在这里添加代码
**/
}
public void reomveRow(int row){
super.removeRow(row);
/*
*如果要更新数据到数据库,在这里添加代码
**/
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window.
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane.
JComponent newContentPane = new TableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
//frame.setContentPane(newContentPane);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(newContentPane); //Display the window.
frame.pack();
frame.setVisible(true);
} public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
* ColorEditor.java (compiles with releases 1.3 and 1.4) is used by
* TableDialogEditDemo.java.
*/
package table;import javax.swing.AbstractCellEditor;
import javax.swing.table.TableCellEditor;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JTable;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class ColorEditor extends AbstractCellEditor
implements TableCellEditor,
ActionListener {
Color currentColor;
JButton button;
JColorChooser colorChooser;
JDialog dialog;
protected static final String EDIT = "edit"; public ColorEditor() {
//Set up the editor (from the table's point of view),
//which is a button.
//This button brings up the color chooser dialog,
//which is the editor from the user's point of view.
button = new JButton();
button.setActionCommand(EDIT);
button.addActionListener(this);
button.setBorderPainted(false); //Set up the dialog that the button brings up.
colorChooser = new JColorChooser();
dialog = JColorChooser.createDialog(button,
"Pick a Color",
true, //modal
colorChooser,
this, //OK button handler
null); //no CANCEL button handler
} /**
* Handles events from the editor button and from
* the dialog's OK button.
*/
public void actionPerformed(ActionEvent e) {
if (EDIT.equals(e.getActionCommand())) {
//The user has clicked the cell, so
//bring up the dialog.
button.setBackground(currentColor);
colorChooser.setColor(currentColor);
dialog.setVisible(true); //Make the renderer reappear.
fireEditingStopped(); } else { //User pressed dialog's "OK" button.
currentColor = colorChooser.getColor();
}
} //Implement the one CellEditor method that AbstractCellEditor doesn't.
public Object getCellEditorValue() {
return currentColor;
} //Implement the one method defined by TableCellEditor.
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column) {
currentColor = (Color)value;
return button;
}
}//--------------------------------------------------
/*
* ColorRenderer.java (compiles with releases 1.2, 1.3, and 1.4) is used by
* TableDialogEditDemo.java.
*/
package table;import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;
import java.awt.Color;
import java.awt.Component;public class ColorRenderer extends JLabel
implements TableCellRenderer {
Border unselectedBorder = null;
Border selectedBorder = null;
boolean isBordered = true; public ColorRenderer(boolean isBordered) {
this.isBordered = isBordered;
setOpaque(true); //MUST do this for background to show up.
} public Component getTableCellRendererComponent(
JTable table, Object color,
boolean isSelected, boolean hasFocus,
int row, int column) {
Color newColor = (Color)color;
setBackground(newColor);
if (isBordered) {
if (isSelected) {
if (selectedBorder == null) {
selectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
table.getSelectionBackground());
}
setBorder(selectedBorder);
} else {
if (unselectedBorder == null) {
unselectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
table.getBackground());
}
setBorder(unselectedBorder);
}
}
setToolTipText("RGB value: " + newColor.getRed() + ", "
+ newColor.getGreen() + ", "
+ newColor.getBlue());
return this;
}
}
//------------------------------------------------------------
只是实现了删除功能,但没有更新数据到数据库,如果要更新到数据,
请LZ自己加代码...