Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
JList – A real confusion.
Darshak Patel
Greenhorn
Posts: 1
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Please have a look on the following code. Try to run it. Check the code and let me know do we have any other good way to achieve the same thing?
Code:
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Graphics2D; import javax.swing.DefaultListCellRenderer; import javax.swing.DefaultListModel; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.ListCellRenderer; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; import javax.swing.border.LineBorder; import javax.swing.border.TitledBorder; public class SamplesList { static int previndex = -1; static JList jlist2 = null; public static void main(String args[]) { JFrame frame = new JFrame("Samples"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultListModel model = new DefaultListModel(); model.ensureCapacity(100); for (int i = 0; i < 100; i++) { model.addElement(Integer.toString(i)); } jlist2 = new JList(model); jlist2.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jList1MouseClicked(evt); //repaint(); } public void mouseExited(java.awt.event.MouseEvent evt) { jList1MouseExited(evt); //repaint(); } }); jlist2.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseMoved(java.awt.event.MouseEvent evt) { jList1MouseMoved(evt); //repaint(); } }); JScrollPane scrollPane2 = new JScrollPane(jlist2); frame.add(scrollPane2, BorderLayout.CENTER); frame.setSize(300, 350); frame.setVisible(true); jlist2.ensureIndexIsVisible(50); jlist2.removeSelectionInterval(previndex,previndex); } private static void jList1MouseMoved(java.awt.event.MouseEvent evt) { Object o = evt.getSource(); javax.swing.JList oj=(javax.swing.JList)o; int indexis = oj.locationToIndex( evt.getPoint()); System.out.println("Hello ::::::"+previndex+"::::"+indexis); if(previndex!=indexis){ oj.setSelectedIndex(indexis); oj.setSelectionBackground(java.awt.Color.BLUE); }else{ oj.setSelectedIndex(previndex); oj.setSelectionBackground(java.awt.Color.GREEN); } } private static void jList1MouseClicked(java.awt.event.MouseEvent evt) { Object o = evt.getSource(); javax.swing.JList oj=(javax.swing.JList)o; int indexis = oj.locationToIndex( evt.getPoint()); previndex = indexis; oj.setSelectedIndex(indexis); oj.setSelectionBackground(java.awt.Color.GREEN); oj.repaint(); //oj.removeSelectionInterval(previndex,previndex); changeModel(); } private static void jList1MouseExited(java.awt.event.MouseEvent evt) { // TODO add your handling code here: Object o = evt.getSource(); javax.swing.JList oj=(javax.swing.JList)o; if(previndex!=-1){ oj.setSelectedIndex(previndex); oj.setSelectionBackground(java.awt.Color.GREEN); } }//GEN-LAST:event_jList1MouseExited private static void changeModel(){ DefaultListModel model = new DefaultListModel(); model.ensureCapacity(100); for (int i = 0; i < 100; i++) { if(i==previndex){ model.addElement("<html><body bgcolor=\"#00FF00\" width=\"300\" height=\"20\" "+Integer.toString(i)+"</body></html>"); }else{ model.addElement(Integer.toString(i)); } } jlist2.setModel(model); } }
Regards,
Darshak.
pete stein
Bartender
Posts: 1561
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Please check out this policy before cross-posting in many,
many
fora (I've counted at least 3 so far -- are there more?):
BeForthrightWhenCrossPostingToOtherSites
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Loading/Saving a Listbox
about JList
Character based Search
Resultset
dnd
More...