简体   繁体   中英

Java GUI - displaying text in a new window from another method

I have the following interface structure: a frame in which I can browse and choose some files (in a class file), when I press a button it reads the files (in a separate class file) and sends them for some processing (in another class file, the 3rd). The processing itself is irrelevant for this question.

When I press the processing button mentioned before, a new window (frame) launches. In that window I have a textarea in which I want to display some console output while the processing takes place, and then another text.

The method that draws the second frame is located in the 3rd class file, the processing one, is as following:

public static void drawScenario(){
    final JPanel mainPanel2 = new JPanel();
    JPanel firstLine = new JPanel();
    JPanel secLine = new JPanel();

    mainPanel2.setLayout(new BoxLayout(mainPanel2, BoxLayout.Y_AXIS));
    mainPanel2.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
    mainPanel2.add(Box.createVerticalGlue());

    firstLine.setLayout(new BoxLayout(firstLine, BoxLayout.X_AXIS));
    firstLine.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
    firstLine.add(Box.createVerticalGlue());

    secLine.setLayout(new BoxLayout(secLine, BoxLayout.X_AXIS));
    secLine.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
    secLine.add(Box.createVerticalGlue());

    JTextArea textArea = new JTextArea("", 10, 40);
    textArea.setLineWrap(true);
    JScrollPane scrollPane = new JScrollPane(textArea); 
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    textArea.setEditable(false);

    JLabel label1 = new JLabel("Processing results:");

    firstLine.add(Box.createRigidArea(new Dimension(5,0)));
    firstLine.add(label1);
    firstLine.add(Box.createRigidArea(new Dimension(5,0)));

    secLine.add(textArea);
    secLine.add(Box.createRigidArea(new Dimension(5,0)));

    mainPanel2.add(firstLine);
    mainPanel2.add(Box.createRigidArea(new Dimension(0, 30)));
    mainPanel2.add(secLine);
    mainPanel2.add(Box.createRigidArea(new Dimension(0, 20)));

    JFrame frame = new JFrame("Test results");
    frame.setSize(400, 300);
    frame.setLocation(50,50);
    frame.setVisible( true );
    frame.add(mainPanel2);
    frame.pack();

}

The processing method ( public static void compare(String txt1, String txt2) ) is also located in the same file, below the drawScenario() method. My question is, how do I print text from compare() to the TextArea of the drawScenario() method?

Also, the window doesn't completely draw itself (it displays a black column of sorts and doesn't draw the TextArea inside it) during the processing although I call drawScenario() before compare(). Is there a way I can fix that?

Thank you!

As for your first question, let textArea be an instance variable instead. As for your second, I don't see anything wrong with the code you have shown.

The text area should be visible from drawScenario and compare methods for you to be able to update it. Here is how you could do this (with minimal changes to your code)

public class MyClass {
     private static JTextArea textArea = new JTextArea("", 10, 40);
     public void static drawScenario(){
         // ...
     }
     public static void compare(String txt1, String txt2){
         textArea.setText("here is some text...");
     }    
}

Now if you want a proper implementation of your requirement, you should do this:

  1. The drawScenario and compare and all UI elements can be made non static
  2. Create your own frame (that extends JFrame ) class which will encapsulates all UI elements creation. In your drawScenario() you could just create the frame and display it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM