Avatar of NaveenKhanna
NaveenKhanna

asked on 

Acess the JtextArea control from other class running in another Thread

Hello

1)My Application Should Run Automatically(Unattended mode) -->solution i have used is Timer
2)My application Must Have tray icon------->Solution i have used Tray icon Class
3)In My application there is 2 control JtextArea displays status message of two classes Ex."Send.java" "Receive.Java"---Solution i have used jtextarea.append("Hello");

problem
1) When Program starts running i am not able to acess the User interface (Form)
2) i want acess the Trayicon where it contains popup "Exit" "Start"

I have tried SwingUtilities.invokeand wait(runnablee)
But it is not working as for me...?

//code snippet for timer
actionlistner t_ac=
 t_ac = new ActionListener() {
 
            public void actionPerformed(ActionEvent e) {
//                jButton1ActionPerformed(null);
//                jButton4ActionPerformed(null);
                //t_sendTRD.run();
                // t_RecvTRD.run();
                formWindowOpened(null);
            }
        };
        m_MainTmr = new Timer(1000, t_ac);
        m_MainTmr.start();
 
//To draw trayIcon
 
  private void DrawTrayIcon() {
        SystemTray tray = SystemTray.getSystemTray();
        Image image = Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\XML\\Globe.jpg");
        final JPopupMenu popup = new JPopupMenu();
        JMenuItem item = new JMenuItem("Exit");
        JMenuItem ite = new JMenuItem("jjklk");
 
        popup.add(item);
        popup.add(ite);
        TrayIcon trayIcon = new TrayIcon(image, "Right Click", null);
 
        javax.swing.Action t_acc = new javax.swing.Action() {
 
            public Object getValue(String key) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
 
            public void putValue(String key, Object value) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
 
            public void setEnabled(boolean b) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
 
            public boolean isEnabled() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
 
            public void addPropertyChangeListener(PropertyChangeListener listener) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
 
            public void removePropertyChangeListener(PropertyChangeListener listener) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
 
            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equalsIgnoreCase("EXIT")) {
                    System.exit(0);
                }
            }
        };
 
        ite.addActionListener(t_acc);
        item.addActionListener(t_acc);
 
        trayIcon.addMouseListener(new MouseListener() {
 
            public void mouseClicked(MouseEvent e) {
            }
 
            public void mousePressed(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    if (e.isPopupTrigger()) {
                        try {
                            popup.setLocation(e.getX(), e.getY());
                            popup.setInvoker(popup);
                            popup.setVisible(true);
                            Thread.sleep(MouseEvent.MOUSE_EVENT_MASK);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(ApprovalUtility.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
 
            public void mouseReleased(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    if (e.isPopupTrigger()) {
                        popup.setLocation(e.getX(), e.getY());
                        popup.setInvoker(popup);
                        popup.setVisible(true);
                    }
                }
            }
 
            public void mouseEntered(MouseEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
 
            public void mouseExited(MouseEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println("Can't add to tray");
        }
    }

Open in new window

Java

Avatar of undefined
Last Comment
ksivananth

8/22/2022 - Mon