实现的内容为:点击表格某项,再点击删除钮,可实现删除点击表格某项,再点击编辑按钮,可实现内容改变点击添加按钮,可实现向表格中添加内容
总结:总的来说中间遇到了很多困难,但是都一步步的解决了。
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.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; // static JPanel editJPanel ; JFrame disEditFrame = new JFrame();
JFrame disaddFrame = new JFrame(); Point mousePoint ;
String obName; 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; 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);
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 {
int row = table.rowAtPoint(mousePoint);
if (row !=-1 ) {
model.removeRow(table.rowAtPoint(mousePoint));
}
} catch (Exception e2) {
System.out.println("please choose first");
}
}
}); //貌似都是第一个是正确的,但是点击第二次或者之后的可能就存在问题??已解决
//还存在一个问题,修改过某一个值后,会在之后的修改框中显示同样名字的变量的修改后的值,而不是最开始的值:已解决
editButton.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent arg0) {
try { final int row= table.rowAtPoint(mousePoint);
if ( row !=-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(mOptmanagerPanel.dfpJpanel());
disEditFrame.setTitle("DFP");
break;
case "LM":
Editbox.removeAll();
Editbox.add(mOptmanagerPanel.lmJpanel());
disEditFrame.setTitle("LM");
break;
case "NewTR":
Editbox.removeAll();
Editbox.add(mOptmanagerPanel.newTrJpanel());
disEditFrame.setTitle("NewTR");
break;
case "NewSD":
Editbox.removeAll();
Editbox.add(mOptmanagerPanel.newSdJpanel());
disEditFrame.setTitle("NewSD");
break;
case "PS":
Editbox.removeAll();
Editbox.add(mOptmanagerPanel.psJpanel());
disEditFrame.setTitle("PS");
break;
case "DE":
Editbox.removeAll();
Editbox.add(mOptmanagerPanel.deJpanel());
disEditFrame.setTitle("DE");
break;
case "PSO":
Editbox.removeAll();
Editbox.add(mOptmanagerPanel.psoJpanel());
disEditFrame.setTitle("PSO");
break;
case "SPLX":
Editbox.removeAll();
Editbox.add(mOptmanagerPanel.splxJpanel());
disEditFrame.setTitle("SPLX");
break;
case "TR":
Editbox.removeAll();
Editbox.add(mOptmanagerPanel.trJpanel());
disEditFrame.setTitle("TR");
break;
default:
System.out.println("please choose edit item");
break;
}
}
editPanel();
}catch (Exception e) {
System.out.println("please choose again !");
}
}
});
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) { addPanel();
}
}); headerJPanel.add(addButton);
headerJPanel.add(delButton);
headerJPanel.add(editButton); return headerJPanel;
} public void addPanel(){
addLeftBox.removeAll();
disaddFrame.setVisible(true); mOptmanagerPanel = new OptmanagerPanel();
disaddFrame.setLayout(new BorderLayout());
addCenterJPanel.setLayout(new BorderLayout()); DefaultListModel listModel = new DefaultListModel<>();
for (OptAlgorithm mAlgorithm :algorithmlist) {
listModel.addElement(mAlgorithm.getName().toString());
}
rowNameList = new JList(listModel);
rowNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); addScrollPane = new JScrollPane(rowNameList);
addScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); rowNameList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
try {
System.out.println(rowNameList.getSelectedIndex());
rowListName = rowNameList.getSelectedValue().toString();
System.out.println(rowListName);
switch (rowListName) {
case "BFGS":
addCenterBox.removeAll();
addCenterBox.add(mOptmanagerPanel.bfgsjJPanel());
disaddFrame.validate();
break;
case "CG":
addCenterBox.removeAll();
addCenterBox.add(mOptmanagerPanel.cgJpanel());
disaddFrame.validate();
break;
case "DFP":
addCenterBox.removeAll();
addCenterBox.add(mOptmanagerPanel.dfpJpanel());
disaddFrame.validate();
break;
case "LM":
addCenterBox.removeAll();
addCenterBox.add(mOptmanagerPanel.lmJpanel());
disaddFrame.validate();
break;
case "NewSD":
addCenterBox.removeAll();
addCenterBox.add(mOptmanagerPanel.newSdJpanel());
disaddFrame.validate();
break;
case "NewTR":
addCenterBox.removeAll();
addCenterBox.add(mOptmanagerPanel.newTrJpanel());
disaddFrame.validate();
break;
case "PS":
addCenterBox.removeAll();
addCenterBox.add(mOptmanagerPanel.psJpanel());
disaddFrame.validate();
break;
case "DE":
addCenterBox.removeAll();
addCenterBox.add(mOptmanagerPanel.deJpanel());
disaddFrame.validate();
break;
case "PSO":
addCenterBox.removeAll();
addCenterBox.add(mOptmanagerPanel.psoJpanel());
disaddFrame.validate();
break;
case "SPLX":
addCenterBox.removeAll();
addCenterBox.add(mOptmanagerPanel.splxJpanel());
disaddFrame.validate();
break;
case "TR":
addCenterBox.removeAll();
addCenterBox.add(mOptmanagerPanel.trJpanel());
disaddFrame.validate();
break;
default:
break;
}
} catch (Exception e2) {
System.out.println("again");
} }
});
addOkButton.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent arg0) {
addrowData[0] = rowListName;
addrowData[1] = String.valueOf(0);
model.addRow(addrowData);
disaddFrame.setVisible(false); }
});
addCancleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
disaddFrame.setVisible(false); //System.exit(0);
}
}); addLeftBox.add(addScrollPane);
addBottomBox.add(initAddOkandCancleJPanel()); addCenterJPanel.add(addCenterBox,BorderLayout.NORTH); disaddFrame.add(addLeftBox,BorderLayout.WEST);
disaddFrame.add(addCenterJPanel,BorderLayout.CENTER);
disaddFrame.add(addBottomBox,BorderLayout.SOUTH);
disaddFrame.setTitle("Add");
disaddFrame.setBounds(750,400,300,400);
//disaddFrame.pack();
disaddFrame.setVisible(true);
} public void editPanel(){ //存在关闭后第二次打开有滚动条嵌套和确定取消按钮位置不一致的问题,已解决:box.removeAll();
editOkButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if(settingEditor.stopEditing()){}
disEditFrame.setVisible(false);
addBottomBox.removeAll();
}
});
editCancleButton.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent arg0) {
disEditFrame.setVisible(false);
addBottomBox.removeAll(); }
}); Editbox.add(initEditOkandCancleJPanel()); disEditFrame.add(Editbox);
disEditFrame.setBounds(750,400,300,400);
disEditFrame.setVisible(true);
disEditFrame.pack();
} public String[][] getAlgorithmData(){ rowData = new String[algorithmlist.size()][2];
for (int i = 0; i < algorithmlist.size(); i++) {
rowData[i][0] = algorithmlist.get(i).getName();
rowData[i][1] = String.valueOf(i);
}
return rowData;
} /*public String[] getRowName(){
rowName = new String[algorithmlist.size()];
for (int i = 0; i < algorithmlist.size(); i++) {
rowName[i] = algorithmlist.get(i).getName();
}
return rowName;
}*/ public JPanel initAddOkandCancleJPanel(){
okandCancleJPanel = new JPanel();
okandCancleJPanel.add(addOkButton);
okandCancleJPanel.add(addCancleButton);
return okandCancleJPanel;
}
public JPanel initEditOkandCancleJPanel(){
okandCancleJPanel = new JPanel();
okandCancleJPanel.add(editOkButton);
okandCancleJPanel.add(editCancleButton);
return okandCancleJPanel;
}
/*public JPanel initdurationJPanel(){
durationJPanel.setLayout(new GridLayout(1, 2,30,10));
durationJPanel.add(new Label("Duration"));
durationJPanel.add(new TextField(0));
return durationJPanel;
}*/ public JPanel bfgsjJPanel(){
setting = new BFGSSetting();
settingEditor = new BFGSSettingEditor((BFGSSetting) setting);
bfgsJPanel = (JPanel) settingEditor.getEditorComponent();
return bfgsJPanel;
}
public JPanel cgJpanel(){
setting = new CGSetting();
settingEditor = new CGSettingEditor((CGSetting) setting);
cgJPanel = (JPanel) settingEditor.getEditorComponent();
return cgJPanel;
}
public JPanel dfpJpanel(){
setting = new DFPSetting() ;
settingEditor = new DFPSettingEditor((DFPSetting) setting);
dfpJPanel = (JPanel) settingEditor.getEditorComponent();
return dfpJPanel;
}
public JPanel lmJpanel(){
setting = new LMSetting();
settingEditor = new LMSettingEditor((LMSetting) setting);
lmJPanel = (JPanel) settingEditor.getEditorComponent();
return lmJPanel;
}
public JPanel newTrJpanel(){
setting = new NewTRSetting();
settingEditor = new NewTRSettingEditor((NewTRSetting) setting);
newTRJPanel = (JPanel) settingEditor.getEditorComponent();
return newTRJPanel;
}
public JPanel newSdJpanel(){
setting = new NewSDSetting();
settingEditor = new NewSDSettingEditor((NewSDSetting) setting);
newSDJPanel = (JPanel) settingEditor.getEditorComponent();
return newSDJPanel;
}
public JPanel psJpanel(){
setting = new PatternSearchSetting();
settingEditor = new PatternSearchSettingEditor((PatternSearchSetting) setting);
pSJPanel = (JPanel) settingEditor.getEditorComponent();
return pSJPanel;
}
public JPanel deJpanel(){
setting = new DifferentialEvolutionSetting();
settingEditor = new DifferentialEvolutionSettingEditor((DifferentialEvolutionSetting) setting);
deJPanel = (JPanel) settingEditor.getEditorComponent();
return deJPanel;
}
public JPanel psoJpanel(){
setting = new PSOSetting();
settingEditor = new PSOSettingEditor((PSOSetting) setting);
psoJPanel = (JPanel) settingEditor.getEditorComponent();
return psoJPanel;
}
public JPanel splxJpanel(){
setting = new SimplexSetting();
settingEditor = new SimplexSettingEditor((SimplexSetting) setting);
splxJPanel = (JPanel) settingEditor.getEditorComponent();
return splxJPanel;
}
public JPanel trJpanel(){
setting = new TRSetting();
settingEditor = new TRSettingEditor((TRSetting) setting);
trJPanel = (JPanel) settingEditor.getEditorComponent();
return trJPanel;
} }

