gui problem

Joined
Feb 6, 2012
Messages
2
Reaction score
0
I am having trouble updating a TextArea. The only way I can get the TextArea to update causes a new jpanel to appear. How do I fix this and get the TextArea to properly update thanks I am using methods refresh and refresh2 at the bottom of the code.

Code:
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    
    String path;
    String path2;
    JButton btnLoadMpu = new JButton("");
    JButton btnLoadMpu2 = new JButton("");
    JButton btnFind1 = new JButton("Find");
    JButton btnFind2 = new JButton("Find");
    TextArea textArea_1 = new TextArea();
    TextArea textArea_2 = new TextArea();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        //EventQueue.invokeLater(new Runnable() {
            //public void run() {
            //    try {
                    MPUComp frame = new MPUComp();
                    frame.setVisible(true);
            //    } catch (Exception e) {
            //        e.printStackTrace();
            //    }
        //    }
    //    });
    }

    /**
     * Create the frame.
     */
    public MPUComp() {
        setTitle("Mpu Finder");
        
        ImageIcon LoadIco = new ImageIcon(getClass().getResource("load.png"));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 799, 680);
        

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        
        
        
        btnFind1.setBounds(250, 27, 68, 23);
        //btnFind1.setIcon(LoadIco);
        
        contentPane.add(btnFind1);
        
        
        btnLoadMpu.setBounds(328, 27, 36, 22);
        btnLoadMpu.setIcon(LoadIco);
        
        contentPane.add(btnLoadMpu);
        
        btnFind2.setBounds(642, 27, 68, 23);
        contentPane.add(btnFind2);
        
        btnLoadMpu2.setBounds(720, 28, 36, 22);
        btnLoadMpu2.setIcon(LoadIco);
        
        contentPane.add(btnLoadMpu2);
        
        menu();
        
        
    }
    
    public void menu()
    {
        //Menu Items
          JMenuBar menubar = new JMenuBar();
          JMenu filemenu = new JMenu("File");
          filemenu.add(new JSeparator());
          JMenuItem fileItem1 = new JMenuItem("Open MPU #1");
          JMenuItem fileItem2 = new JMenuItem("Open MPU #2");
          JMenuItem fileItem3 = new JMenuItem("Exit");
          fileItem3.setBorder(UIManager.getBorder("FormattedTextField.border"));
          JButton openButton = new JButton("Open a File...");
         
          filemenu.add(fileItem1);
          filemenu.add(fileItem2);
          filemenu.add(fileItem3);
          menubar.add(filemenu);

          //contentPane.add(menubar);
            setJMenuBar(menubar);
          
        //end Menu Items
          
        //text fields
            textField = new JTextField();
            textField.setBounds(10, 27, 230, 20);
            contentPane.add(textField);
            textField.setColumns(10);
            
            textField_1 = new JTextField();
            textField_1.setColumns(10);
            textField_1.setBounds(402, 27, 230, 20);
            contentPane.add(textField_1);  
            
            textArea_1.setBounds(10, 70, 380, 514);
            contentPane.add(textArea_1);
            

            textArea_2.setBounds(402, 70, 380, 514);
            contentPane.add(textArea_2);
          
          
          
        //action listeners  
            // exit method
            class MenuActionListener2 implements ActionListener {    
                public void actionPerformed(ActionEvent e)
                {
                    System.exit(0);
                }
                }
            
            
            class MenuActionListener implements ActionListener {    
            public void actionPerformed(ActionEvent e)
            {
                textArea_1.setText("Open MPU File"); 
                System.out.println("Selected: " + e.getActionCommand());
                // create a file chooser 
                final JFileChooser fc = new JFileChooser(); 
                
                // in response to a button click: 
                int returnVal = fc.showDialog(null, "Open MPU File");
                 
                if (returnVal == JFileChooser.APPROVE_OPTION) { 
                  File file = fc.getSelectedFile(); 
                  File currentPath = fc.getCurrentDirectory();
                  //String currentPathStr = currentPath.getPath();
                  // if it is a text file
                  if (file.getName().endsWith("xml")) { 
                    // load the image using the given file path
                    path = file.getPath();
                    System.out.println(path);
                    //setPath(path);
                                  try {
                                    Thread.sleep(2);
                                } catch (InterruptedException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                                }
                                  textArea_1.setText("MPU #1 Loaded");

                                  SwingUtilities.invokeLater(new Runnable() {
                                        public void run() {
                                            //mpuChecker mC = new mpuChecker();
                                            //mC.CheckMpu(path);
                                        }
                                    });
                    
                          }
                                
                 else { 
                     textArea_1.setText("Open command cancelled by user."); 
                }
            }
            }
        }
            
            class MenuActionListener5 implements ActionListener {    
                public void actionPerformed(ActionEvent e)
                {
                    textArea_2.setText("Open MPU #2 File"); 
                    System.out.println("Selected: " + e.getActionCommand());
                    // create a file chooser 
                    final JFileChooser fc = new JFileChooser(); 
                    
                    // in response to a button click: 
                    int returnVal2 = fc.showDialog(null, "Open MPU File");
                     
                    if (returnVal2 == JFileChooser.APPROVE_OPTION) { 
                      File file2 = fc.getSelectedFile(); 
                      File currentPath = fc.getCurrentDirectory();
                      //String currentPathStr = currentPath.getPath();
                      // if it is a text file
                      if (file2.getName().endsWith("xml")) { 
                        // load the image using the given file path
                        path2 = file2.getPath();
                        System.out.println(path2);
                        //setPath(path);
                                      try {
                                        Thread.sleep(2);
                                    } catch (InterruptedException e1) {
                                        // TODO Auto-generated catch block
                                        e1.printStackTrace();
                                    }
                                      textArea_2.setText("MPU #2 Loaded");
//
//                                      SwingUtilities.invokeLater(new Runnable() {
//                                            public void run() {
//                                                //mpuChecker mC = new mpuChecker();
//                                                //mC.CheckMpu(path);
//                                            }
//                                        });
                        
                              }
                                    
                     else { 
                         textArea_2.setText("Open command cancelled by user."); 
                    }
                }
                }
            }        
                
            
             fileItem1.addActionListener(new MenuActionListener());
             fileItem2.addActionListener(new MenuActionListener5());
             fileItem3.addActionListener(new MenuActionListener2());
             btnLoadMpu.addActionListener(new MenuActionListener());
             btnLoadMpu2.addActionListener(new MenuActionListener5());
             btnFind1.addActionListener(new MenuActionListener3());
             btnFind2.addActionListener(new MenuActionListener4());
    }
    
    class MenuActionListener3 implements ActionListener {    
        public void actionPerformed(ActionEvent e)
        {
            mpuChecker mC = new mpuChecker();
            mC.CheckMpu(path, textField.getText(),1);
            //mC.findMid(textField.getText());
        }
    }
    class MenuActionListener4 implements ActionListener {    
        public void actionPerformed(ActionEvent e)
        {
            mpuChecker mC = new mpuChecker();
            mC.CheckMpu(path2, textField_1.getText(),2);
            //mC.findMid(textField.getText());
        }
    }    
    
    public void refresh(String pane1) {
        textArea_1.append(pane1 + "\n");
        contentPane.revalidate();
        contentPane.repaint();
        setVisible(true);
    }
    public void refresh2(String pane1) {
            textArea_2.append(pane1 + "\n");
            contentPane.revalidate();
            contentPane.repaint();
            setVisible(true);
        
    }
     }
 
Joined
Feb 6, 2012
Messages
2
Reaction score
0
anyone I think I am not calling the right textarea and not refreshing the right jpanel?

Since I called them in the constructor I am not sure how to call it in other methods in the same class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top