Link to home
Start Free TrialLog in
Avatar of Sreejith22
Sreejith22Flag for India

asked on

Java expert help needed - Code attached

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);
        }

		
}

Open in new window

Avatar of for_yan
for_yan
Flag of United States of America image


There is no problem with typing file path in the textfiled - what kind of doubts you have on this subject - just type, and
then use getText() method and use the string whicvh you get as a file name

As to drag the file - do you mean you want to drag the file from outside your java application - say from Windows
folder window? That seems problematic to me.
well drag and drop for swing elements is posible, but not really "easy"  there is a complete tutorial here, check it out,

Drag and Drop is possible fomr one java component to another - but from system window to java application - well, everything is possible
but it is problematic. You can probably do something through clipboard, but direct drag into Java window from say your desktop
seesm difficult
Avatar of Sreejith22

ASKER

>>As to drag the file - do you mean you want to drag the file from outside your java application - say from Windows folder window?


Yes, I want to drag file and drop it inside textfield, so that, the path comes there automatically.
But you want to take the file say, from your desktop, or from say JFileChooser window?

Waht is your OS ?
>>But you want to take the file say, from your desktop, or from say JFileChooser window?


Destop or from any other Windows folder
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
pls, any code snippet which would prove to be an apt solution is much appreciated.
see code above