Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 24th, 2006, 8:50 PM   #11
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by Kilo
woot thanks a ton bro! the button works great... but ofcourse one problem leads to another... next step! before i use file i/o i need to be able to take control of the text in the JTextField so that i could pass it a function.
I believe JTextField's getText() function is what you're looking for.

Quote:
Oh another thing! when it comes time to use the fileName String (the variable passed to my function) do i replace file.in with the string name?
Whatever works. file.in is the name of the file. If the text file is in the same folder as the compiled program you don't need the path to the file.
titaniumdecoy is offline   Reply With Quote
Old Feb 24th, 2006, 9:16 PM   #12
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
GRRR java annoys me.. it is nothing like c++!

[php]
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.io.*;

public class lettercount extends JFrame
{
public lettercount()
{
this("");
}

public lettercount(String title)
{
super(title);

scanButton fileButton=new scanButton();
fileButton.setBounds(220,20,72,20); // x, y, width, height
fileButton.setVisible(true);

JTextField filePath=new JTextField();
filePath.setBounds(20,20,200,20);
filePath.setVisible(true);

getContentPane().setLayout(null);
getContentPane().add(fileButton);
getContentPane().add(filePath);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(450,250);
setVisible(true);
}

public static void main(String args[])
{
new lettercount("Letter Counter");

char[] letterArray=new char[26];
int[] letterCount=new int[26];
int count=0;

for(int i=97;i<=122;i++)
{
letterArray[count]=(char)i;
System.out.printf("%d = %s\n",count,letterArray[count]);
count++;
}
}

private static void scanFile(String tempFile)
{
//RandomAccessFile f = new RandomAccessFile ("file.in", "r");
//PrintWriter out = new PrintWriter(
// new BufferedWriter(
// new FileWriter("file.out")));
//String firstLine = f.readLine();
//out.print(firstLine);
//out.close();
}
}

class scanButton extends JButton implements ActionListener
{
scanButton()
{
super("Scan File");
addActionListener(this);
}

public void actionPerformed(ActionEvent arg0)
{
scanFile(filePath.getText());
}
}
[/php]

i need my scanFile function to be called from my button class! I understand access modifers and such but don't understand how to make this possible in Java.
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Feb 24th, 2006, 9:38 PM   #13
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
You need to put the ScanButton class inside the lettercount class (BTW, all public classes should start with a capital letter).

Alternatively you could put the ScanButton class in another file (ScanButton.java) and make the lettercount class recognize it making package package_name; the first line in both files.
titaniumdecoy is offline   Reply With Quote
Old Feb 24th, 2006, 10:54 PM   #14
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
still getting the same access errors "Cannot find symbol"

[php]
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.io.*;

public class lettercount extends JFrame
{
public lettercount()
{
this("");
}

public lettercount(String title)
{
super(title);

ScanButton fileButton=new ScanButton();
fileButton.setBounds(220,20,72,20); // x, y, width, height
fileButton.setVisible(true);

JTextField filePath=new JTextField();
filePath.setBounds(20,20,200,20);
filePath.setVisible(true);

getContentPane().setLayout(null);
getContentPane().add(fileButton);
getContentPane().add(filePath);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(450,250);
setVisible(true);
}

public static void main(String args[])
{
new lettercount("Letter Counter");

char[] letterArray=new char[26];
int[] letterCount=new int[26];
int count=0;

for(int i=97;i<=122;i++)
{
letterArray[count]=(char)i;
System.out.printf("%d = %s\n",count,letterArray[count]);
count++;
}
}

private static void scanFile(String tempFile)
{
//RandomAccessFile f = new RandomAccessFile ("file.in", "r");
//PrintWriter out = new PrintWriter(
// new BufferedWriter(
// new FileWriter("file.out")));
//String firstLine = f.readLine();
//out.print(firstLine);
//out.close();
}

class ScanButton extends JButton implements ActionListener
{
ScanButton()
{
super("Scan File");
addActionListener(this);
}

public void actionPerformed(ActionEvent arg0)
{
scanFile(filePath.getText());
}
}
}
[/php]
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Feb 25th, 2006, 4:20 AM   #15
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
Quote:
still getting the same access errors "Cannot find symbol"
You're getting an error because the ScanButton class is calling a local method that does not exist. Thus, the "Cannot find symbol" error.

Your biggest obstacle at this point seems to be OOP in Java. Since you're just learning Java, I would highly recommend that you create each class in a separate file. I think this will help you conceptually. In order to invoke a non-static method in a different class, you must first obtain an object of that class (by creating a new object or passing an instance to the constructor). Once you have an object, you simply use the dot operator to call the method:
object.method();
jaeusm is offline   Reply With Quote
Old Feb 25th, 2006, 5:39 AM   #16
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
It might help if you had a quick rundown of blocks and scopes in Java, Kilo. A block is any piece of code surrounded by brackets {}. A variable can be accessed only within the block in which it was defined. This is known as the variable's scope.

Some examples might help make it clear:
// the string 'foo' is in the same block as the println method,
// so everything works okay

{
    String foo = "Hello";
    System.out.println(foo);
}

// The same applies here. Inner blocks can access the variables decared
// in outer blocks:

{
    String foo = "Hello";
    {
        System.out.println(foo);
    }
}
However, these bits of code will cause errors, because the program tries to access a variable that goes out of scope:
{
    {
        String foo = "Hello";
    }
    // foo is now out of scope

    System.out.println(foo);    // will cause an error
}
Using this knowledge, take another look at how classes are defined:
class Foo
{
    // class scope
    // variables defined here can be accessed by any method in the class

    void bar()
    {
        // method scope
        // variables defined here can only be accessed inside the method

        if (...)
        {
            // inner scope
            // variables defined here can only be accessed within the
            // if statement
        }
    }
}
Thus, if you want filePath to be accessable by two different methods, you need to define it as in the class scope:
public class lettercount extends JFrame
{
    JTextField filePath;

    ...
}
Also, you want to put all of the "getContentPane().add"s and "fileButton.setBounds" in the constructor of the class, because currently you're creating your GUI within the method that handles what do to when the button is clicked. And the user obviously can't click the button before the GUI has been created.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC