Java/Swing Components/List — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 06:57, 1 июня 2010
Содержание
CheckBox List
/*
* Copyright (C) 2005 - 2007 JasperSoft Corporation. All rights reserved.
* http://www.jaspersoft.ru.
*
* Unless you have purchased a commercial license agreement from JasperSoft,
* the following license terms apply:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* This program is distributed WITHOUT ANY WARRANTY; and without the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
* or write to:
*
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330,
* Boston, MA USA 02111-1307
*
*
*
*
* CheckBoxList.java
*
* Created on October 5, 2006, 9:53 AM
*
*/
/**
*
* @author gtoffoli
*/
import java.awt.Color;
import java.awt.ruponent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
public class CheckBoxList extends JList {
public CheckBoxList() {
super();
setModel(new DefaultListModel());
setCellRenderer(new CheckboxCellRenderer());
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int index = locationToIndex(e.getPoint());
if (index != -1) {
Object obj = getModel().getElementAt(index);
if (obj instanceof JCheckBox) {
JCheckBox checkbox = (JCheckBox) obj;
checkbox.setSelected(!checkbox.isSelected());
repaint();
}
}
}
}
);
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
@SuppressWarnings("unchecked")
public int[] getCheckedIdexes() {
java.util.List list = new java.util.ArrayList();
DefaultListModel dlm = (DefaultListModel) getModel();
for (int i = 0; i < dlm.size(); ++i) {
Object obj = getModel().getElementAt(i);
if (obj instanceof JCheckBox) {
JCheckBox checkbox = (JCheckBox) obj;
if (checkbox.isSelected()) {
list.add(new Integer(i));
}
}
}
int[] indexes = new int[list.size()];
for (int i = 0; i < list.size(); ++i) {
indexes[i] = ((Integer) list.get(i)).intValue();
}
return indexes;
}
@SuppressWarnings("unchecked")
public java.util.List getCheckedItems() {
java.util.List list = new java.util.ArrayList();
DefaultListModel dlm = (DefaultListModel) getModel();
for (int i = 0; i < dlm.size(); ++i) {
Object obj = getModel().getElementAt(i);
if (obj instanceof JCheckBox) {
JCheckBox checkbox = (JCheckBox) obj;
if (checkbox.isSelected()) {
list.add(checkbox);
}
}
}
return list;
}
}
/*
* Copyright (C) 2005 - 2007 JasperSoft Corporation. All rights reserved.
* http://www.jaspersoft.ru.
*
* Unless you have purchased a commercial license agreement from JasperSoft, the
* following license terms apply:
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*
* This program is distributed WITHOUT ANY WARRANTY; and without the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, see http://www.gnu.org/licenses/gpl.txt or write to:
*
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA USA
* 02111-1307
*
*
*
*
* CheckboxCellRenderer.java
*
* Created on October 5, 2006, 10:03 AM
*
*/
/**
*
* @author gtoffoli
*/
class CheckboxCellRenderer extends DefaultListCellRenderer {
protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
if (value instanceof CheckBoxListEntry) {
CheckBoxListEntry checkbox = (CheckBoxListEntry) value;
checkbox.setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
if (checkbox.isRed()) {
checkbox.setForeground(Color.red);
} else {
checkbox.setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
}
checkbox.setEnabled(isEnabled());
checkbox.setFont(getFont());
checkbox.setFocusPainted(false);
checkbox.setBorderPainted(true);
checkbox.setBorder(isSelected ? UIManager.getBorder("List.focusCellHighlightBorder")
: noFocusBorder);
return checkbox;
} else {
return super.getListCellRendererComponent(list, value.getClass().getName(), index,
isSelected, cellHasFocus);
}
}
}
/*
* Copyright (C) 2005 - 2007 JasperSoft Corporation. All rights reserved.
* http://www.jaspersoft.ru.
*
* Unless you have purchased a commercial license agreement from JasperSoft, the
* following license terms apply:
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*
* This program is distributed WITHOUT ANY WARRANTY; and without the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, see http://www.gnu.org/licenses/gpl.txt or write to:
*
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA USA
* 02111-1307
*
*
*
*
* CheckBoxListEntry.java
*
* Created on October 5, 2006, 10:19 AM
*
*/
/**
*
* @author gtoffoli
*/
class CheckBoxListEntry extends JCheckBox {
private Object value = null;
private boolean red = false;
public CheckBoxListEntry(Object itemValue, boolean selected) {
super(itemValue == null ? "" : "" + itemValue, selected);
setValue(itemValue);
}
public boolean isSelected() {
return super.isSelected();
}
public void setSelected(boolean selected) {
super.setSelected(selected);
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public boolean isRed() {
return red;
}
public void setRed(boolean red) {
this.red = red;
}
}
CheckBox List by Zhiguo Yin
/*
author: yinzhiguo
mail: yin_zhiguo at hotmail.ru
env: ubuntu 8.04 jdk1.6.0
this is a checkbox list ctrl for developer java swing application.
it is very simple. you only require implements a method!
require apache commons-lang.jar
For example:
langList = new CheckboxList(ts){
@Override
public Object getDataLabel(Object data){
Book a = (Book)data;
return a.getTitle();
}
};
centerPane.add(langList, BorderLayout.CENTER);
ArrayList as = langList.getSelectedData();
the ts is a Object array that your checkboxlist model.
you will implements the getDataLabel method.
get user"s chooser is simple, only invoke langList.getSelectedData()
*/
package com.jexp.java.swing.rumon.pane;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JMenuItem;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
/**
* this is a checkbox list for swing application
* @author yinzhiguo
*/
public class CheckboxList extends javax.swing.JPanel {
private Vector<BooleanWrap> dataModel = new Vector<BooleanWrap>();
private int columnWidth = 0;
public CheckboxList( Object[] datas) {
if(datas == null)
throw new java.lang.IllegalArgumentException("datas is null!");
int max = 0;
for(Object o : datas)
{
BooleanWrap bw = new BooleanWrap(o);
int a = bw.getLabel().length();
if(max < a)
max = a;
dataModel.add(bw);
}
columnWidth = max;
initComponents();
this.setPreferredSize(table.getPreferredSize());
}
public CheckboxList(ArrayList list) {
if(list == null || list.isEmpty())
throw new java.lang.IllegalArgumentException("list is null or empty!");
int max = 0;
for(Object o : list)
{
BooleanWrap bw = new BooleanWrap(o);
int a = bw.getLabel().length();
if(max < a)
max = a;
dataModel.add(bw);
}
columnWidth = max;
initComponents();
this.setPreferredSize(table.getPreferredSize());
}
private JMenuItem getMenuItem(String id) {
JMenuItem item = new javax.swing.JMenuItem();
if("all".equals(id))
{
item.setText("??");
}else if("notAll".equals(id))
{
item.setText("???");
}else if("reverse".equals(id))
{
item.setText("??");
}
return item;
}
public ArrayList getSelectedData(){
ArrayList i = new ArrayList();
for(BooleanWrap bw : dataModel){
if(bw.getChecked()){
i.add(bw.getData());
}
}
return i;
}
private TableModel getTableModel() {
return new DataModel();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
popupMenu = new javax.swing.JPopupMenu();
allMenuItem = getMenuItem("all");
notAllMenuItem = getMenuItem("notAll");
reverseMenuItem = getMenuItem("reverse");
colText = new javax.swing.JTextField();
scrollPane = new javax.swing.JScrollPane();
table = new javax.swing.JTable();
allMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
allMenuItemActionPerformed(evt);
}
});
popupMenu.add(allMenuItem);
notAllMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
notAllMenuItemActionPerformed(evt);
}
});
popupMenu.add(notAllMenuItem);
reverseMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
reverseMenuItemActionPerformed(evt);
}
});
popupMenu.add(reverseMenuItem);
setLayout(new java.awt.BorderLayout());
add(colText, java.awt.BorderLayout.PAGE_START);
table.setModel(getTableModel());
table.setComponentPopupMenu(popupMenu);
setTableParam();
scrollPane.setViewportView(table);
add(scrollPane, java.awt.BorderLayout.CENTER);
}// </editor-fold>//GEN-END:initComponents
private void actionPerformOfButton(int type){
if(!dataModel.isEmpty()){
for(BooleanWrap bw : dataModel)
{
if(type == 1){
bw.setChecked(Boolean.TRUE);
}else if(type == 2)
{
bw.setChecked(Boolean.FALSE);
}else if(type == 3){
bw.setChecked(!bw.getChecked());
}
}
((AbstractTableModel)table.getModel()).fireTableDataChanged();
}
}
private void allMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allMenuItemActionPerformed
// TODO add your handling code here:
actionPerformOfButton(1);
}//GEN-LAST:event_allMenuItemActionPerformed
private void notAllMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_notAllMenuItemActionPerformed
// TODO add your handling code here:
actionPerformOfButton(2);
}//GEN-LAST:event_notAllMenuItemActionPerformed
private void reverseMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reverseMenuItemActionPerformed
// TODO add your handling code here:
actionPerformOfButton(3);
}//GEN-LAST:event_reverseMenuItemActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem allMenuItem;
private javax.swing.JTextField colText;
private javax.swing.JMenuItem notAllMenuItem;
private javax.swing.JPopupMenu popupMenu;
private javax.swing.JMenuItem reverseMenuItem;
private javax.swing.JScrollPane scrollPane;
private javax.swing.JTable table;
// End of variables declaration//GEN-END:variables
private void setTableParam() {
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// table.setDragEnabled(false);
table.setShowGrid(false);
table.getTableHeader().setEnabled(false);
//???????
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setPreferredSize(new Dimension(0, 0));
table.getTableHeader().setDefaultRenderer(renderer);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
return;
}
ListSelectionModel sm = (ListSelectionModel) e.getSource();
int row = sm.getLeadSelectionIndex();
if(row >=0){
BooleanWrap bw = dataModel.get(row);
colText.setText(bw.getLabel());
}
}
});
DefaultTableColumnModel dtm = (DefaultTableColumnModel)table.getColumnModel();
int count = dtm.getColumnCount();
for(int i = 0; i < count; i++)
{
TableColumn tc = dtm.getColumn(i);
tc.setResizable(false);
if(i == 0){
tc.setWidth(15);
tc.setPreferredWidth(25);
}else{
tc.setWidth(columnWidth * 3);
tc.setPreferredWidth(columnWidth * 10);
}
}
ColumnColorTableCellRenderer ctlr = new ColumnColorTableCellRenderer();
table.getColumnModel().getColumn(1).setCellRenderer(ctlr);
}
class DataModel extends AbstractTableModel {
private String[] columnNames = {"...", "..."};
public Class getColumnClass(int columnIndex) {
if(columnIndex == 0)
return Boolean.class;
return String.class;
}
public String getColumnName(int column) {
return columnNames[column];
}
public int getRowCount() {
return dataModel.size();
}
public int getColumnCount() {
return columnNames.length;
}
public Object getValueAt(int rowIndex, int colIndex) {
BooleanWrap bw = dataModel.get(rowIndex);
if(colIndex == 1)
return getDataLabel(bw.getData());
else{
return bw.getChecked();
}
}
@Override
public void setValueAt(Object value, int rowIndex, int colIndex) {
if(colIndex == 0){
Boolean flag = (Boolean)value;
BooleanWrap bw = dataModel.get(rowIndex);
bw.setChecked(flag);
}
}
@Override
public boolean isCellEditable(int rowIndex,
int columnIndex) {
if(columnIndex == 0)
return true;
return false;
}
}
/**
* ????????list????????
* ????,??????
* @param data
* @return
*/
public Object getDataLabel(Object data){
return data.toString();
}
class BooleanWrap{
private Boolean checked = Boolean.FALSE;
private Object data = null;
public BooleanWrap(Object obj)
{
this.data = obj;
}
public Boolean getChecked() {
return checked;
}
public void setChecked(Boolean checked) {
this.checked = checked;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getLabel(){
return (String)getDataLabel(this.getData());
}
}
}
//////////////////////////////////////////////////////
/*
* AlternationColorTableCellRenderer.java
*
* Created on 2007?9?14?, ??10:32
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.jexp.java.swing.rumon.pane;
import java.awt.Color;
import java.awt.ruponent;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
/**
*
* @author java
*/
public class ColumnColorTableCellRenderer extends DefaultTableCellRenderer{
private Color colorOne = Color.white;
private Color colorTwo = new Color(206,231,255);
private Color textColor = Color.BLUE;
private Color numberColor = Color.RED;
private Color dateColor = Color.BLUE;
/** Creates a new instance of AlternationColorTableCellRenderer */
public ColumnColorTableCellRenderer(Color one, Color two) {
if(one != null)
colorOne = one;
if(two != null)
colorTwo = two;
}
public ColumnColorTableCellRenderer() {
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if(value instanceof String)
{
String str = (String)value;
this.setForeground(textColor);
}else if(value instanceof Integer || value instanceof Double || value instanceof Long)
{
this.setForeground(numberColor);
this.setHorizontalAlignment(SwingConstants.RIGHT);
}
if(row % 2 == 0){
setBackground(colorOne);
}
else if(row % 2 == 1)
{
setBackground(colorTwo);
}
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
}
public Color getTextColor() {
return textColor;
}
public void setTextColor(Color textColor) {
this.textColor = textColor;
}
public Color getNumberColor() {
return numberColor;
}
public void setNumberColor(Color numberColor) {
this.numberColor = numberColor;
}
public Color getDateColor() {
return dateColor;
}
public void setDateColor(Color dateColor) {
this.dateColor = dateColor;
}
}
//////////////////////////////////////////////////////
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.jexp.java.swing.rumon.pane;
import com.uusee.platform.util.*;
import java.awt.ruponent;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
/**
* @email yin_zhiguo@hotmail.ru
* @author yinzhiguo
*/
public class SwingUtil {
/** Creates a new instance of SwingUtil */
private SwingUtil() {
}
public static String[] getClassNameOfLookAndFeel() {
LookAndFeelInfo[] lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
String[] name = new String[lookAndFeelInfos.length];
for (int i = 0; i < lookAndFeelInfos.length; i++) {
name[i] = lookAndFeelInfos[i].getClassName();
}
return name;
}
public static void setLookAndFeel(String clzz) {
try {
UIManager.setLookAndFeel(clzz);
} catch (Exception exception) {
exception.printStackTrace();
}
}
public static int getScreenWidth() {
return getScreenSize().width;
}
public static int getScreenHeight() {
return getScreenSize().height;
}
public static Dimension getScreenSize() {
return Toolkit.getDefaultToolkit().getScreenSize();
}
public static void setLocationToCenterOfComponent(Component parent, Component child)
{
Point parentPoint = parent.getLocation();
Dimension parentDim = parent.getSize();
Dimension childDim = child.getSize();
child.setLocation(parentPoint.x + (parentDim.width - childDim.width)/2, parentPoint.y + (parentDim.height - childDim.height)/2);
}
/**
* ???????
* @param comp
*/
public static void setLocationToCenterOfScreen(Component comp) {
Dimension screenSize = getScreenSize();
Dimension frameSize = comp.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
comp.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
public static String[] convertToMaxWidth(String[] strs, FontMetrics fm){
int max = 0; //????
int fix = fm.stringWidth(" ");//???????(?????)
int[] widths = new int[strs.length]; //???????(?????)
int[] dis = new int[strs.length]; //???????(?????)
for(int i = 0; i < strs.length; i++){
int w = fm.stringWidth(strs[i]);
// System.out.println(strs[i] + " " + w);
widths[i] = w;
if(w > max){
max = w;
}
}
max = max + fix * 4;
// System.out.println("max: " + max + ", fix: " + fix);
for(int i = 0; i < strs.length; i++){
int distance = max - widths[i]; //??????
dis[i] = (distance / fix) + 1;
}
String[] res = new String[strs.length];
for(int i = 0; i < strs.length; i++){
res[i] = Helper.leftAppendSpace(strs[i], dis[i]);
//System.out.println(strs[i] + " " + dis[i] + ", "+ widths[i] + ", " + fm.stringWidth(strs[i]));
}
return res;
}
}
//////////////////////////////////////////////////////
package com.jexp.java.swing.rumon.pane;
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
/**
* @email yin_zhiguo@hotmail.ru
* @author yinzhiguo
*/
public class CheckboxListTestFrame extends javax.swing.JFrame {
/** Creates new form CheckboxListTestFrame */
public CheckboxListTestFrame() {
initComponents();
this.setSize(140, 230);
this.setTitle("CheckboxList ??");
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
centerPane = new javax.swing.JPanel();
testButPane = new javax.swing.JPanel();
testBut = getButtonBy("test");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
centerPane.setLayout(new java.awt.BorderLayout());
testButPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
testBut.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
testButActionPerformed(evt);
}
});
testButPane.add(testBut);
centerPane.add(testButPane, java.awt.BorderLayout.PAGE_END);
prepareCheckboxListPane();
getContentPane().add(centerPane, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* ??ID??JButton???
* @param id ?????
* @return ????
*/
public JButton getButtonBy(String id){
JButton but = new JButton();
if("test".equals(id)){
but.setText("??");
but.setToolTipText("??");
}
return but;
}
private CheckboxList langList = null;
class Book{
private String title = "";
private double price = 0.0;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
private void prepareCheckboxListPane() {
Book t = new Book();
t.setTitle("Java??");
t.setPrice(20.5);
Book t1 = new Book();
t1.setTitle("PHP??");
t1.setPrice(30.5);
Book t2 = new Book();
t2.setTitle("C++??");
t2.setPrice(50.5);
Book t3 = new Book();
t3.setTitle("Python??");
t3.setPrice(30.5);
Book t4 = new Book();
t4.setTitle("Perl??");
t4.setPrice(23.5);
Book t5 = new Book();
t5.setTitle("SmallTalk??");
t5.setPrice(23.5);
Book t6 = new Book();
t6.setTitle("Ruby??");
t6.setPrice(23.5);
Book t7 = new Book();
t7.setTitle("Lisp??");
t7.setPrice(23.5);
Book t8 = new Book();
t8.setTitle("Shell??");
t8.setPrice(23.5);
Book[] ts = {t,t1,t2,t3,t4,t5,t6,t7,t8};
langList = new CheckboxList(ts){
@Override
public Object getDataLabel(Object data){
Book a = (Book)data;
return a.getTitle();
}
};
centerPane.add(langList, BorderLayout.CENTER);
}
private void testButActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButActionPerformed
// TODO add your handling code here:
ArrayList as = langList.getSelectedData();
if(as.isEmpty()){
JOptionPane.showMessageDialog(this, "????????!", "??", JOptionPane.INFORMATION_MESSAGE);
}else{
StringBuffer sub = new StringBuffer();
sub.append("<html>");
sub.append("<b>????:</b><br>");
sub.append("<ul>");
for(Object o : as){
Book b = (Book)o;
sub.append("<li>" + b.getTitle() + ", " + b.getPrice() + "</li>");
}
sub.append("</ul>");
sub.append("</html>");
JOptionPane.showMessageDialog(this, sub.toString(), "??", JOptionPane.INFORMATION_MESSAGE);
}
}//GEN-LAST:event_testButActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
UIManager.put("swing.boldMetal", Boolean.FALSE);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
CheckboxListTestFrame cltf = new CheckboxListTestFrame();
SwingUtil.setLocationToCenterOfScreen(cltf);
cltf.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel centerPane;
private javax.swing.JButton testBut;
private javax.swing.JPanel testButPane;
// End of variables declaration//GEN-END:variables
}
Check List Example
// Example from http://www.crionics.ru/products/opensource/faq/swing_ex/SwingExamples.html
//File:CheckListExample.java
/* (swing1.1.1beta2) */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.MetalIconFactory;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;
/**
* @version 1.0 04/24/99
*/
public class CheckListExample extends JFrame {
public CheckListExample() {
super("CheckList Example");
String[] strs = { "swing", "home", "basic", "metal", "JList" };
final JList list = new JList(createData(strs));
// set "home" icon
Icon icon = MetalIconFactory.getFileChooserHomeFolderIcon();
((CheckableItem) list.getModel().getElementAt(1)).setIcon(icon);
list.setCellRenderer(new CheckListRenderer());
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setBorder(new EmptyBorder(0, 4, 0, 0));
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int index = list.locationToIndex(e.getPoint());
CheckableItem item = (CheckableItem) list.getModel()
.getElementAt(index);
item.setSelected(!item.isSelected());
Rectangle rect = list.getCellBounds(index, index);
list.repaint(rect);
}
});
JScrollPane sp = new JScrollPane(list);
final JTextArea textArea = new JTextArea(3, 10);
JScrollPane textPanel = new JScrollPane(textArea);
JButton printButton = new JButton("print");
printButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ListModel model = list.getModel();
int n = model.getSize();
for (int i = 0; i < n; i++) {
CheckableItem item = (CheckableItem) model.getElementAt(i);
if (item.isSelected()) {
textArea.append(item.toString());
textArea.append(System.getProperty("line.separator"));
}
}
}
});
JButton clearButton = new JButton("clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText("");
}
});
JPanel panel = new JPanel(new GridLayout(2, 1));
panel.add(printButton);
panel.add(clearButton);
getContentPane().add(sp, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.EAST);
getContentPane().add(textPanel, BorderLayout.SOUTH);
}
private CheckableItem[] createData(String[] strs) {
int n = strs.length;
CheckableItem[] items = new CheckableItem[n];
for (int i = 0; i < n; i++) {
items[i] = new CheckableItem(strs[i]);
}
return items;
}
class CheckableItem {
private String str;
private boolean isSelected;
private Icon icon;
public CheckableItem(String str) {
this.str = str;
isSelected = false;
}
public void setSelected(boolean b) {
isSelected = b;
}
public boolean isSelected() {
return isSelected;
}
public String toString() {
return str;
}
public void setIcon(Icon icon) {
this.icon = icon;
}
public Icon getIcon() {
return icon;
}
}
class CheckListRenderer extends CheckRenderer implements ListCellRenderer {
Icon commonIcon;
public CheckListRenderer() {
check.setBackground(UIManager.getColor("List.textBackground"));
label.setForeground(UIManager.getColor("List.textForeground"));
commonIcon = UIManager.getIcon("Tree.leafIcon");
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean hasFocus) {
setEnabled(list.isEnabled());
check.setSelected(((CheckableItem) value).isSelected());
label.setFont(list.getFont());
label.setText(value.toString());
label.setSelected(isSelected);
label.setFocus(hasFocus);
Icon icon = ((CheckableItem) value).getIcon();
if (icon == null) {
icon = commonIcon;
}
label.setIcon(icon);
return this;
}
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}
CheckListExample frame = new CheckListExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class CheckRenderer extends JPanel implements TreeCellRenderer {
protected JCheckBox check;
protected TreeLabel label;
public CheckRenderer() {
setLayout(null);
add(check = new JCheckBox());
add(label = new TreeLabel());
check.setBackground(UIManager.getColor("Tree.textBackground"));
label.setForeground(UIManager.getColor("Tree.textForeground"));
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean isSelected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
String stringValue = tree.convertValueToText(value, isSelected,
expanded, leaf, row, hasFocus);
setEnabled(tree.isEnabled());
check.setSelected(((CheckNode) value).isSelected());
label.setFont(tree.getFont());
label.setText(stringValue);
label.setSelected(isSelected);
label.setFocus(hasFocus);
if (leaf) {
label.setIcon(UIManager.getIcon("Tree.leafIcon"));
} else if (expanded) {
label.setIcon(UIManager.getIcon("Tree.openIcon"));
} else {
label.setIcon(UIManager.getIcon("Tree.closedIcon"));
}
return this;
}
public Dimension getPreferredSize() {
Dimension d_check = check.getPreferredSize();
Dimension d_label = label.getPreferredSize();
return new Dimension(d_check.width + d_label.width,
(d_check.height < d_label.height ? d_label.height
: d_check.height));
}
public void doLayout() {
Dimension d_check = check.getPreferredSize();
Dimension d_label = label.getPreferredSize();
int y_check = 0;
int y_label = 0;
if (d_check.height < d_label.height) {
y_check = (d_label.height - d_check.height) / 2;
} else {
y_label = (d_check.height - d_label.height) / 2;
}
check.setLocation(0, y_check);
check.setBounds(0, y_check, d_check.width, d_check.height);
label.setLocation(d_check.width, y_label);
label.setBounds(d_check.width, y_label, d_label.width, d_label.height);
}
public void setBackground(Color color) {
if (color instanceof ColorUIResource)
color = null;
super.setBackground(color);
}
public class TreeLabel extends JLabel {
boolean isSelected;
boolean hasFocus;
public TreeLabel() {
}
public void setBackground(Color color) {
if (color instanceof ColorUIResource)
color = null;
super.setBackground(color);
}
public void paint(Graphics g) {
String str;
if ((str = getText()) != null) {
if (0 < str.length()) {
if (isSelected) {
g.setColor(UIManager
.getColor("Tree.selectionBackground"));
} else {
g.setColor(UIManager.getColor("Tree.textBackground"));
}
Dimension d = getPreferredSize();
int imageOffset = 0;
Icon currentI = getIcon();
if (currentI != null) {
imageOffset = currentI.getIconWidth()
+ Math.max(0, getIconTextGap() - 1);
}
g.fillRect(imageOffset, 0, d.width - 1 - imageOffset,
d.height);
if (hasFocus) {
g.setColor(UIManager
.getColor("Tree.selectionBorderColor"));
g.drawRect(imageOffset, 0, d.width - 1 - imageOffset,
d.height - 1);
}
}
}
super.paint(g);
}
public Dimension getPreferredSize() {
Dimension retDimension = super.getPreferredSize();
if (retDimension != null) {
retDimension = new Dimension(retDimension.width + 3,
retDimension.height);
}
return retDimension;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
public void setFocus(boolean hasFocus) {
this.hasFocus = hasFocus;
}
}
}
class CheckNode extends DefaultMutableTreeNode {
public final static int SINGLE_SELECTION = 0;
public final static int DIG_IN_SELECTION = 4;
protected int selectionMode;
protected boolean isSelected;
public CheckNode() {
this(null);
}
public CheckNode(Object userObject) {
this(userObject, true, false);
}
public CheckNode(Object userObject, boolean allowsChildren,
boolean isSelected) {
super(userObject, allowsChildren);
this.isSelected = isSelected;
setSelectionMode(DIG_IN_SELECTION);
}
public void setSelectionMode(int mode) {
selectionMode = mode;
}
public int getSelectionMode() {
return selectionMode;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
if ((selectionMode == DIG_IN_SELECTION) && (children != null)) {
Enumeration e = children.elements();
while (e.hasMoreElements()) {
CheckNode node = (CheckNode) e.nextElement();
node.setSelected(isSelected);
}
}
}
public boolean isSelected() {
return isSelected;
}
// If you want to change "isSelected" by CellEditor,
/*
* public void setUserObject(Object obj) { if (obj instanceof Boolean) {
* setSelected(((Boolean)obj).booleanValue()); } else {
* super.setUserObject(obj); } }
*/
}
Check List Example 2
// Example from http://www.crionics.ru/products/opensource/faq/swing_ex/SwingExamples.html
//File:CheckListExample2.java
/* (swing1.1.1beta2) */
import java.awt.BorderLayout;
import java.awt.ruponent;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
/**
* @version 1.0 04/26/99
*/
public class CheckListExample2 extends JFrame {
public CheckListExample2() {
super("CheckList Example");
String[] strs = { "swing", "home", "basic", "metal", "JList" };
final JList list = new JList(createData(strs));
list.setCellRenderer(new CheckListRenderer());
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setBorder(new EmptyBorder(0, 4, 0, 0));
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int index = list.locationToIndex(e.getPoint());
CheckableItem item = (CheckableItem) list.getModel()
.getElementAt(index);
item.setSelected(!item.isSelected());
Rectangle rect = list.getCellBounds(index, index);
list.repaint(rect);
}
});
JScrollPane sp = new JScrollPane(list);
final JTextArea textArea = new JTextArea(3, 10);
JScrollPane textPanel = new JScrollPane(textArea);
JButton printButton = new JButton("print");
printButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ListModel model = list.getModel();
int n = model.getSize();
for (int i = 0; i < n; i++) {
CheckableItem item = (CheckableItem) model.getElementAt(i);
if (item.isSelected()) {
textArea.append(item.toString());
textArea.append(System.getProperty("line.separator"));
}
}
}
});
JButton clearButton = new JButton("clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText("");
}
});
JPanel panel = new JPanel(new GridLayout(2, 1));
panel.add(printButton);
panel.add(clearButton);
getContentPane().add(sp, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.EAST);
getContentPane().add(textPanel, BorderLayout.SOUTH);
}
private CheckableItem[] createData(String[] strs) {
int n = strs.length;
CheckableItem[] items = new CheckableItem[n];
for (int i = 0; i < n; i++) {
items[i] = new CheckableItem(strs[i]);
}
return items;
}
class CheckableItem {
private String str;
private boolean isSelected;
public CheckableItem(String str) {
this.str = str;
isSelected = false;
}
public void setSelected(boolean b) {
isSelected = b;
}
public boolean isSelected() {
return isSelected;
}
public String toString() {
return str;
}
}
class CheckListRenderer extends JCheckBox implements ListCellRenderer {
public CheckListRenderer() {
setBackground(UIManager.getColor("List.textBackground"));
setForeground(UIManager.getColor("List.textForeground"));
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean hasFocus) {
setEnabled(list.isEnabled());
setSelected(((CheckableItem) value).isSelected());
setFont(list.getFont());
setText(value.toString());
return this;
}
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}
CheckListExample2 frame = new CheckListExample2();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300, 200);
frame.setVisible(true);
}
}
DND Drag and drop List
/**
* This is an example of a component, which serves as a DragSource as
* well as Drop Target.
* To illustrate the concept, JList has been used as a droppable target
* and a draggable source.
* Any component can be used instead of a JList.
* The code also contains debugging messages which can be used for
* diagnostics and understanding the flow of events.
*
* @version 1.0
*/
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListModel;
public class DNDList extends JList implements DropTargetListener, DragSourceListener, DragGestureListener
{
/**
* enables this component to be a dropTarget
*/
DropTarget dropTarget = null;
/**
* enables this component to be a Drag Source
*/
DragSource dragSource = null;
/**
* constructor - initializes the DropTarget and DragSource.
*/
public DNDList( ListModel dataModel )
{
super( dataModel );
dropTarget = new DropTarget( this, this );
dragSource = new DragSource();
dragSource.createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this );
}
/**
* is invoked when you are dragging over the DropSite
*
*/
public void dragEnter( DropTargetDragEvent event )
{
// debug messages for diagnostics
System.out.println( "dragEnter" );
event.acceptDrag( DnDConstants.ACTION_MOVE );
}
/**
* is invoked when you are exit the DropSite without dropping
*
*/
public void dragExit( DropTargetEvent event )
{
System.out.println( "dragExit" );
}
/**
* is invoked when a drag operation is going on
*
*/
public void dragOver( DropTargetDragEvent event )
{
System.out.println( "dragOver" );
}
/**
* a drop has occurred
*
*/
public void drop( DropTargetDropEvent event )
{
try
{
Transferable transferable = event.getTransferable();
// we accept only Strings
if( transferable.isDataFlavorSupported( DataFlavor.stringFlavor ) )
{
event.acceptDrop( DnDConstants.ACTION_MOVE );
String s = ( String )transferable.getTransferData( DataFlavor.stringFlavor );
addElement( s );
event.getDropTargetContext().dropComplete( true );
}
else
{
event.rejectDrop();
}
}
catch( Exception exception )
{
System.err.println( "Exception" + exception.getMessage() );
event.rejectDrop();
}
}
/**
* is invoked if the use modifies the current drop gesture
*
*/
public void dropActionChanged( DropTargetDragEvent event )
{
}
/**
* a drag gesture has been initiated
*
*/
public void dragGestureRecognized( DragGestureEvent event )
{
Object selected = getSelectedValue();
if( selected != null )
{
StringSelection text = new StringSelection( selected.toString() );
// as the name suggests, starts the dragging
dragSource.startDrag( event, DragSource.DefaultMoveDrop, text, this );
}
else
{
System.out.println( "nothing was selected" );
}
}
/**
* this message goes to DragSourceListener, informing it that the dragging
* has ended
*
*/
public void dragDropEnd( DragSourceDropEvent event )
{
if( event.getDropSuccess() )
{
removeElement();
}
}
/**
* this message goes to DragSourceListener, informing it that the dragging
* has entered the DropSite
*
*/
public void dragEnter( DragSourceDragEvent event )
{
System.out.println( " dragEnter" );
}
/**
* this message goes to DragSourceListener, informing it that the dragging
* has exited the DropSite
*
*/
public void dragExit( DragSourceEvent event )
{
System.out.println( "dragExit" );
}
/**
* this message goes to DragSourceListener, informing it that the dragging is
* currently ocurring over the DropSite
*
*/
public void dragOver( DragSourceDragEvent event )
{
System.out.println( "dragExit" );
}
/**
* is invoked when the user changes the dropAction
*
*/
public void dropActionChanged( DragSourceDragEvent event )
{
System.out.println( "dropActionChanged" );
}
/**
* adds elements to itself
*
*/
public void addElement( Object s )
{
( ( DefaultListModel )getModel() ).addElement( s.toString() );
}
/**
* removes an element from itself
*/
public void removeElement()
{
( ( DefaultListModel )getModel() ).removeElement( getSelectedValue() );
}
}
Editable List Example
// Example from http://www.crionics.ru/products/opensource/faq/swing_ex/SwingExamples.html
/*
* (swing1.1)
*/
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
/**
* @version 1.0 12/24/98
*/
public class EditableListExample extends JFrame {
public EditableListExample() {
super("Editable List Example");
String[] data = { "a", "b", "c", "d", "e", "f", "g" };
JList list = new JList(data);
JScrollPane scrollList = new JScrollPane(list);
scrollList.setMinimumSize(new Dimension(100, 80));
Box listBox = new Box(BoxLayout.Y_AXIS);
listBox.add(scrollList);
listBox.add(new JLabel("JList"));
DefaultTableModel dm = new DefaultTableModel();
Vector dummyHeader = new Vector();
dummyHeader.addElement("");
dm.setDataVector(strArray2Vector(data), dummyHeader);
JTable table = new JTable(dm);
table.setShowGrid(false);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollTable = new JScrollPane(table);
scrollTable.setColumnHeader(null);
scrollTable.setMinimumSize(new Dimension(100, 80));
Box tableBox = new Box(BoxLayout.Y_AXIS);
tableBox.add(scrollTable);
tableBox.add(new JLabel("JTable"));
Container c = getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.X_AXIS));
c.add(listBox);
c.add(new JSeparator(SwingConstants.VERTICAL));
//c.add(new JLabel("test"));
//c.add(new JSeparator(SwingConstants.HORIZONTAL));
c.add(tableBox);
setSize(220, 130);
setVisible(true);
}
private Vector strArray2Vector(String[] str) {
Vector vector = new Vector();
for (int i = 0; i < str.length; i++) {
Vector v = new Vector();
v.addElement(str[i]);
vector.addElement(v);
}
return vector;
}
public static void main(String[] args) {
final EditableListExample frame = new EditableListExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
Mouse Roll over List
ToolTip List Example
// Example from http://www.crionics.ru/products/opensource/faq/swing_ex/SwingExamples.html
//File:ToolTipListExample.java
/* (swing1.1.1) */
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
/**
* @version 1.0 08/26/99
*/
public class ToolTipListExample extends JFrame {
public ToolTipListExample() {
super("ToolTip Example");
String[][] strs = { { "Acinonyx jutatus", "Cheetah" },
{ "Panthera leo", "Lion" }, { "Canis lupus", "Wolf" },
{ "Lycaon pictus", "Llycaon" }, { "Vulpes Vulpes", "Fox" } };
JList list = new JList(createItems(strs)) {
public String getToolTipText(MouseEvent e) {
int index = locationToIndex(e.getPoint());
if (-1 < index) {
ToolTipItem item = (ToolTipItem) getModel().getElementAt(
index);
return item.getToolTipText();
} else {
//return super.getToolTipText();
return null;
}
}
};
list.setToolTipText("");
getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);
}
Object[] createItems(String[][] strs) {
ToolTipItem[] items = new ToolTipItem[strs.length];
for (int i = 0; i < strs.length; i++) {
items[i] = new ToolTipItem(strs[i][0], strs[i][1]);
}
return items;
}
class ToolTipItem {
String obj;
String toolTipText;
public ToolTipItem(String obj, String text) {
this.obj = obj;
this.toolTipText = text;
}
public String getToolTipText() {
return toolTipText;
}
public String toString() {
return obj;
}
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}
ToolTipListExample frame = new ToolTipListExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(140, 150);
frame.setVisible(true);
}
}