Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
If-else in actionPerformed() is not working at all. Someone please any help me. Thank you
 

import java.awt.*;
import java.awt.event.*;
import java.io.*;
 
public class MyFileDialog implements ActionListener
{
    FileDialog fd;
    Button btn;
    TextArea txtAr;
    TextField txtPath;
    GridBagLayout gblo;
    GridBagConstraints gbc;
    Frame frm;
    Label lbl1;
    
    MyFileDialog()
    {
        frm=new Frame("File Dialog");
        frm.setVisible(true);
        frm.setSize(700, 700);
        frm.setLocation(250,250);
        gblo=new GridBagLayout();
        gbc=new GridBagConstraints();
        
        frm.setLayout(gblo);
        
        gbc.fill=GridBagConstraints.BOTH;
        gbc.anchor=GridBagConstraints.CENTER;
        
        //gbc.weightx=0.0;
        gbc.insets.left=0;
        gbc.insets.right=0;
       //frm.add(lbl1=new Label("Enter file name: ",Label.LEFT));       
        //gblo.setConstraints(lbl1, gbc);
        
        gbc.insets.left=2;
        gbc.insets.bottom=5;
        frm.add(txtPath=new TextField(30));
        gblo.setConstraints(txtPath, gbc);
        
        gbc.insets.left=5;
        gbc.insets.top=5;
        gbc.gridwidth=GridBagConstraints.REMAINDER;
        frm.add(btn=new Button("  Load  "));
        btn.setSize(20,10);
        gblo.setConstraints(btn, gbc);
        btn.addActionListener(this);
        
        gbc.insets.left=1;
        gbc.gridwidth=2;
        frm.add(txtAr=new TextArea("",20,70,TextArea.SCROLLBARS_BOTH));
        
        
        frm.addWindowListener(new WindowAdapter()
                                                   {@Override
                                                   public void windowClosing(WindowEvent we)
                                                   {
                                                        frm.setVisible(false);
                                                        System.exit(0);
                                                   }});
    }
 
    @Override
    public void actionPerformed(ActionEvent e)
    {   
        String dir="";
        if(txtPath.getText()==""||txtPath.getText()==null)
        {
            
            fd=new FileDialog(frm,"Load your file",FileDialog.LOAD);
            fd.setVisible(true);
 
            dir=fd.getDirectory()+fd.getFile();
            txtPath.setText(dir);
            
        }
        else
            dir=txtPath.getText();
        
        loadFile(dir);
    }
    
    public void loadFile(String path)
    {
        try
        {
            FileInputStream fis=new FileInputStream(path);
            int space=fis.available();
            byte []buffer=new byte[space];
            
            fis.read(buffer);
                       
            String fileContent=new String(buffer);
            txtAr.setText(fileContent);            
        }
        catch(IOException e)
        {
            txtAr.setText("File cannot be loaded");    
        }       
    }
    
    public static void main(String args[])
    {
           new MyFileDialog();
    }
}
Posted 28-Apr-13 0:50am
Edited 28-Apr-13 1:42am
v2
Comments
Richard MacCutchan at 28-Apr-13 6:43am
   
What does "not working" mean?
However, you may wish to look at http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#equals(java.lang.Object),

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
Try changing your if condition as shown below.
if (txtPath.getText() != null && txtPath.getText().trim().length > 0)
    dir=txtPath.getText();
else {
    fd = new FileDialog(frm, "Load your file", FileDialog.LOAD);
    fd.setVisible(true);
 
    dir=fd.getDirectory() + fd.getFile();
    txtPath.setText(dir);
}
You can also use StringUtils.isEmpty or StringUtils.IsBlank from commons lang library a null safe helper class for performing string related operations.
 
Regards,
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 28 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100