
com.github.becausetesting.commonswindow.utils.ui.JPopupMenuUtils Maven / Gradle / Ivy
package com.github.becausetesting.commonswindow.utils.ui;
/*-
* false
* commons-window
* sectionDelimiter
* Copyright (C) 2016 Alter Hu
* sectionDelimiter
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
/*-
* #%L
* commons-window
* $Id:$
* $HeadURL:$
* %%
* Copyright (C) 2016 Alter Hu
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JTable;
import org.apache.log4j.Logger;
import com.github.becausetesting.commonswindow.RowDataModel;
import com.github.becausetesting.commonswindow.utils.ClipboardUtils;
public class JPopupMenuUtils {
private static Logger log=Logger.getLogger(JPopupMenuUtils.class);
public static void tablePopupMenu(JTable table,RowDataModel rowTableModel) {
JPopupMenu popupMenu = new JPopupMenu();
ClipboardUtils clipboard = new ClipboardUtils();
JMenuItem mntmViewDetail = new JMenuItem("View Detail");
mntmViewDetail.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// view the detail
}
});
popupMenu.add(mntmViewDetail);
JSeparator separatorCopy = new JSeparator();
popupMenu.add(separatorCopy);
JMenuItem mntmCut = new JMenuItem("Cut");
mntmCut.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// add the cut operation
}
});
popupMenu.add(mntmCut);
JMenuItem mntmCopy = new JMenuItem("Copy");
mntmCopy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// add the copy operation
int selectedRow = table.getSelectedRow();
List object = (List) rowTableModel.getRowsDataAsList(selectedRow).get(0);
String rowdata = object.toString();
clipboard.setClipboardContents(rowdata);
log.info("Copied row " + selectedRow + " data: " + rowdata + " in the clipboard.");
}
});
popupMenu.add(mntmCopy);
JSeparator separatorExport = new JSeparator();
popupMenu.add(separatorExport);
JMenu mnExport = new JMenu("Export");
popupMenu.add(mnExport);
JMenuItem mntmTxt = new JMenuItem("Txt");
mntmTxt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
mnExport.add(mntmTxt);
JMenuItem mntmClipborad = new JMenuItem("Clipborad");
mntmClipborad.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int selectedRow = table.getSelectedRow();
List object = (List) rowTableModel.getRowsDataAsList(selectedRow).get(0);
String rowdata = object.toString();
clipboard.setClipboardContents(rowdata);
log.info("Copied row " + selectedRow + " data: " + rowdata + " in the clipboard.");
}
});
mnExport.add(mntmClipborad);
JMenuItem mntmExcel = new JMenuItem("Excel");
mntmExcel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
mnExport.add(mntmExcel);
JSeparator separatorDelete = new JSeparator();
popupMenu.add(separatorDelete);
JMenuItem mntmDelete = new JMenuItem("Delete");
mntmDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int[] selectedRow = table.getSelectedRows();
rowTableModel.removeRow(selectedRow);
}
});
popupMenu.add(mntmDelete);
// add the popup menu
table.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
showMenu(e);
}
}
private void showMenu(MouseEvent e) {
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy