Advertisement
Guest User

(Java, SSCE included) SwingWorker gives "Cannot find symbol"

a guest
Aug 11th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. /* Gives these compilation errors:
  2. foo.java:21: error: cannot find symbol
  3.                                         this.things.put(thing.id, thing);
  4.                                             ^
  5.   symbol: variable things
  6. foo.java:22: error: cannot find symbol
  7.                                         this.jtp.setText(String.valueOf(this.thi
  8. ngs.size()));
  9.                                                                             ^
  10.   symbol: variable things
  11. foo.java:22: error: cannot find symbol
  12.                                         this.jtp.setText(String.valueOf(this.thi
  13. ngs.size()));
  14.                                             ^
  15.   symbol: variable jtp
  16. 3 errors
  17. */
  18.  
  19. import javax.swing.*;
  20. import java.awt.*;
  21. import java.awt.event.*;
  22. import java.util.HashMap;
  23. import java.util.List;
  24.  
  25. public class foo extends JFrame {
  26.     HashMap<Integer,Thing> Things = new HashMap<Integer,Thing>();
  27.     JTextPane jtp = new JTextPane();
  28.  
  29.     public void findThings() {
  30.         SwingWorker<HashMap<Integer,Thing>,Thing> sw1 = new SwingWorker<HashMap<Integer,Thing>,Thing>() {
  31.             protected HashMap<Integer,Thing> doInBackground() {
  32.                 HashMap<Integer,Thing> things = new HashMap<Integer,Thing>();
  33.                 Thread.sleep(1000);
  34.                 return things;
  35.             }
  36.  
  37.             protected void process(List<Thing> chunks) {
  38.                 for(Thing thing : chunks) {
  39.                     this.things.put(thing.id, thing);
  40.                     this.jtp.setText(String.valueOf(this.things.size()));
  41.                 }
  42.             }
  43.         };
  44.  
  45.         sw1.execute();
  46.     }
  47.  
  48.     public foo() {
  49.         super();
  50.         setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
  51.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52.  
  53.         JButton jbtn = new JButton("findThings()");
  54.         jbtn.addActionListener(new ActionListener() {
  55.             public void actionPerformed(ActionEvent e) {
  56.                 findThings();
  57.             }
  58.         });
  59.         add(jbtn);
  60.         this.jtp.setPreferredSize(new Dimension(300,300));
  61.         add(this.jtp); 
  62.  
  63.         setLocationRelativeTo(null);
  64.         pack();
  65.         setVisible(true);
  66.     }
  67.  
  68.     public static void foo(String[] args) {
  69.         SwingUtilities.invokeLater(new Runnable() {
  70.             public void run() {
  71.                 new foo();
  72.             }
  73.         });
  74.     }
  75.  
  76.     private class Thing {
  77.         public Thing() {
  78.             id = 100;
  79.             name = "Thing's name";
  80.         }
  81.  
  82.         Integer id = null;
  83.         String name = null;
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement