package com.platformda.optimize;

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
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.JTable;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor; /**
*
* @author Administrator
*/
public class OptDurationManager { public OptDurationManager() {
}
OptDurationManager mOptDurationManager;
JButton addButton = new JButton();
JButton delButton = new JButton();
JButton editButton = new JButton();
ImageIcon addIcon = new ImageIcon();
ImageIcon delIcon = new ImageIcon();
ImageIcon editIcon = new ImageIcon();
OptAlgorithmManager manager = new OptAlgorithmManager();
List<OptAlgorithm> algorithms = OptAlgorithmManager.getAllAlgorithms();
List<OptAlgorithm> addAlgorithms = new ArrayList<OptAlgorithm>();
String[] columnName = {"AlgorithmName", "Duration"};
String[][] rowData;
String[] addRowData;
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable();
JScrollPane scrollPane = new JScrollPane();
DefaultListModel listModel;
JList optAlgorithmList;
JScrollPane AddAlgorithmScroll;
JPanel tableJPanel = new JPanel();
JPanel headerJPanel = new JPanel();
JPanel addJPanel = new JPanel();
JPanel addCenterJPanel = new JPanel();
JPanel editJPanel = new JPanel();
Point mousePoint;
String AlgorithmName;
int delRow;
static int addRow;
static OptSettingEditor settingEditor;
static OptSetting setting;
DialogDescriptor AddDescriptor;
DialogDescriptor EditDescriptor; public void DurationManager() {
JFrame mJFrame = new JFrame();
mJFrame.setLayout(new BorderLayout());
mJFrame.add(initTableJScrollPane(), BorderLayout.CENTER);
mJFrame.add(tableHeader(), BorderLayout.NORTH);
mJFrame.setBounds(300, 300, 300, 100);
mJFrame.setTitle("OptDurationManager");
mJFrame.pack();
mJFrame.setVisible(true);
mJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public JPanel initTableJScrollPane() {
int i = 0;
rowData = new String[algorithms.size()][2];
for (OptAlgorithm mAlgorithm : algorithms) {
rowData[i][0] = mAlgorithm.getName();
rowData[i][1] = String.valueOf(i++);
} model = new DefaultTableModel(rowData, columnName);
table = new JTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
mousePoint = e.getPoint();
AlgorithmName = table.getValueAt(table.rowAtPoint(mousePoint), 0).toString();
System.out.println("(TableRow) " + table.rowAtPoint(mousePoint) + " : " + AlgorithmName);
}
}); scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); tableJPanel.setLayout(new BorderLayout());
tableJPanel.add(scrollPane, BorderLayout.NORTH); return tableJPanel;
} public JPanel tableHeader() { headerJPanel.setLayout(new BorderLayout()); addIcon = new ImageIcon("Picture/add.png");
addButton.setIcon(addIcon);
addButton.setToolTipText("add");
delIcon = new ImageIcon("Picture/remove.png");
delButton.setIcon(delIcon);
delButton.setToolTipText("remove");
editIcon = new ImageIcon("Picture/edit.png");
editButton.setIcon(editIcon);
editButton.setToolTipText("edit"); delButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
delRow = table.rowAtPoint(mousePoint);
if (delRow != -1) {
int delresponse = JOptionPane.showConfirmDialog(null, "Are you sure to remove ?", "Warning", JOptionPane.OK_CANCEL_OPTION);
if (delresponse == JOptionPane.OK_OPTION) {
model.removeRow(table.rowAtPoint(mousePoint));
for (OptAlgorithm mAlgorithm : algorithms) {
if (AlgorithmName.equals(mAlgorithm.getName())) {
addAlgorithms.add(mAlgorithm);
}
}
System.out.println("(Table)RowCount: " + table.getRowCount());
}
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "please choose remove item first ! ");
}
}
});
//问题1:删除后点击编辑,还可以编辑:连续点多次删除,会逐行删除,接着点击编辑,会编辑连续删除中的第一个
//问题2:个数多于algorithms.size()的无法编辑(增添的行数已经增加到table中,并且可以获取)
//---编辑是按行数来,删除后,也会出现编辑不对应的情况...
//解决问题2:获取该行的mAlgorithm.getName(),然后遍历寻找该algorithm,即可实现对应,且不论行数为多少都可编辑
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try { for (OptAlgorithm mAlgorithm : algorithms) {
if (AlgorithmName.equals(mAlgorithm.getName())) {
setting = mAlgorithm.getSetting();
settingEditor = mAlgorithm.getOptSettingEditor();
}
}
editJPanel.setLayout(new BorderLayout());
editJPanel.add(settingEditor.getEditorComponent(), BorderLayout.NORTH); EditDescriptor = new DialogDescriptor(editJPanel, AlgorithmName, true,
DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, null);
Object result = DialogDisplayer.getDefault().notify(EditDescriptor); // displays the dialog
if (result == NotifyDescriptor.OK_OPTION) {
if (settingEditor.stopEditing()) {
}
editJPanel.removeAll();
} else if (result == NotifyDescriptor.CANCEL_OPTION) {
editJPanel.removeAll();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "please choose edit item first !");
}
}
}); //问题3:点击jlist后所显示的面板无法全显,解决:增加JList每个单元格的高度
//已实现:如果某一个algorithm已经修改数据,那么在add时,点击该algorithm会显示已经修改过的值;
//如果删除该algorithm,则再添加时点击该algorithm会显示最初的值
//可改进:只有没有的algorithm,才需要添加;遇到的问题是:弹出的对话框太小。。。
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
addJPanel.removeAll();
addJPanel.setLayout(new BorderLayout()); listModel = new DefaultListModel();
for (OptAlgorithm mAlgorithm : algorithms) {
listModel.addElement(mAlgorithm.getName());
}
optAlgorithmList = new JList(listModel);
optAlgorithmList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
optAlgorithmList.setFixedCellWidth(80);
optAlgorithmList.setFixedCellHeight(32); AddAlgorithmScroll = new JScrollPane(optAlgorithmList);
AddAlgorithmScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); addCenterJPanel.setLayout(new BorderLayout());
//没有设置大小的效果。。
addCenterJPanel.setSize(1000, 1000);
// addCenterJPanel.setBounds(new Rectangle(500, 1000)); setting = algorithms.get(0).getSetting();
settingEditor = algorithms.get(0).getOptSettingEditor();
addCenterJPanel.add(settingEditor.getEditorComponent(), BorderLayout.NORTH);
addJPanel.add(addCenterJPanel, BorderLayout.CENTER); optAlgorithmList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
try {
addRow = optAlgorithmList.getSelectedIndex();
System.out.println("(JListRow) " + addRow + " : " + optAlgorithmList.getSelectedValue().toString());
addCenterJPanel.removeAll();
setting = algorithms.get(addRow).getSetting();
settingEditor = algorithms.get(addRow).getOptSettingEditor();
addCenterJPanel.add(settingEditor.getEditorComponent(), BorderLayout.NORTH);
addJPanel.add(addCenterJPanel, BorderLayout.CENTER);
addJPanel.validate();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error !");
}
}
});
addJPanel.add(AddAlgorithmScroll, BorderLayout.WEST); AddDescriptor = new DialogDescriptor(addJPanel, "addAlgorithm", true,
DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, null);
Object result = DialogDisplayer.getDefault().notify(AddDescriptor); // displays the dialog addRowData = new String[2];
if (result == NotifyDescriptor.OK_OPTION) {
addRowData[0] = algorithms.get(addRow).getName();
addRowData[1] = String.valueOf(0);
model.addRow(addRowData);
} else if (result == NotifyDescriptor.CANCEL_OPTION) {
}
}
}); JToolBar tableToolBar = new JToolBar();
tableToolBar.add(addButton);
tableToolBar.add(delButton);
tableToolBar.add(editButton);
headerJPanel.add(tableToolBar, BorderLayout.NORTH); return headerJPanel;
}
}