java layout 表格项增加、删除、修改的更多相关文章

  1. [JavaWeb基础] 004.用JSP + SERVLET 进行简单的增加删除修改

    上一次的文章,我们讲解了如何用JAVA访问MySql数据库,对数据进行增加删除修改查询.那么这次我们把具体的页面的数据库操作结合在一起,进行一次简单的学生信息操作案例. 首先我们创建一个专门用于学生管 ...

  2. Nodejs之MEAN栈开发(九)---- 用户评论的增加/删除/修改

    由于工作中做实时通信的项目,需要用到Nodejs做通讯转接功能,刚开始接触,很多都不懂,于是我和同事就准备去学习nodejs,结合nodejs之MEAN栈实战书籍<Getting.MEAN.wi ...

  3. 在Javascript操作JSON对象,增加 删除 修改

    在Javascript操作JSON对象,增加删除修改全有的,详情见代码 <script type="text/javascript"> var jsonObj2 = { ...

  4. AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层

    AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层 AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层我理解的图层的作用大概是把 ...

  5. java 表格项的删除、编辑、增加 修改版

    修改之后的java 代码: package com.platformda.optimize; import java.awt.BorderLayout; import java.awt.Button; ...

  6. ztree树形菜单的增加删除修改和换图标

    首先需要注意一点,如果有研究过树形菜单,就会发现实现删除和修改功能特别简单,但是增加却有一点复杂.造成这个现象是ztree树形菜单的历史遗留问题.大概是之前的版本没有增加这个功能,后来的版本加上了这个 ...

  7. Sql增加,删除,修改列

    1. 查看约束条件 - MySQL: SELECT * FROM information_schema.`TABLE_CONSTRAINTS` where table_name = 'book'; - ...

  8. Spring Boot 增加删除修改 批量

    1.批量删除  a.自定义Repositoy中写 前台处理https://blog.csdn.net/yhflyl/article/details/81557670首先前台先要获取所有的要删除数据的I ...

  9. php后台增加删除修改跳转页面

    第一步 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

随机推荐

  1. [代码搜索]OpenGrok搭建简易教程

    面对着动辄几十GB且随时不断更新的大型代码,我们产生了以下需求:1.快速搜索代码2.代码存放于本地/服务器3.代码可跳转4.跨平台5.易于维护... 显然SourceInsight.ctags.gre ...

  2. python list comprehension twos for loop 嵌套for循环

    list comprehension 后面可以有多个for loops,每个for后面可以有if [(x, y, x * y)for x in(0,1,2,3)for y in(0,1,2,3)if ...

  3. poj 1442 Black Box(优先队列&Treap)

    题目链接:http://poj.org/problem?id=1442 思路分析: <1>维护一个最小堆与最大堆,最大堆中存储最小的K个数,其余存储在最小堆中; <2>使用Tr ...

  4. poj 3984 迷宫问题(dfs)

    题目链接:http://poj.org/problem?id=3984 思路:经典型的DFS题目.搜索时注意剪枝:越界处理,不能访问处理. 代码: #include <iostream> ...

  5. poj 1084 Brainman(归并排序)

    题目链接:http://poj.org/problem?id=1804 思路分析:序列的逆序数即为交换次数,所以求出该序列的逆序数即可. 根据分治法思想,序列分为两个大小相等的两部分,分别求子序列的逆 ...

  6. 文本图片自适应高度小bug以及解决办法

    自定义cell的文本图片自适应高度代码,如果存在自定义的cell赋值封装,就必须将自适应高度代码写在这个方法中 点击效果: 注:- (void)layoutSubviews 方法不能同时操作,否则会出 ...

  7. 基于内容的图像检索技(CBIR)术相术介绍

    基于内容的图像检索技(CBIR)术相术介绍 kezunhai@gmail.com http://blog.csdn.net/kezunhai 近20年来,计算机与信号处理领域如火如荼地发展着,随着普通 ...

  8. 2013 ACM/ICPC 成都网络赛解题报告

    第三题:HDU 4730 We Love MOE Girls 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4730 水题~~~ #include < ...

  9. Android:基于Eclipse编译调试系统级应用源代码

    一.      概要描述 在使用Eclipse导入android工程源代码以后,我们可以使用ddms调试和跟踪源代码. 本文讲述动态调试源代码和静态调试源代码的两种方法,避免build system. ...

  10. ListView.MultiChoiceModeListener

    参考:http://www.cnblogs.com/a284628487/p/3460400.html和http://blog.csdn.net/mayingcai1987/article/detai ...