java新增编辑_java 控制表项删除、编辑、添加(实现接口)

本文介绍了一个Java程序,用于管理OptDuration,实现了表中的算法名称和持续时间的增删改功能。通过监听鼠标事件进行操作,包括添加、删除和编辑算法。在编辑时,使用OptSettingEditor获取并更新设置。程序还处理了删除后编辑可能出现的问题,以及添加新算法的逻辑。
摘要由CSDN通过智能技术生成

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 algorithms = OptAlgorithmManager.getAllAlgorithms();

List addAlgorithms = new ArrayList();

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多行的作用,也算是一点进步吧。

package BookTest; import java.awt.*; import java.awt.event.*; import java.util.AbstractQueue; import java.util.Vector; import javax.swing.*; import javax.swing.table.*; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeSelectionModel; import javax.swing.event.*; import javax.swing.table.TableColumnModel; //import DbHelp.OperatorJobs; import Dao.ArrayBook; public class Test extends JFrame { public JPanel jp=null; public String aa; //JTable jTable1 = new JTable(); public JTable jTable1=null; private JTree jTree1; DefaultTableModel mode; private JScrollPane scrollPane = null; DefaultMutableTreeNode node; JButton[] btn={new JButton("加"),new JButton("删除"),new JButton("修改"),new JButton("查询"),new JButton("重置")}; public JTextField[] txt ={new JTextField(),new JTextField(),new JTextField(),new JTextField(),new JTextField(),new JTextField()}; public String[] str1={"1","summer","boy","20","",""}; public String[] str2={"2","wulei","girl","19","",""}; public String[] str3={"3","west","boy","20","",""}; public String[] str4={"4","fish","girl","18","",""}; public String[] str5={"5","youku","boy","21","",""}; public String[] str6={"6","aa","boy","21","",""}; public String[][] tempData1={str1,str2,str3,str4,str5,str6}; public String[] aa1={"1","summer","boy","20","123","123"}; public String[] aa2={"2","wulei","girl","19","123",""}; public String[] aa3={"3","west","boy","20","123",""}; public String[] aa4={"4","fish","girl","18","1",""}; public String[] aa5={"5","youku","boy","21","12",""}; public String[] aa6={"6","aa","boy","21","123",""}; public String[][] tempData4={aa1,aa2,aa3,aa4,aa5,aa6}; //设置表头数据 public String[] tempTitle={"ID","标题","内容","作者","时间","是否选中"}; //设置表格的数据集合 Vector v1=new Vector(); Vecto
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值