| | |
updating a JList
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 23
Reputation:
Solved Threads: 0
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)
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)
import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.ImageIcon; import javax.swing.*; import javax.swing.event.*; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.*; import javax.swing.border.*; import javax.swing.*; import java.awt.event.*; import java.awt.BorderLayout; import java.awt.Dimension; import java.util.*; /* * ButtonDemo.java requires the following files: * images/right.gif * images/middle.gif * images/left.gif */ public class test3 extends JPanel implements ActionListener, ListSelectionListener, KeyListener, FocusListener{ private master m; JList objectsL;// = new JList(); JList typesL;// = new JList(); JTextField url; JTextField keyWord; DefaultListModel listModel; public test3() { m =new master(); JPanel pane = new JPanel(); pane.setLayout(new GridBagLayout()); url = new JTextField("URL goes here"); url.setForeground(Color.gray); JButton search = new JButton("Search"); search.setActionCommand("search"); search.addActionListener(this); search.setToolTipText("Opens URL, and searches for links."); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; c.weightx= 0.9; pane.add(url,c); c.gridx=1; //c.fill = 0; c.weightx= 0.1; pane.add(search,c); //relating to lists------------------ listModel = new DefaultListModel(); listModel.addElement("try"); String[] objects = {};//"empty","empty","empty"}; objectsL = new JList(objects); objectsL.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); objectsL.setLayoutOrientation(JList.VERTICAL); objectsL.setVisibleRowCount(20); String[] types ={"All","All image types","jpg","png","bmp","tiff","gif","All video types","mpg","mpeg","avi","rmvb","mp4"}; //DefaultListModel temp = new DefaultListModel(); //for(int i=0;i<types.length;i++) // temp.addElement(types[i]); typesL = new JList(types); typesL.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); typesL.setLayoutOrientation(JList.VERTICAL); typesL.setVisibleRowCount(1); objectsL.setToolTipText("List of Links"); typesL.setToolTipText("Common file types"); objectsL.addListSelectionListener(this); typesL.addListSelectionListener(this); JScrollPane typesLP = new JScrollPane(typesL); JScrollPane objectsLP = new JScrollPane(objectsL); JPanel lists = new JPanel(); lists.setLayout(new GridLayout(1,2)); Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED); objectsL.setBorder(BorderFactory.createTitledBorder( loweredetched, "Links")); typesL.setBorder(BorderFactory.createTitledBorder( loweredetched, "File types")); lists.add(objectsLP); lists.add(typesLP); c.gridy=1; c.gridx=0; c.gridwidth=2; c.fill = GridBagConstraints.BOTH; c.weightx=1.0; c.weighty=1.0; pane.add(lists,c); //-------------------------------------------------------------------------- JPanel otPan = new JPanel(); otPan.setLayout(new GridLayout(1,2,30,0)); JButton downloadMulti = new JButton("Download"); downloadMulti.setActionCommand("download"); downloadMulti.addActionListener(this); downloadMulti.setToolTipText("Use when downloading multiple items."); keyWord = new JTextField("Keyword"); keyWord.addKeyListener(this); keyWord.addFocusListener(this); keyWord.setForeground(Color.gray); otPan.add(downloadMulti,c); otPan.add(keyWord,c); c.fill= GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.PAGE_END; c.gridy = 2; c.weighty=0; pane.add(otPan,c); add(pane); String[] temp = {"updated","updated"}; objectsL = new JList(temp); } //---------------------Button evnts---------------------------- public void actionPerformed(ActionEvent e) { if ("search".equals(e.getActionCommand())) { System.out.println(url.getText()); if(m.findObjects(url.getText())) { updateList(objectsL, m.getObjects()); System.out.println(m.getObjects().get(1)); } } else if("download".equals(e.getActionCommand())) System.out.println("Downloading"); } //-------------------------------------------------------------- //------------------------List Events--------------------------- public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting()) return; else { JList theList = (JList)e.getSource(); if(theList==objectsL) System.out.println("objects was hit"); else if(theList == typesL) System.out.println("types was hit"); } } //-------------------------------------------------------------- //------------------------Key events--------------------------- public void keyTyped(KeyEvent e) { System.out.println("you entered a key"); updateList(objectsL, m.getObjects(keyWord.getText())); } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } //-------------------------------------------------------------- //------------------------Focus events------------------------- public void focusGained(FocusEvent e) {//both text field use focus listener if(url.equals(e.getSource()))//does nto work yet { url.setForeground(Color.black); url.selectAll(); } else { keyWord.setForeground(Color.black); keyWord.selectAll(); } } public void focusLost(FocusEvent e) { } //-------------------------------------------------------------- public void updateList(JList list, ArrayList objs)//your standard list update method { DefaultListModel listModel = (DefaultListModel)list.getModel(); listModel.clear(); for(int i=0; i<objs.size(); i++) { listModel.addElement((String)objs.get(i)); } } /** Returns an ImageIcon, or null if the path was invalid. */ protected static ImageIcon createImageIcon(String path) { java.net.URL imgURL = test3.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("test3"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. test3 newContentPane = new test3(); newContentPane.setLayout(new GridLayout()); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
•
•
Join Date: Nov 2006
Posts: 23
Reputation:
Solved Threads: 0
Let me focus the question.
When updating a JList would this code do the trick:
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.
When updating a JList would this code do the trick:
Java Syntax (Toggle Plain Text)
public void updateList(JList list, ArrayList objs) { DefaultListModel listModel = (DefaultListModel)list.getModel(); listModel.clear(); for(int i=0; i<objs.size(); i++) { listModel.addElement((String)objs.get(i)); } }
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.
![]() |
Similar Threads
- Updating drivers (Windows NT / 2000 / XP)
- Updating a JList Renderer (Java)
- IE slow after updating Norton Virus Defs (Web Browsers)
- HELP! Updating Sound Card Driver and now Windows Only boots up Black Screen! :( (Windows NT / 2000 / XP)
- updating 2 HTML tables on one PHP page (PHP)
Other Threads in the Java Forum
- Previous Thread: JTable Cell Renderers
- Next Thread: need some help
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class client code color component count database derby design eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui homework html ide if_statement image integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel julia keyword linux list macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object open-source os problem producer program programming project projectideas property read recursion reference replaysolutions ria scanner search server set size sms sort sourcelabs splash sql sqlite stop string swing testautomation threads transforms tree ui unicode validation windows





