Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-04-2008, 04:10 PM
Member
 
Join Date: Jul 2008
Posts: 26
aneesahamedaa is on a distinguished road
JList issues
Hi all,
In the attached code, I need to have the following options
1) get the name(eg;first.doc) of the selected item/items.
2) When I right click on top of a particular item, I need to have that item alone selected.
3) I need to have no selection for elements once I click outside the list.
I am attaching the code I developed, here.
Kindly help implementing these.
Any help is greatly appreciated.
Code:
import java.awt.BorderLayout; import java.awt.Component; import java.awt.Image; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; import javax.swing.DefaultListCellRenderer; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.ListCellRenderer; import javax.swing.UIManager; import javax.swing.filechooser.FileSystemView; import sun.awt.shell.ShellFolder; public class MainClass { public static void main(String args[]) throws Exception { MainClass mc = new MainClass(); Object elements[][] = { {"first.doc",mc.getIcone(mc.getExtension("first.doc".toUpperCase()))}, {"second.pdf",mc.getIcone(mc.getExtension("second.pdf".toUpperCase()))}, {"third.txt",mc.getIcone(mc.getExtension("third.txt".toUpperCase()))} , {"first.doc",mc.getIcone(mc.getExtension("first.doc".toUpperCase()))}, {"second.pdf",mc.getIcone(mc.getExtension("second.pdf".toUpperCase()))}, {"third.txt",mc.getIcone(mc.getExtension("third.txt".toUpperCase()))} }; JFrame frame = new JFrame("Trial"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JList jlist = new JList(elements); ListCellRenderer renderer = new ComplexCellRenderer(); jlist.setCellRenderer(renderer); jlist.setLayoutOrientation(JList.HORIZONTAL_WRAP); jlist.setVisibleRowCount(-1); JScrollPane scrollPane = new JScrollPane(jlist); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); } public String getExtension(String name) { if(name.lastIndexOf(".")!=-1) { String extensionPossible = name.substring(name.lastIndexOf(".")+1, name.length()); if(extensionPossible.length()>6) { return ""; } else { return extensionPossible; } } else return ""; } public Icon getIcone(String extension) { File file; String cheminIcone = ""; if(((System.getProperties().get("os.name").toString()).startsWith("Mac"))) cheminIcone = System.getProperties().getProperty("file.separator"); else if(((System.getProperties().get("os.name").toString()).startsWith("Linux"))) cheminIcone = "/"+"tmp"+"/BoooDrive-"+System.getProperty("user.name")+"/"; else cheminIcone = System.getenv("TEMP") + System.getProperties().getProperty("file.separator"); File repIcone = new File(cheminIcone); if(!repIcone.exists()) repIcone.mkdirs(); try { if(extension.equals("FOLDER")) { file = new File(cheminIcone + "icon"); file.mkdir(); } else { file = new File(cheminIcone + "icon." + extension.toLowerCase()); file.createNewFile(); } Icon icone = getSystemIcon(file); file.delete(); return icone; } catch (IOException e){ } return null; } public static Icon getSystemIcon(File f) { if (f != null) { Class fsv = FileSystemView.class; try { Method m = fsv.getDeclaredMethod("getShellFolder", new Class[]{File.class}); m.setAccessible(true); ShellFolder sf = (ShellFolder) m.invoke(FileSystemView.getFileSystemView(), f); Image img = sf.getIcon(true); if (img != null) { return new ImageIcon(img, sf.getFolderType()); } else { return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon"); } } catch (Exception e) {e.printStackTrace();} } return null; } } class ComplexCellRenderer implements ListCellRenderer { protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer(); public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Icon icon = null; String theText = null; JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); renderer.setVerticalTextPosition(JLabel.BOTTOM); renderer.setHorizontalTextPosition(JLabel.CENTER); if (value instanceof Object[]) { Object values[] = (Object[]) value; theText = (String) values[0]; icon = (Icon) values[1]; } else { } renderer.setText(theText); renderer.setIcon(icon); return renderer; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-09-2008, 01:16 PM
Member
 
Join Date: Jul 2008
Posts: 26
aneesahamedaa is on a distinguished road
Please provide help.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-09-2008, 10:08 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
Click in the south textField to remove focus from the JList.
Code:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.lang.reflect.Method; import javax.swing.*; import javax.swing.event.*; import javax.swing.filechooser.FileSystemView; import sun.awt.shell.ShellFolder; public class MC { public static void main(String[] args) throws Exception { MC mc = new MC(); Object[][] elements = { { "first.doc", mc.getIcone(mc.getExtension("first.doc".toUpperCase())) }, { "second.pdf", mc.getIcone(mc.getExtension("second.pdf".toUpperCase())) }, {"third.txt", mc.getIcone(mc.getExtension("third.txt".toUpperCase())) }, {"first.doc", mc.getIcone(mc.getExtension("first.doc".toUpperCase())) }, {"second.pdf", mc.getIcone(mc.getExtension("second.pdf".toUpperCase())) }, {"third.txt", mc.getIcone(mc.getExtension("third.txt".toUpperCase())) } }; JFrame frame = new JFrame("Trial"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JList jlist = new JList(elements); ListCellRenderer renderer = new ComplexCellRenderer(); jlist.setCellRenderer(renderer); jlist.setLayoutOrientation(JList.HORIZONTAL_WRAP); jlist.setVisibleRowCount(-1); // 2) When I right click on top of a particular item, // I need to have that item alone selected. jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 1) get the name (eg, first.doc) of the selected item jlist.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if(!e.getValueIsAdjusting()) { Object value = ((JList)e.getSource()).getSelectedValue(); if(value != null) { String name = (String)((Object[])value)[0]; System.out.println("name = " + name); } } } }); // 3) I need to have no selection for elements once // I click outside the list. jlist.addFocusListener(new FocusAdapter() { public void focusLost(FocusEvent e) { ((JList)e.getSource()).clearSelection(); } }); JScrollPane scrollPane = new JScrollPane(jlist); frame.add(scrollPane, BorderLayout.CENTER); frame.add(new JTextField(), BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); } public String getExtension(String name) { if(name.lastIndexOf(".")!=-1) { String extensionPossible = name.substring(name.lastIndexOf(".")+1, name.length()); if(extensionPossible.length()>6) { return ""; } else { return extensionPossible; } } else return ""; } public Icon getIcone(String extension) { File file; String cheminIcone = ""; if(((System.getProperties().get("os.name") .toString()).startsWith("Mac"))) cheminIcone = System.getProperties().getProperty("file.separator"); else if(((System.getProperties().get("os.name") .toString()).startsWith("Linux"))) cheminIcone = "/"+"tmp"+"/BoooDrive-"+ System.getProperty("user.name")+"/"; else cheminIcone = System.getenv("TEMP") + System.getProperties().getProperty("file.separator"); File repIcone = new File(cheminIcone); if(!repIcone.exists()) repIcone.mkdirs(); try { if(extension.equals("FOLDER")) { file = new File(cheminIcone + "icon"); file.mkdir(); } else { file = new File(cheminIcone + "icon." + extension.toLowerCase()); file.createNewFile(); } Icon icone = getSystemIcon(file); file.delete(); return icone; } catch (IOException e){ } return null; } @SuppressWarnings("unchecked") public static Icon getSystemIcon(File f) { if (f != null) { Class fsv = FileSystemView.class; try { Method m = fsv.getDeclaredMethod("getShellFolder", new Class[] {File.class}); m.setAccessible(true); ShellFolder sf = (ShellFolder) m.invoke( FileSystemView.getFileSystemView(), f); Image img = sf.getIcon(true); if (img != null) { return new ImageIcon(img, sf.getFolderType()); } else { return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon"); } } catch (Exception e) {e.printStackTrace();} } return null; } } class ComplexCellRenderer implements ListCellRenderer { protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer(); public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Icon icon = null; String theText = null; JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); renderer.setVerticalTextPosition(JLabel.BOTTOM); renderer.setHorizontalTextPosition(JLabel.CENTER); if (value instanceof Object[]) { Object[] values = (Object[]) value; theText = (String) values[0]; icon = (Icon) values[1]; } else { System.out.println("value = " + value.toString()); } renderer.setText(theText); renderer.setIcon(icon); return renderer; } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-10-2008, 02:16 PM
Member
 
Join Date: Jul 2008
Posts: 26
aneesahamedaa is on a distinguished road
Thanks alot.
That was an awesome work hardwired.
Also,
How can I reduce the width of the selected item.
Just see the link JList Selected Area
I need to somehow reduce the width of the selected area. What should be done?
Anees

Last edited by aneesahamedaa : 09-10-2008 at 03:30 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-11-2008, 01:27 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
How can I reduce the width of the selected item.
The JList api has setFixedWidth/Height methods you could use to move the list items closer together.
If you want the selectionColor to cover only the rectangle enclosing the icon and string then you may have to look at some plaf code. You might start with the paintImpl and paintCell methods in the BasicListUI class. You could try calculating the rectangle union of the icon and text rectangles and paint it in the backgroundSelection color.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
About JList hungleon88 Advanced Java 5 08-30-2008 11:24 PM
JList and JPanels JetsYanks New To Java 3 05-12-2008 06:56 AM
JList problem zizou147 Advanced Java 1 04-17-2008 10:50 AM
Help with JList Albert NetBeans 1 07-13-2007 05:42 PM
add a jlist column Alan JCreator 1 05-28-2007 06:51 AM


All times are GMT +3. The time now is 11:53 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org