java 表格项的删除、编辑、增加 修改版
修改之后的java 代码:
package com.platformda.optimize;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Point;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import com.platformda.optimize.algorithm.BFGSSetting;
import com.platformda.optimize.algorithm.BFGSSettingEditor;
import com.platformda.optimize.algorithm.CGSetting;
import com.platformda.optimize.algorithm.CGSettingEditor;
import com.platformda.optimize.algorithm.DFPSetting;
import com.platformda.optimize.algorithm.DFPSettingEditor;
import com.platformda.optimize.algorithm.DifferentialEvolutionSetting;
import com.platformda.optimize.algorithm.DifferentialEvolutionSettingEditor;
import com.platformda.optimize.algorithm.LMSetting;
import com.platformda.optimize.algorithm.LMSettingEditor;
import com.platformda.optimize.algorithm.NewSDSetting;
import com.platformda.optimize.algorithm.NewSDSettingEditor;
import com.platformda.optimize.algorithm.NewTRSetting;
import com.platformda.optimize.algorithm.NewTRSettingEditor;
import com.platformda.optimize.algorithm.PSOSetting;
import com.platformda.optimize.algorithm.PSOSettingEditor;
import com.platformda.optimize.algorithm.PatternSearchSetting;
import com.platformda.optimize.algorithm.PatternSearchSettingEditor;
import com.platformda.optimize.algorithm.SimplexSetting;
import com.platformda.optimize.algorithm.SimplexSettingEditor;
import com.platformda.optimize.algorithm.TRSetting;
import com.platformda.optimize.algorithm.TRSettingEditor;
public class OptmanagerPanel {
OptmanagerPanel mOptmanagerPanel ;
JButton addButton = new JButton();
JButton delButton = new JButton();
JButton editButton = new JButton();
ImageIcon addIcon = new ImageIcon();
ImageIcon delIcon = new ImageIcon();
ImageIcon editIcon = new ImageIcon();
Button editOkButton = new Button(" Ok ");
Button editCancleButton = new Button("Cancle");
Button addOkButton = new Button(" Ok ");
Button addCancleButton = new Button("Cancle");
JPanel okandCancleJPanel = new JPanel();
String[] columnName = { "AlgorithmName", "Duration" };
String[][] rowData ;
String[] rowName;
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable();
JScrollPane scrollPane = new JScrollPane();
JPanel tableJPanel = new JPanel();
JPanel headerJPanel = new JPanel();
JPanel addJPanel = new JPanel();
static OptSettingEditor settingEditor ;
static OptSetting setting;
int row;
int editRow;
int delRow;
// static JPanel editJPanel ;
JFrame disEditFrame = new JFrame();
JFrame disaddFrame = new JFrame();
Point mousePoint ;
String obName;
int[] delSelected ;
OptAlgorithmManager manager = new OptAlgorithmManager();
List<OptAlgorithm> algorithmlist = OptAlgorithmManager.getAllAlgorithms();
String[] addrowData = new String[2];
JPanel durationJPanel = new JPanel();
JList<Object> rowNameList;
JScrollPane addScrollPane;
String rowListName;
String delName;
Box Editbox = Box.createVerticalBox();
Box addCenterBox = Box.createHorizontalBox();
Box addLeftBox = Box.createVerticalBox();
Box addBottomBox = Box.createVerticalBox();
JPanel addCenterJPanel = new JPanel();
//--
JPanel bfgsJPanel ,cgJPanel , dfpJPanel , lmJPanel,newSDJPanel ,newTRJPanel, pSJPanel, deJPanel,psoJPanel,splxJPanel,trJPanel;
public void initPanel(){
mOptmanagerPanel = new OptmanagerPanel();
JFrame mJFrame = new JFrame();
mJFrame.setLayout(new BorderLayout());
mJFrame.add(mOptmanagerPanel.tableHeaderJPanel(),BorderLayout.NORTH);
mJFrame.add(mOptmanagerPanel.tablePanel(), BorderLayout.CENTER);
mJFrame.setBounds(300, 300, 300, 100);
mJFrame.pack();
mJFrame.setVisible(true);
mJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public JPanel tablePanel(){
mOptmanagerPanel = new OptmanagerPanel();
rowData = mOptmanagerPanel.getAlgorithmData();
model = new DefaultTableModel(rowData,columnName);
table = new JTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//所获取的行,在删除后会自动后移
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
mousePoint = e.getPoint();
System.out.println(table.rowAtPoint(mousePoint));
obName = table.getValueAt(table.rowAtPoint(mousePoint), 0).toString();
System.out.println(obName);
}
});
tableJPanel.setLayout(new BoxLayout(tableJPanel,BoxLayout.Y_AXIS));
tableJPanel.add(Box.createVerticalStrut(10));
tableJPanel.add(scrollPane);
tableJPanel.add(Box.createVerticalStrut(10));
return tableJPanel;
}
public JPanel tableHeaderJPanel(){
mOptmanagerPanel = new OptmanagerPanel();
headerJPanel.setLayout(new BoxLayout(headerJPanel, BoxLayout.X_AXIS));
addIcon = new ImageIcon("Picture/add.png");
addButton.setIcon(addIcon);
delIcon = new ImageIcon("Picture/remove.png");
delButton.setIcon(delIcon);
editIcon = new ImageIcon("Picture/edit.png");
editButton.setIcon(editIcon);
//存在问题:未选,则点击删除报错,已解决:加try catch
delButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
delRow = table.rowAtPoint(mousePoint);
if (delRow !=-1 ) {
int delresponse = JOptionPane.showConfirmDialog(null, "Sure to remove ?", "Warning", JOptionPane.OK_CANCEL_OPTION);
if (delresponse == JOptionPane.OK_OPTION) {
model.removeRow(table.rowAtPoint(mousePoint));
}
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "please choose before remove ! ","warning",JOptionPane.YES_OPTION);
}
}
});
//貌似都是第一个是正确的,但是点击第二次或者之后的可能就存在问题??已解决:box.remove
//还存在一个问题,修改过某一个值后,会在之后的修改框中显示同样名字的变量的修改后的值,而不是最开始的值:已解决
//不是显示 修改之后的值
//存在问题:点击删除后,再点击编辑,还可以编辑。解决:分别获取editRow和delRow
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
editRow = table.rowAtPoint(mousePoint);
obName = table.getValueAt(editRow, 0).toString();
if ( editRow !=-1 ) {
switch (obName) {
case "BFGS":
Editbox.removeAll();
Editbox.add(mOptmanagerPanel.bfgsjJPanel());
disEditFrame.setTitle("BFGS");
break;
case "CG":
Editbox.removeAll();
Editbox.add(mOptmanagerPanel.cgJpanel());
disEditFrame.setTitle("CG");
break;
case "DFP":
Editbox.removeAll();
Editbox.add(mOpt补充:软件开发 , Java ,