今天把前几天写的代码重写了一下,昨天写的被主管批了一顿,想想,的确挺垃圾的,就是玩码字的,java实现的是一种接口思想。

今天写的是200多行,实现的是昨天500多行的作用,也算是一点进步吧。

java 控制表项删除、编辑、添加(实现接口)的更多相关文章

  1. 表单的编辑添加和删除 .removeClass() .append() .preAll() .attr('b') document.createElement()

    1.$(..).removeClass()   去除属性 2$(..).append 把内容加在后面 3.$(..).preAll()  前面所有的兄弟属性 4.$(..).attr('b')  属性 ...

  2. JAVA循环迭代中删除或添加集合数据报java.util.ConcurrentModificationException错误

    1.写出下面的输出结果 public class test{ public static void main(String [] args) List<String> list = new ...

  3. cmd中删除、添加、修改注册表命令

    转自:http://www.jb51.net/article/30586.htm regedit的运行参数 REGEDIT [/L:system] [/R:user] filename1 REGEDI ...

  4. HTML基础第五讲---控制表格及其表项的对齐方式

    转自:https://i.cnblogs.com/posts?categoryid=1121494 缺省情况下,表格在浏览器屏幕上左对齐,你可以使用<TABLE>的ALIGN属性来指定表格 ...

  5. 注册表的作用、bat文件中REG ADD命令添加注册表项以及bat

    注册表的用途与设置 注册表是windows的核心,里面储存着大量的系统信息,说白了就是一个庞大的数据库.如果你不懂什么是数据库,那没关系,不影响你了解注册表,不过最好对数据库有所了解.注册表里面所有的 ...

  6. delphi 注册表操作(读取、添加、删除、修改)完全手册

    DELPHI VS PASCAL(87)  32位Delphi程序中可利用TRegistry对象来存取注册表文件中的信息. 一.创建和释放TRegistry对象 1.创建TRegistry对象.为了操 ...

  7. ASP.NET MVC5+EF6+EasyUI 后台管理系统(82)-Easyui Datagrid批量操作(编辑,删除,添加)

    前言 有时候我们的后台系统表单比较复杂,做过进销存或者一些销售订单的都应该有过感觉 虽然Easyui Datagrid提供了行内编辑,但是不够灵活,但是我们稍微修改一下来达到批量编辑,批量删除,批量添 ...

  8. SQL语句创建数据库,SQL语句删除数据库,SQL语句创建表,SQL语句删除表,SQL语句添加约束,SQL语句删除约束

    创建数据库: CREATE DATABASE Test --要创建的数据库名称 ON PRIMARY ( --数据库文件的具体描述 NAME='Test_data', --主数据文件的逻辑名称 FIL ...

  9. 【转】C#程序打包安装部署之添加注册表项

    今天为大家整理了一些怎样去做程序安装包的具体文档,这些文档并不能确保每个人在做安装包的时候都能正确去生成和运行,但是这些文档的指导作用对于需要的朋友来说还是很有必要的,在实际产品的安装部署过程中可能有 ...

随机推荐

  1. PHP 重载 __call() _callStatic方法

    在C++和java中,可以函数参数的个数或类型来进行重载.但php是弱类型的语言,无法采用传统的方法.若下面这样: class Person{ function fun1($a) { echo 'fu ...

  2. 转:CI引入外部js与css

    其实不管是在用CI还是ZF都有同样一个问题,就是路径的问题.前期,我在用ZF做CMS时,我在.htaccess文件中设置了如遇到js,css,img等资源文件都不重定向.但今天在用CI时,却忘记了,搞 ...

  3. zk create() 方法

    create() $path = $zkh->create($req_path, $data); $path = $zkh->create($req_path, $data, 'flags ...

  4. Ruby学习-第二章

    第二章 类继承,属性,类变量 1.如何声明一个子类 class Treasure < Thing 这样Thing类中的属性name,description都被Treasure继承 2.以下三种方 ...

  5. freemarker序列的拆分

    freemarker序列的拆分 1.简易说明 序列的拆分能够是数组.字符串.布尔值等等 2.实现源代码 <#--freemarker序列的拆分--> ${"hudjfkskhd你 ...

  6. SQLyog 注册码

    用户名: 随意填写 秘钥: ccbfc13e-c31d-42ce-8939-3c7e63ed5417a56ea5da-f30b-4fb1-8a05-95f346a9b20ba0fe8645-3916- ...

  7. SED修改指定行

    一个文件:cat aa #如果第三行是5的话将改为8,很明显第三行是5所以 结果改变 [root@remote ~]# sed -e '3s/5/8/' aa [root@remote ~]# #如果 ...

  8. 模拟QQ系统设置面板实现功能

    业务需求: 基于网盘客户端的实现,原有网盘的设置面板无论从界面显示还是从业务需求都不能满足我们的正常需求.当前的要求是,模拟QQ系统设置的面板实现当前我们网盘中的基本配置功能.在完成这篇文章时已将基本 ...

  9. Android学习笔记:进度条ProgressBar的使用以及与AsyncTask的配合使用

    ProgressBar时android用于显示进度的组件.当执行一个比较耗时的操作(如io操作.网络操作等),为了避免界面没有变化让用户体验降低,提供一个进度条可以让用户知道程序还在运行. 一.Pro ...

  10. SQL之单行函数

    单行函数语法: function name(column|expression,[arg1,arg2,...]) 参数说明: function name:函数名称 column:数据库列名 expre ...