troubleshooting Question

Java expert help needed - Code attached

Avatar of Sreejith22
Sreejith22Flag for India asked on
JavaJava EE
11 Comments1 Solution395 ViewsLast Modified:
Hi experts,

Please find attached the file which can be run to view the output. When you view the output, you can notice two text fields and a browse button corresponding to each text field. I need to modify this in such a way that, I do not want to browse and find the input file, instead I just need to drag and drop a file to this textfield, such that, the file path comes there. Also, I should have the option to type the path of the file.
Any help in this regard which provides me the perfect solution would be much appreciated with points.

Regards,
Sree
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class TextAcceleratorExample extends JFrame{
       JTextField textField = new JTextField(50);
       JTextField textField_2 = new JTextField(50);
       JButton button_Extract = new JButton("Extract");
       JButton button_Cancel = new JButton("Cancel");
       String text_1 ;
       String text_2;
     JButton button;
     JButton button_2;
     private JPanel getContent() {
            // Try changing columns argument.
		     
             button = new JButton("Browse");
		      
             button_2 = new JButton("Browse");
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc= new GridBagConstraints();
            gbc.insets = new Insets(5,15,5,0);
            gbc.weightx = 1.0;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.anchor = GridBagConstraints.CENTER;
            panel.add(new JLabel("Enter Input Path  : "), gbc);
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1.0;
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.FIRST_LINE_END;
            gbc.insets = new Insets(10,0,5,5);
            //textField.setPreferredSize(new Dimension(100,20));
            panel.add(textField, gbc);
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.WEST;
            button.addActionListener(new OpenL());
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.insets = new Insets(5,5,5,5);
            panel.add(button, gbc);
		        
            GridBagConstraints gbc_2 = new GridBagConstraints();
            gbc.insets = new Insets(5,15,5,0);
            gbc_2.weightx = 1.0;
            gbc_2.gridy = 1;
            gbc_2.gridx = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.anchor = GridBagConstraints.CENTER;
            panel.add(new JLabel("Enter Output Path : "), gbc_2);
            gbc_2.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(10,0,5,5);
            gbc.weightx = 1.0;
            gbc_2.gridx = 1;
            gbc_2.gridy = 1;
            gbc.anchor = GridBagConstraints.WEST;
           // textField_2.setPreferredSize(new Dimension(100,20));
            panel.add(textField_2, gbc_2);
            gbc_2.fill = GridBagConstraints.NONE;
            gbc_2.anchor = GridBagConstraints.EAST;
            button_2.addActionListener(new OpenL());
            gbc_2.gridx = 2;
            gbc_2.gridy = 1;
            gbc_2.insets = new Insets(5,5,5,5);
            panel.add(button_2,gbc_2);
		        
            GridBagConstraints gbc_3 = new GridBagConstraints();
            gbc_3.insets = new Insets(3,3,10,30);
            gbc_3.weightx = 1.0;
            gbc_3.gridy = 4;
            gbc_3.gridx = 1;
            gbc_3.fill = GridBagConstraints.VERTICAL;
            gbc_3.anchor = GridBagConstraints.CENTER;
            panel.add(button_Extract,gbc_3);
            button_Extract.addActionListener(new ActionListener() {
					
                public void actionPerformed(ActionEvent e) {
                   // showDialog("Extract");

                    String fileInput = textField.getText();
                    String fileOutput = textField_2.getText() + System.getProperty("file.separator") + "hci_final.log";
                    try{
                       // DataInputStream in = new DataInputStream(new FileInputStream(fileInput));
                        PrintStream psout = new PrintStream(new FileOutputStream(fileOutput));
                        BufferedReader r = new BufferedReader(new FileReader(fileInput));
                        String buff = null;
                        boolean reading = false;
                        int count = 0;

                        while((buff=r.readLine()) != null){
                                       if(buff.trim().length() == 0)continue;
                            System.out.println (count + "  I: " + buff.indexOf(" I "));
                             System.out.println (count + "  D: " + buff.indexOf(" D "));
                            System.out.println(buff.charAt(31));

                           // System.out.println(count + ": " + buff);
                            count++;

                            if((buff.indexOf("SENT")> -1) ||buff.indexOf("RCVD") > -1 && (reading == false)){
                                reading = true;
                                if(buff.charAt(31)== 'I')psout.println(buff);
                                continue;
                            }
                            if(buff.indexOf("--") > -1 && reading == true){
                                reading = false;
                                  if(buff.charAt(31)== 'I')psout.println(buff);
                                continue;

                            }

                            if(reading){
                                 if(buff.charAt(31)== 'I')psout.println(buff);
                                continue;
                            }



                        }
showDialog("File extraction is success " );
                          r.close();
                        System.exit(0);


                    } catch(Exception ex){
                        ex.printStackTrace();
                    }
                }
             }) ;
            return panel;
        }
     class OpenL implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int rVal = -1;
                if(e.getSource().equals(button)) {
              JFileChooser c = new JFileChooser();
              // Demonstrate "Open" dialog:
              rVal = c.showOpenDialog(TextAcceleratorExample.this);
              if (rVal == JFileChooser.APPROVE_OPTION) {
                  textField.setText(c.getSelectedFile().getAbsolutePath());
              //    textField_2.setText(c.getCurrentDirectory().toString());
                  text_1 = textField.getText();
                  text_2 = textField_2.getText();
                  System.out.print("textField" + textField.getText());
              }
                }

                 if(e.getSource().equals(button_2)){
                        JFileChooser c1 = new JFileChooser();
                     c1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                     rVal = c1.showOpenDialog(TextAcceleratorExample.this);
                     if (rVal == JFileChooser.APPROVE_OPTION) {
                      textField_2.setText(c1.getSelectedFile().getAbsolutePath());
                     }
                 }

              if (rVal == JFileChooser.CANCEL_OPTION) {
                  textField.setText("You pressed cancel");
                  textField_2.setText("");
              }
            }
          }
		

        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           // f.add();
            f.add(new TextAcceleratorExample().getContent());
            f.setSize(400,200);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setTitle("SIGNAL EXTRACTOR");
            f.setVisible(true);
        }
        private void showDialog(String s){
            JOptionPane.showMessageDialog(this, s, "Button says: ", JOptionPane.PLAIN_MESSAGE);
        }

		
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 11 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros