943,940 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 14344
  • Java RSS
Nov 22nd, 2006
0

updating a JList

Expand Post »
So this is my first attempt at a GUI in swing, and so far things seem to have been going somewhat smoothly.
However I am un-able to update a JList named objectsL.
I imagine this is because I have already added it to a panel?
So, now Im am very lost, and any help would be appreciated.
(Oh and Im very sorry for my messy code)

Java Syntax (Toggle Plain Text)
  1. import javax.swing.AbstractButton;
  2. import javax.swing.JButton;
  3. import javax.swing.JPanel;
  4. import javax.swing.JFrame;
  5. import javax.swing.ImageIcon;
  6. import javax.swing.*;
  7. import javax.swing.event.*;
  8. import java.awt.Dimension;
  9.  
  10.  
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.KeyEvent;
  14. import java.awt.*;
  15. import javax.swing.border.*;
  16.  
  17. import javax.swing.*;
  18. import java.awt.event.*;
  19. import java.awt.BorderLayout;
  20. import java.awt.Dimension;
  21. import java.util.*;
  22.  
  23. /*
  24.  * ButtonDemo.java requires the following files:
  25.  * images/right.gif
  26.  * images/middle.gif
  27.  * images/left.gif
  28.  */
  29. public class test3 extends JPanel
  30. implements ActionListener, ListSelectionListener, KeyListener, FocusListener{
  31. private master m;
  32. JList objectsL;// = new JList();
  33. JList typesL;// = new JList();
  34. JTextField url;
  35. JTextField keyWord;
  36. DefaultListModel listModel;
  37. public test3() {
  38. m =new master();
  39. JPanel pane = new JPanel();
  40. pane.setLayout(new GridBagLayout());
  41. url = new JTextField("URL goes here");
  42. url.setForeground(Color.gray);
  43. JButton search = new JButton("Search");
  44.  
  45. search.setActionCommand("search");
  46. search.addActionListener(this);
  47. search.setToolTipText("Opens URL, and searches for links.");
  48.  
  49.  
  50. GridBagConstraints c = new GridBagConstraints();
  51. c.fill = GridBagConstraints.HORIZONTAL;
  52. c.gridx = 0;
  53. c.gridy = 0;
  54. c.weightx= 0.9;
  55. pane.add(url,c);
  56. c.gridx=1;
  57. //c.fill = 0;
  58. c.weightx= 0.1;
  59. pane.add(search,c);
  60. //relating to lists------------------
  61. listModel = new DefaultListModel();
  62. listModel.addElement("try");
  63. String[] objects = {};//"empty","empty","empty"};
  64. objectsL = new JList(objects);
  65. objectsL.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  66. objectsL.setLayoutOrientation(JList.VERTICAL);
  67. objectsL.setVisibleRowCount(20);
  68.  
  69.  
  70.  
  71. String[] types ={"All","All image types","jpg","png","bmp","tiff","gif","All video types","mpg","mpeg","avi","rmvb","mp4"};
  72. //DefaultListModel temp = new DefaultListModel();
  73. //for(int i=0;i<types.length;i++)
  74. // temp.addElement(types[i]);
  75. typesL = new JList(types);
  76. typesL.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  77. typesL.setLayoutOrientation(JList.VERTICAL);
  78. typesL.setVisibleRowCount(1);
  79.  
  80. objectsL.setToolTipText("List of Links");
  81. typesL.setToolTipText("Common file types");
  82.  
  83. objectsL.addListSelectionListener(this);
  84. typesL.addListSelectionListener(this);
  85.  
  86. JScrollPane typesLP = new JScrollPane(typesL);
  87. JScrollPane objectsLP = new JScrollPane(objectsL);
  88.  
  89.  
  90. JPanel lists = new JPanel();
  91. lists.setLayout(new GridLayout(1,2));
  92.  
  93. Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
  94.  
  95. objectsL.setBorder(BorderFactory.createTitledBorder(
  96. loweredetched, "Links"));
  97. typesL.setBorder(BorderFactory.createTitledBorder(
  98. loweredetched, "File types"));
  99.  
  100. lists.add(objectsLP);
  101. lists.add(typesLP);
  102. c.gridy=1;
  103. c.gridx=0;
  104. c.gridwidth=2;
  105. c.fill = GridBagConstraints.BOTH;
  106. c.weightx=1.0;
  107. c.weighty=1.0;
  108. pane.add(lists,c);
  109. //--------------------------------------------------------------------------
  110. JPanel otPan = new JPanel();
  111. otPan.setLayout(new GridLayout(1,2,30,0));
  112. JButton downloadMulti = new JButton("Download");
  113. downloadMulti.setActionCommand("download");
  114. downloadMulti.addActionListener(this);
  115. downloadMulti.setToolTipText("Use when downloading multiple items.");
  116. keyWord = new JTextField("Keyword");
  117. keyWord.addKeyListener(this);
  118. keyWord.addFocusListener(this);
  119. keyWord.setForeground(Color.gray);
  120. otPan.add(downloadMulti,c);
  121. otPan.add(keyWord,c);
  122.  
  123. c.fill= GridBagConstraints.HORIZONTAL;
  124. c.anchor = GridBagConstraints.PAGE_END;
  125. c.gridy = 2;
  126. c.weighty=0;
  127. pane.add(otPan,c);
  128.  
  129. add(pane);
  130.  
  131. String[] temp = {"updated","updated"};
  132. objectsL = new JList(temp);
  133. }
  134. //---------------------Button evnts----------------------------
  135. public void actionPerformed(ActionEvent e) {
  136. if ("search".equals(e.getActionCommand()))
  137. {
  138. System.out.println(url.getText());
  139.  
  140. if(m.findObjects(url.getText()))
  141. {
  142. updateList(objectsL, m.getObjects());
  143. System.out.println(m.getObjects().get(1));
  144. }
  145. }
  146. else if("download".equals(e.getActionCommand()))
  147. System.out.println("Downloading");
  148. }
  149. //--------------------------------------------------------------
  150. //------------------------List Events---------------------------
  151. public void valueChanged(ListSelectionEvent e)
  152. {
  153. if (e.getValueIsAdjusting())
  154. return;
  155. else
  156. {
  157. JList theList = (JList)e.getSource();
  158. if(theList==objectsL)
  159. System.out.println("objects was hit");
  160. else if(theList == typesL)
  161. System.out.println("types was hit");
  162. }
  163. }
  164. //--------------------------------------------------------------
  165. //------------------------Key events---------------------------
  166. public void keyTyped(KeyEvent e) {
  167. System.out.println("you entered a key");
  168. updateList(objectsL, m.getObjects(keyWord.getText()));
  169. }
  170. public void keyPressed(KeyEvent e) {
  171.  
  172. }
  173.  
  174. public void keyReleased(KeyEvent e) {
  175.  
  176. }
  177. //--------------------------------------------------------------
  178. //------------------------Focus events-------------------------
  179. public void focusGained(FocusEvent e)
  180. {//both text field use focus listener
  181. if(url.equals(e.getSource()))//does nto work yet
  182. {
  183. url.setForeground(Color.black);
  184. url.selectAll();
  185. }
  186. else
  187. {
  188. keyWord.setForeground(Color.black);
  189. keyWord.selectAll();
  190.  
  191. }
  192. }
  193.  
  194. public void focusLost(FocusEvent e) {
  195.  
  196. }
  197. //--------------------------------------------------------------
  198.  
  199. public void updateList(JList list, ArrayList objs)//your standard list update method
  200. {
  201. DefaultListModel listModel = (DefaultListModel)list.getModel();
  202. listModel.clear();
  203. for(int i=0; i<objs.size(); i++)
  204. {
  205. listModel.addElement((String)objs.get(i));
  206. }
  207. }
  208.  
  209. /** Returns an ImageIcon, or null if the path was invalid. */
  210. protected static ImageIcon createImageIcon(String path) {
  211. java.net.URL imgURL = test3.class.getResource(path);
  212. if (imgURL != null) {
  213. return new ImageIcon(imgURL);
  214. } else {
  215. System.err.println("Couldn't find file: " + path);
  216. return null;
  217. }
  218. }
  219.  
  220. /**
  221.   * Create the GUI and show it. For thread safety,
  222.   * this method should be invoked from the
  223.   * event-dispatching thread.
  224.   */
  225. private static void createAndShowGUI() {
  226.  
  227. //Create and set up the window.
  228. JFrame frame = new JFrame("test3");
  229. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  230.  
  231. //Create and set up the content pane.
  232. test3 newContentPane = new test3();
  233. newContentPane.setLayout(new GridLayout());
  234. newContentPane.setOpaque(true); //content panes must be opaque
  235. frame.setContentPane(newContentPane);
  236.  
  237. //Display the window.
  238. frame.pack();
  239. frame.setVisible(true);
  240. }
  241.  
  242. public static void main(String[] args) {
  243. //Schedule a job for the event-dispatching thread:
  244. //creating and showing this application's GUI.
  245. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  246. public void run() {
  247. createAndShowGUI();
  248. }
  249. });
  250. }
  251. }
Similar Threads
Reputation Points: 13
Solved Threads: 4
Light Poster
kimbokasteniv is offline Offline
46 posts
since Nov 2006
Nov 22nd, 2006
0

Re: updating a JList

Let me focus the question.
When updating a JList would this code do the trick:

Java Syntax (Toggle Plain Text)
  1. public void updateList(JList list, ArrayList objs)
  2. {
  3. DefaultListModel listModel = (DefaultListModel)list.getModel();
  4. listModel.clear();
  5. for(int i=0; i<objs.size(); i++)
  6. {
  7. listModel.addElement((String)objs.get(i));
  8. }
  9. }

That was the method I used to update a JList when using a GUI based off of BreezySwing. However this throws a classCastException error, when used in a GUI based off swing.
Reputation Points: 13
Solved Threads: 4
Light Poster
kimbokasteniv is offline Offline
46 posts
since Nov 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: JTable Cell Renderers
Next Thread in Java Forum Timeline: need some help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC