2013-12-17 24 views
1

我試圖從另一個類打印到我的JTextArea。我在我的主類Risk_Mgnt_Manager裏面有ActivityLogger調用方法Alert,這是JTextArea所在的位置。我能夠將字符串傳入此方法並打印,但不會將附加或setText附加到JTextArea。我錯過了什麼?無法從另一個類打印到JTextArea

我的目標是讓不同的類將消息發送到類ActivityLogger,然後將它發送到JTextArea。

任何示例表示感謝,並提前謝謝。

主要類

package risk_mgnt_manager; 

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.IOException; 
import java.sql.SQLException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.xml.parsers.ParserConfigurationException; 
import org.xml.sax.SAXException; 

public class Risk_Mgnt_Manager extends JFrame{ 
    boolean begin = false; 
    String message = null; 
    JTextArea text = new JTextArea(); 
    JButton Start = new JButton("Start");//exit program button 
    JButton End = new JButton("End");//Ok button executes message creation 
    JButton Exit = new JButton("Exit Program"); 

public void Alert(String a){ 
    System.out.println(a); // This is printing correctly 
    text.append(a + "\n"); // why won't this display the string? 
} 

public Risk_Mgnt_Manager(){ 
    text.setEditable(false); 
    text.setWrapStyleWord(true); 
    text.setLineWrap(true); 
    JScrollPane scroll = new JScrollPane(text); 

    setLayout(new GridLayout(2, 3, 5, 5)); //LayoutManager Setup 
    JPanel myPanel = new JPanel(new GridLayout(3,0)); 
    //JPanel myPanel2 = new JPanel(new GridLayout(1, 1)); 
    //JPanel myPanel3 = new JPanel(new GridLayout(1, 1)); 
    JPanel myPanel4 = new JPanel(new GridLayout(1, 1)); 

    myPanel.add(new JLabel("Start Automated Processes: ")); 
    myPanel.add(Start); 

    myPanel.add(new JLabel("End Automated Processes: ")); 
    myPanel.add(End); 

    myPanel.add(new JLabel(" ")); 
    myPanel.add(Exit); 
    myPanel4.add(text); 

    Start.addActionListener(new startActions());//Listener for button 1 
    End.addActionListener(new stopActions());//Listener for button 2 
    Exit.addActionListener(new Quit());//Listener for button 2 

    add(myPanel); 
    //add(myPanel2); 
    //add(myPanel3); 
    add(myPanel4); 

} 

public void StartAutomation(boolean start) throws SAXException,  ParserConfigurationException, IOException, SQLException{ 
     //calls test class 
     Test t = new Test(); 
     t.mainTest(begin); 

     //ignore these classes 
     // Step one import settlement data from FIX 1 settlement tables 
     ImportSettles tbl = new ImportSettles(); 
     //tbl.DataTransfer(begin); 

     // Step two import Real-Time price data from t_span_price on FIX 1 
     ImportSpanPrice tbl2 = new ImportSpanPrice(); 
     //tbl2.DataTransfer1(begin); 

     // Step three import from xml file 
     ImportTradeData tbl3 = new ImportTradeData(); 
     //tbl3.parseXML(begin); 

     // Step four not used as of 11/26/2013 
     ImportFirmRpt tbl4 = new ImportFirmRpt(); 

     // Step five import poew.csv file 
     ImportPOEW tbl5 = new ImportPOEW(); 
     //tbl5.csvImportPOEW(begin); 

     // Step six import paycollect.csv file 
     ImportPaycollect tbl6 = new ImportPaycollect(); 
     //tbl6.csvImportPaycollect(begin); 

     // Step seven import data from RISK 1 
     ImportSecDeposit tbl7 = new ImportSecDeposit(); 
     //tbl7.DataTransfer2(begin); 

     // Step 8 import FCM financial info, WinJammer not used as of 11/26/2013 
     ImportFCM tbl8 = new ImportFCM(); 


     // Step nine import CGM_post.csv file 
     ImportCGMPost tbl9 = new ImportCGMPost(); 
     //tbl9.csvImportCGMPost(begin); 

     // Step ten import RM_Intraday_paycollect.csv 
     ImportIntraday tbl10 = new ImportIntraday(); 
     //tbl10.csvImportIntra(begin); 
} 

private static void ProjectFrame(){ 
    Risk_Mgnt_Manager projectFrame = new Risk_Mgnt_Manager(); 
    projectFrame.setSize(500, 300); //JFrame size set 
    projectFrame.setLocationRelativeTo(null); //JFrame centered to center of screen 
    projectFrame.setTitle("Automation Control"); //JFrame Title 
    projectFrame.setVisible(true);//JFrame is visible upon start of program 
    projectFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 



public static void main(String[] args) { 
    ProjectFrame(); 
} 
static class Quit implements ActionListener { 
     public void actionPerformed (ActionEvent e) { 
      //Once Exit JButton is pressed the program exits 
      System.exit(0); 
     } 
    } 
public class startActions implements ActionListener { 
     public void actionPerformed (ActionEvent e) { 
      //Once Exit JButton is pressed the program exits 
      begin = true; 
      try { 
       StartAutomation(begin); 
      } catch (SAXException ex) { 
       Logger.getLogger(Risk_Mgnt_Manager.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (ParserConfigurationException ex) { 
       Logger.getLogger(Risk_Mgnt_Manager.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (IOException ex) { 
       Logger.getLogger(Risk_Mgnt_Manager.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (SQLException ex) { 
       Logger.getLogger(Risk_Mgnt_Manager.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 
public class stopActions implements ActionListener { 
     public void actionPerformed (ActionEvent e) { 
      //Once Exit JButton is pressed the program exits 
      begin = false; 
      try { 
       StartAutomation(begin); 
      } catch (SAXException ex) { 
       Logger.getLogger(Risk_Mgnt_Manager.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (ParserConfigurationException ex) { 
       Logger.getLogger(Risk_Mgnt_Manager.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (IOException ex) { 
       Logger.getLogger(Risk_Mgnt_Manager.class.getName()).log(Level.SEVERE, null, ex); 
      } catch (SQLException ex) { 
       Logger.getLogger(Risk_Mgnt_Manager.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 
} 

測試類

package risk_mgnt_manager; 

import java.util.Date; 

/** 
* 
* @author bgilbert 
*/ 
public class Test { 
    public void mainTest(boolean a){ 
     ActivityLogger act = new ActivityLogger(); 
     act.logger("Testing message reporting " + new Date(), 1, true); 
} 

} 

ActivityLogger類

package risk_mgnt_manager; 
/** 
* 
* @author MLaMeyer 
*/ 
public class ActivityLogger{ 
    private String message; 

    // this will perform different purposes once I can print to JTextArea 
    public void logger(String log, int type, boolean execution){ 
    if (execution == true) { 

       message = log; 
    } 
      if (execution == false) { 

       message = log; 
    } 

      print(); 

} 
    // calls method Alert in main class and passes the string correctly 
    public void print(){ 
     Risk_Mgnt_Manager m = new Risk_Mgnt_Manager(); 
     m.Alert(message); 
    } 
} 

回答

1

你的程序打印出的其他類,只是沒有在顯示的對象:

public void print(){ 
    Risk_Mgnt_Manager m = new Risk_Mgnt_Manager(); 
    m.Alert(message); 
} 

當你創建一個新的Risk_Mgnt_Manager,你做到這一點,創建一個新的完全獨特的Risk_Mgnt_Manager對象,一個不顯示。打印到它將不會影響顯示的一個。

解決方案是將對您的記錄器類的引用傳遞給實際顯示的Risk_Mgnt_Manager對象。

public class ActivityLogger{ 
    private String message; 
    private Risk_Mgnt_Manager m; // ***** added 

    public ActivityLogger(Risk_Mgnt_Manager m) { 
     this.m = m; // ****** added 
    } 

    // this will perform different purposes once I can print to JTextArea 
    public void logger(String log, int type, boolean execution){ 
    if (execution == true) { 

       message = log; 
    } 
      if (execution == false) { 

       message = log; 
    } 

      print(); 

} 
    // calls method Alert in main class and passes the string correctly 
    public void print(){ 
     // Risk_Mgnt_Manager m = new Risk_Mgnt_Manager(); 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      m.Alert(message); 
     } 
     }); 
    } 
} 

不管你做什麼,不要試圖解決這個我做任何靜態的那條路會導致痛苦。

+0

現在ActivityLogger需要一個參數,我還必須通過引用將對象傳遞給我的Test類嗎? – MadMardigans

+0

@ user1789170:對不起,我不確定我是否理解你的問題。你能重新說一下嗎? –

+0

@ user1789170:底線是您不能登錄到任何一個Risk_Mgnt_Manager對象,而是必須在對當前顯示的Risk_Mgnt_Manager對象的引用上調用alert(...)。這將取決於你如何獲得參考連接,但它必須以某種方式完成。 –

1

您需要更新單獨Thread的UI,我的意思是UI相關的操作應在運行事件派發線程。添加構造在ActivityLogger類像氣墊船的解決方案,然後嘗試,

SwingUtilities.invokeLater(new Runnable() { 
    public void run() { 
     text.append(a+"\n"); 
    } 
}); 
+0

這是不是問題,再看看代碼 – Reimeus

+1

@Reimeus:但一個很好的建議,但1 + –

+1

@HovercraftFullOfEels好的建議,但不一定要添加。 – Makky

0

首先使框架在您的constructor中可見。

public Risk_Mgnt_Manager(){ 

setVisible(true); 

} 

然後根據Hovercraft的解決方案通過參考。