Results 1 to 4 of 4
  1. #1
    GalaxyNexus is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default How to calculate with a button?

    Hey Programmers,

    I have an issue with my code and i cant seem to get it to work. The purpose of this code is to

    use a JFileChooser and have it pick only directory folders
    after the directory is selected then we are supposed to find the size of the directory folder
    and have it print out onto a textField.

    here is my code: I used Eclipse and Window builder to conduct my code

    Java Code:
    import java.awt.EventQueue;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.text.JTextComponent;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.io.File;
    
    
    
    public class SizeReader {
    
    	protected static final JTextComponent filelist = null;
    	private JFrame frame;
    	private JTextField textField;
    	private JTextField SizeOutput;
    	protected File file;
    	protected Object path;
    	protected long size;
    	protected Object getContenPane;
    	protected Object directory;
    	private int totalFolder;
    	private int totalFile;
    	/**
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					SizeReader window = new SizeReader();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 */
    	public SizeReader() {
    		initialize();
    	}
    
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {
    		frame = new JFrame();
    		frame.setBounds(100, 100, 571, 144);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(null);
    
    		textField = new JTextField();
    		textField.setBounds(10, 11, 402, 35);
    		frame.getContentPane().add(textField);
    		textField.setColumns(10);
    
    		//JFileChooser
    		JButton DirectoryButton = new JButton("Open Directory");
    		DirectoryButton.addActionListener(new ActionListener() {
    			private JFileChooser gcd;
    
    			public void actionPerformed(ActionEvent e) {
    
    				JFileChooser chooser = new JFileChooser();
    				chooser.setFileHidingEnabled(true);
    				chooser.setMultiSelectionEnabled(false);
    				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    				chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    				int state = chooser.showOpenDialog(frame);
    				if (state == JFileChooser.APPROVE_OPTION )
    
    					textField.setText(chooser.getSelectedFile().getAbsolutePath());
    			}
    		});
    		DirectoryButton.setBounds(422, 17, 122, 23);
    		frame.getContentPane().add(DirectoryButton);
    
    		//Button to find the size of a directory
    		JButton SizeReader = new JButton("Get Size");
    		SizeReader.addActionListener(new ActionListener() {
    			private String getFileSize;
    			private JTextComponent foldersize;
    
    			public void actionPerformed(ActionEvent arg0) {
    
    
    				//textField.setText(SizeOutput.getText(folderSize));
    				//SizeOutput.setText(SizeOutput.getText() + getFileSize);
    			}
    		});
    		SizeReader.setBounds(10, 57, 89, 38);
    		frame.getContentPane().add(SizeReader);
    
    		SizeOutput = new JTextField();
    		SizeOutput.setEditable(false);
    		SizeOutput.setBounds(103, 57, 309, 38);
    		frame.getContentPane().add(SizeOutput);
    		SizeOutput.setColumns(10);
    
    		//Exit button
    		JButton btnNewButton_2 = new JButton("Exit");
    		btnNewButton_2.addActionListener(new ActionListener() {
    			private Object exitButton;
    			public void actionPerformed(ActionEvent arg0) {
    
    				System.exit(0);
    
    				this.add(exitButton);
    			}
    			private void add(Object exitButton2) {
    
    			}
    		});
    		btnNewButton_2.setBounds(484, 65, 60, 23);
    		frame.getContentPane().add(btnNewButton_2);
    
    	}
    	//Calculating the size of a directory method
    	public long getFileSize(File folder) { 
    		totalFolder++;  
    		System.out.println("Folder: " + folder.getName()); 
    		long foldersize = 0; 
    
    		File[] filelist = folder.listFiles(); 
    		for (int i = 0; i < filelist.length; i++) { 
    			if (filelist[i].isDirectory()) { 
    				foldersize += getFileSize(filelist[i]); 
    			} else { 
    				totalFile++; 
    				foldersize += filelist[i].length(); 
    			} 
    		} 
    		return foldersize; 
    	} 
    	public int getTotalFolder() { 
    		return totalFolder; 
    	} 
    	public int getTotalFile() { 
    		return totalFile; 
    	} 
    }


    ---------------------------------------------------------
    What I am trying to figure out is how do i get my "JButton SizeReader"
    to call my calculation method (which is the code at the very end)
    i have tried and written numerous codes such as the if/esle statement and the try/catch statment
    but i cant seem to get the size to calculate correctly and i would always get a error or a 0 in the textfield


    Please help me out Thank you Programmers
    Last edited by Fubarable; 06-09-2012 at 04:52 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to calculate with a button?

    Which part is your calculation code attempt? Is your code compiling or are you seeing compilation errors? If so, please show them. If your code is throwing exceptions, you'll want to post them as well, and for both you'll want to indicate with comments in the code where the problems are occurring.

    Also, you should add more System.out.println(...) debug statements so you can see why your code isn't working.

    Edit: I've added [code] [/code] to your post above so that your code will retain its formatting and be more readable.
    Last edited by Fubarable; 06-09-2012 at 04:55 AM.

  3. #3
    GalaxyNexus is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: How to calculate with a button?

    Thanks for the advice and making my code easier to read sorry about that.


    This is the method for my Jbutton and it is supposed to call my calcultion method when it is being pressed
    Java Code:
    //JButton to find the size of a directory
            JButton SizeReader = new JButton("Get Size");
            SizeReader.addActionListener(new ActionListener() {
                private String getFileSize;
                private JTextComponent foldersize;
     
                public void actionPerformed(ActionEvent arg0) {
     
     
                   
                }
            });
    My calculation method
    Java Code:
        }
        //Calculating the size of a directory method
        public long getFileSize(File folder) { 
            totalFolder++;  
            System.out.println("Folder: " + folder.getName()); 
            long foldersize = 0; 
     
            File[] filelist = folder.listFiles(); 
            for (int i = 0; i < filelist.length; i++) { 
                if (filelist[i].isDirectory()) { 
                    foldersize += getFileSize(filelist[i]); 
                } else { 
                    totalFile++; 
                    foldersize += filelist[i].length(); 
                } 
            } 
            return foldersize; 
        } 
        public int getTotalFolder() { 
            return totalFolder; 
        } 
        public int getTotalFile() { 
            return totalFile; 
        } 
    }




    as of right now the code is doing what it is supposed to do but the only thing that it can not do is calculate the directory size when the Jbutton is being pressed and what my issue is that i cant seem to get the right method for my "Jbutton SizeRead" to call my calculation method. i have used certain if/esle and try/catch statements but i dont know if im doing it right becuase that is my weakspot in Java.....

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to calculate with a button?

    The first thing I would do is to get your calculations working outside of any GUI, passing in a hard-coded File object and then testing the results of calculations. I'd also place more println statements in your calculation methods to see what is happening to your variables as the method runs.

Similar Threads

  1. Replies: 7
    Last Post: 08-06-2011, 04:39 AM
  2. Need help to calculate avg
    By vavavoom1 in forum New To Java
    Replies: 2
    Last Post: 04-02-2011, 08:42 PM
  3. Adding a sqrt button and percentage button to a calculator
    By Josie_Taylor in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 02:16 AM
  4. calculate fft
    By ram.west in forum Advanced Java
    Replies: 2
    Last Post: 08-27-2008, 04:05 AM
  5. Calculate what e1 and e2 should be
    By Legoland in forum New To Java
    Replies: 11
    Last Post: 07-02-2007, 07:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •