Source Code Cross Referenced for ListDemo.java in  » Web-Framework » webonswing » examples » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » webonswing » examples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //WebOnSwing - Web Application Framework
002:        //Copyright (C) 2003 Fernando Damian Petrola
003:        //	
004:        //This library is free software; you can redistribute it and/or
005:        //modify it under the terms of the GNU Lesser General Public
006:        //License as published by the Free Software Foundation; either
007:        //version 2.1 of the License, or (at your option) any later version.
008:        //	
009:        //This library is distributed in the hope that it will be useful,
010:        //but WITHOUT ANY WARRANTY; without even the implied warranty of
011:        //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012:        //Lesser General Public License for more details.
013:        //	
014:        //You should have received a copy of the GNU Lesser General Public
015:        //License along with this library; if not, write to the Free Software
016:        //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
017:
018:        package examples;
019:
020:        import java.awt.*;
021:        import java.awt.event.*;
022:
023:        import javax.swing.*;
024:        import javax.swing.event.*;
025:
026:        public class ListDemo extends JFrame {
027:            public static void main(String s[]) {
028:                JFrame frame = new ListDemo();
029:
030:                frame.addWindowListener(new WindowAdapter() {
031:                    public void windowClosing(WindowEvent e) {
032:                        System.exit(0);
033:                    }
034:                });
035:
036:                frame.pack();
037:                frame.setVisible(true);
038:            }
039:
040:            public ListDemo() {
041:                getContentPane().add(new ListDemoPanel());
042:            }
043:
044:            public static class ListDemoPanel extends JPanel implements 
045:                    ListSelectionListener {
046:                JList list;
047:                DefaultListModel listModel;
048:
049:                private static final String hireString = "Hire";
050:                private static final String fireString = "Fire";
051:                JButton fireButton;
052:                JTextField employeeName;
053:
054:                public ListDemoPanel() {
055:                    listModel = new DefaultListModel();
056:                    listModel.addElement("Alison Huml");
057:                    listModel.addElement("Kathy Walrath");
058:                    listModel.addElement("Lisa Friendly");
059:                    listModel.addElement("Mary Campione");
060:
061:                    //Create the list and put it in a scroll pane
062:                    list = new JList(listModel);
063:                    list
064:                            .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
065:                    list.setSelectedIndex(0);
066:                    list.addListSelectionListener(this );
067:                    JScrollPane listScrollPane = new JScrollPane(list);
068:
069:                    JButton hireButton = new JButton(hireString);
070:                    hireButton.setActionCommand(hireString);
071:                    hireButton.addActionListener(new HireListener());
072:
073:                    fireButton = new JButton(fireString);
074:                    fireButton.setActionCommand(fireString);
075:                    fireButton.addActionListener(new FireListener());
076:
077:                    employeeName = new JTextField(10);
078:                    employeeName.addActionListener(new HireListener());
079:                    String name = listModel.getElementAt(
080:                            list.getSelectedIndex()).toString();
081:                    employeeName.setText(name);
082:
083:                    //Create a panel that uses FlowLayout (the default).
084:                    JPanel buttonPane = new JPanel();
085:                    buttonPane.add(employeeName);
086:                    buttonPane.add(hireButton);
087:                    buttonPane.add(fireButton);
088:
089:                    Container contentPane = this ;
090:                    setLayout(new BorderLayout());
091:                    contentPane.add(listScrollPane, BorderLayout.CENTER);
092:                    contentPane.add(buttonPane, BorderLayout.SOUTH);
093:                }
094:
095:                class FireListener implements  ActionListener {
096:                    public void actionPerformed(ActionEvent e) {
097:                        //This method can be called only if
098:                        //there's a valid selection
099:                        //so go ahead and remove whatever's selected.
100:                        int index = list.getSelectedIndex();
101:                        listModel.remove(index);
102:
103:                        int size = listModel.getSize();
104:
105:                        if (size == 0) {
106:                            //Nobody's left, disable firing.
107:                            fireButton.setEnabled(false);
108:
109:                        } else {
110:                            //Adjust the selection.
111:                            if (index == listModel.getSize()) //removed item in last position
112:                                index--;
113:                            list.setSelectedIndex(index); //otherwise select same index
114:                        }
115:                    }
116:                }
117:
118:                //This listener is shared by the text field and the hire button
119:                class HireListener implements  ActionListener {
120:                    public void actionPerformed(ActionEvent e) {
121:
122:                        //User didn't type in a name...
123:                        if (employeeName.getText().equals("")) {
124:                            Toolkit.getDefaultToolkit().beep();
125:                            return;
126:                        }
127:
128:                        int index = list.getSelectedIndex();
129:                        int size = listModel.getSize();
130:
131:                        //If no selection or if item in last position is selected,
132:                        //add the new hire to end of list, and select new hire.
133:                        if (index == -1 || (index + 1 == size)) {
134:                            listModel.addElement(employeeName.getText());
135:                            list.setSelectedIndex(size);
136:
137:                            //Otherwise insert the new hire after the current selection,
138:                            //and select new hire.
139:                        } else {
140:                            listModel.insertElementAt(employeeName.getText(),
141:                                    index + 1);
142:                            list.setSelectedIndex(index + 1);
143:                        }
144:                    }
145:                }
146:
147:                public void valueChanged(ListSelectionEvent e) {
148:                    if (e.getValueIsAdjusting() == false) {
149:
150:                        if (list.getSelectedIndex() == -1) {
151:                            //No selection, disable fire button.
152:                            fireButton.setEnabled(false);
153:                            employeeName.setText("");
154:
155:                        } else {
156:                            //Selection, update text field.
157:                            fireButton.setEnabled(true);
158:                            String name = list.getSelectedValue().toString();
159:                            employeeName.setText(name);
160:                        }
161:                    }
162:                }
163:
164:            }
165:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.