0

I have two classes. 1.One class contains all the GUI code generated by Netbeans GUI builder (call it GUI class) 2.Another class contains some code containing methods (SaveTraffic which extends SwingWorker).

In my SaveTraffic , I create a frame which just pops up and come in front of my GUI when I run my application. I want to add this frame to my GUI at the specific location where I want to place it. How is this possible? This is my code of SaveTraffic

public class SaveTraffic extends SwingWorker<Void, String> {

    public static int k = 0;
    public GUI f = new GUI();
    public static Frame u = new Frame();
    public static JTextArea h = new JTextArea();
    JScrollPane scrollPane2 = new JScrollPane(h, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    @Override
    public Void doInBackground() throws IOException {
        u.setSize(950, 200);
        u.setLocation(0,550);
        u.add(scrollPane2);
        u.setVisible(true);
        while (f.num() != 2) {
            {
              publish("Hello World");
            }
        }
        return null;
    } //end main function

    @Override
    public void process(List<String> chunks) {
        for (final String text : chunks) {
            Runnable r = new Runnable() {

                @Override
                public void run() {
                    h.setLineWrap(true);
                    h.append(text + "\n");
                }
            };
            new Thread(r).start();
        }
    }

    @Override
    public void done() {
        System.out.println("I am DONE");

    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
Xara
  • 8,748
  • 16
  • 52
  • 82

3 Answers3

2

I see several problems in your code:

  1. You are looking for JDialog and not a new frame. Make sure to pass the correct Dialog owner to the constructor of JDialog. You can choose wheter the dialog is modal or not through a parameter on the constructor or by calling setModal().
  2. You are starting new Threads inside the SwingWorker.publish method. Those thread modify the UI. Althout JTextArea.append is Thread-safe, I would still not use multiple Threads to update the JTextArea. It is unecessary and if some day you add some calls that are not Thread-safe you will end up on a deadlock.
  3. You are adding components to your frame, setting its size and making it visible in the SwingWorker.doInBackground() which is not run on the EDT. Instead move that call before executing your SwingWorker, inside the EDT.
  4. No need to constantly set the line wrap to true on the textarea. Just calling it once is enough.
  5. I am not sure what 'k' is used for, but I would beware of static variables.
  6. Why do you make your Frame and your TextArea static?

(Optional) I would also consider adding a constructor in SaveTraffic and initialize most of the code there.

Community
  • 1
  • 1
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
2
  • don't built Swing Gui inside SwingWorker's methods, SwingWorker is designated to add / modify / change value in JComponent(s) that already exist

  • add process() or publish() to the SwingWorker, inside this method put value or append text the JTextArea periodically

  • for "Monitoring" SwingWorker's statuses add PropertyChangeListener

  • simple example

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

You make a class, that extends JPanel. Then you do all your code in there, and bind it with your method class.

OmniOwl
  • 5,477
  • 17
  • 67
  • 116