you do
displayCompanyName.setText(tokenCompanyName);displayAddress.setText(tokenAddress);
in the StringTokenizer class (not sure of the name)
this class extends MainScreen.
you display MainScreen and not StringTokenizer so that's why you see nothing!
you set the text on the StringTokenizer class, not on MainScreen
So you're saying I should set the text in the MainScreen class instead of the TokenizerClass? Well, I've tried that somehow, in the buildLayout method of MainScreen, but that didnt yield any results.
Can you please elaborate on how I should send the token values back to the MainScreen class so I can use displayCompanyName.setText(tokenvalue)?
I've tried numerous references now, and though the parameters are passed on correctly, as println shows the correct string, I can't seem to let the set text appear on the screen.
My latest try is this (parts of the code are omitted so its not compilable):
public class MainScreen extends JFrame implements ActionListener, WindowListener {private TokenizerClass tokenizer;
public JTextArea displayCompanyName,displayAddress;
private String tokenNew = new String();
public static void main(String[] args){
MainScreen fp = new MainScreen();
}
public void textSetter(String tokenCN, String tokenAdd){
System.out.println(tokenCN+"."+tokenAdd); //shows the correct strings
displayCompanyName.setText(tokenCN);
displayAddress.setText(tokenAdd);
System.out.println(displayCompanyName.getText()); //shows the correct values
//WHY DOESN'T IT SHOW ON THE SCREEN?
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == menuFileItem2){
tokenizer = new TokenizerClass();
tokenizer.fDialogStart();
}
}
-
public class TokenizerClass {
public String mainString, tokenID,tokenCompanyName,tokenAddress
FileDialogClass fDialog = new FileDialogClass();
MainScreen fp2 = new MainScreen();
public TokenizerClass() {
}
public void fDialogStart() {
mainString = new String();
fDialog.openFile(mainString);
}
public void tokenization(String tokenedString) {
System.out.println("Toon newString:");
System.out.println(tokenedString +".");
StringTokenizer st = new StringTokenizer(tokenedString, "%%&#");
tokenID = st.nextToken();System.out.println("TokenID: " +tokenID);
tokenCompanyName = st.nextToken();System.out.println("TokenCompanyName: " +tokenCompanyName);
tokenAddress = st.nextToken();System.out.println("tokenAddress: " +tokenAddress);
System.out.println("Final test");
textTransfer(tokenCompanyName,tokenAddress);
}
public void textTransfer(String tok1, String tok2){
System.out.println(tok1+"."+tok2); //shows correct values
fp2.textSetter(tok1, tok2);
System.out.println("Text set");
}
}
--
--
public class FileDialogClass extends JFrame implements WindowListener, ActionListener {
private BufferedReader readBuffer;
private BufferedWriter outFile;
private TokenizerClass tokenizer;
public FileDialogClass(){
}
public void openFile(String projectDataString){
//opens file and reads contents (code omitted)
while ((lines = readBuffer.readLine()) != null) {
projectData.append(lines);
}
projectDataString = projectData.toString();
tokenizer.tokenization(projectDataString);
}
}
Message was edited by:
N00dles
Message was edited by:
N00dles
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.
Here's my simplified code displaying the problem. The process: Create a txt-file with the text:
textfieldA%textfieldBThen compile the 3 classes below: MainScreen.java, TokenizerClass.java and FileDialogClass.java. Run the application and open up the textfile from the JFileChooser. The textfile is read and a string is sent to the TokenizerClass. After its tokened, the tokens are sent to textSetter() in MainScreen, where it's supposed to be displayed in the two white textfields. Except it doesn't.
PS. Dont mind the ugly layout ;)
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
import java.awt.Frame;
import java.awt.Toolkit;
import java.io.*;
import java.lang.String;
import java.util.*;
public class MainScreen extends JFrame implements ActionListener {
public JPanel panelInfo;
public JTextArea displayCompanyName,displayAddress;
private JMenuBar menuBar;
private JMenu menuFile;
private JMenuItem menuFileItem2;
private TokenizerClass tokenizer;
public static void main(String[] args){
MainScreen fp = new MainScreen();
fp.setSize(300, 400);
fp.setVisible(true);
fp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public MainScreen() {
buildLayout();
buildMenu();
}
public void buildLayout() {
BorderLayout borderLayout1 = new BorderLayout();
panelInfo = new JPanel();
panelInfo.setLayout(borderLayout1);
displayCompanyName = new JTextArea();
displayCompanyName.setEditable(false);
displayAddress = new JTextArea();
displayAddress.setEditable(false);
panelInfo.add(displayCompanyName,BorderLayout.PAGE_START);
panelInfo.add(displayAddress,BorderLayout.PAGE_END);
add(panelInfo);
}
public void buildMenu() {
menuBar = new JMenuBar();
menuFile = new JMenu("File");
menuFile.setMnemonic(KeyEvent.VK_F);
menuFileItem2 = new JMenuItem("Open Project");
menuFileItem2.addActionListener(this);
setJMenuBar(menuBar);
menuBar.add(menuFile);
menuFile.add(menuFileItem2);
}
public void textSetter(String tokenCN, String tokenAdd){
displayCompanyName.setText(tokenCN);
displayAddress.setText(tokenAdd);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == menuFileItem2){
tokenizer = new TokenizerClass();
tokenizer.fDialogStart();
}
}
}
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
import java.awt.Frame;
import java.awt.Toolkit;
import java.io.*;
import java.lang.String;
import java.util.*;
public class FileDialogClass extends JFrame {
private JFileChooser fileDialogBox = new JFileChooser();
private BufferedReader readBuffer;
private BufferedWriter outFile;
private TokenizerClass tokenizer;
public FileDialogClass(){
}
public void openFile(String projectDataString){
tokenizer = new TokenizerClass();
StringBuffer projectData = new StringBuffer();
String lines = null;
File fileName = null;
if (fileDialogBox.showDialog(this, "Open") == JFileChooser.APPROVE_OPTION) {
fileName = fileDialogBox.getSelectedFile();
try {
readBuffer = new BufferedReader(new FileReader(fileName));
while ((lines = readBuffer.readLine()) != null) {
projectData.append(lines);
}
readBuffer.close();
}
catch (IOException e) {
System.err.println("Error in opening file.");
}
projectDataString = projectData.toString();
tokenizer.tokenization(projectDataString);
}
}
}
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
import java.awt.Frame;
import java.awt.Toolkit;
import java.io.*;
import java.lang.String;
import java.util.*;
public class TokenizerClass {
public String mainString, tokenCompanyName,tokenAddress;
FileDialogClass fDialog = new FileDialogClass();
MainScreen fp2 = new MainScreen();
public TokenizerClass() {
}
public void fDialogStart() {
mainString = new String();
fDialog.openFile(mainString);
}
public void tokenization(String tokenedString) {
StringTokenizer st = new StringTokenizer(tokenedString, "%");
tokenCompanyName = st.nextToken();
tokenAddress = st.nextToken();
textTransfer(tokenCompanyName,tokenAddress);
}
public void textTransfer(String tok1, String tok2){
fp2.textSetter(tok1, tok2);
}
}
Any help is greatly appreciated.
I partially figured this out. The values of the textFields are passed on correctly to the main class. The problem is the updating of the textFields, which doesn't work.
Right now, I have made some changes: I have made a call to the method
public void redraw() {this.setSize(750, 700);
this.pack();
this.setLocationRelativeTo(this);
this.setVisible(true);
}
to redraw the JFrame. But instead of refreshing the current JFrame, a new one pops up (with filled in textFields!). So it's not entirely perfect.
Anyone who knows how I can solve this?
Message was edited by:
N00dles
hi!
you need to understand what is object reference ....
/*** @author: aniruddha<br>
* @date: May 3, 2007, 2:59:30 PM<br>
* @source: TokenizerClass.java<br>
* @project: HelpForum<br>
*/
package N00dles;
import java.util.StringTokenizer;
public class TokenizerClass
{
public StringmainString, tokenCompanyName, tokenAddress;
FileDialogClassfDialog= new FileDialogClass();
MainScreenfp2= new MainScreen();
public TokenizerClass(MainScreen a_fp2)
{
fp2 = a_fp2;
}
public void fDialogStart()
{
mainString = new String();
fDialog.openFile(mainString, this);
}
public void tokenization(String tokenedString)
{
StringTokenizer st = new StringTokenizer(tokenedString, "%");
tokenCompanyName = st.nextToken();
tokenAddress = st.nextToken();
textTransfer(tokenCompanyName, tokenAddress);
}
public void textTransfer(String tok1, String tok2)
{
fp2.textSetter(tok1, tok2);
}
}
======================================
/**
* @author: aniruddha<br>
* @date: May 3, 2007, 2:59:56 PM<br>
* @source: FileDialogClass.java<br>
* @project: HelpForum<br>
*/
package N00dles;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class FileDialogClass extends JFrame
{
private JFileChooserfileDialogBox= new JFileChooser();
private BufferedReaderreadBuffer;
private BufferedWriteroutFile;
public FileDialogClass()
{
}
public void openFile(String projectDataString, TokenizerClasstokenizer)
{
StringBuffer projectData = new StringBuffer();
String lines = null;
File fileName = null;
if(fileDialogBox.showDialog(this, "Open") == JFileChooser.APPROVE_OPTION)
{
fileName = fileDialogBox.getSelectedFile();
try
{
readBuffer = new BufferedReader(new FileReader(fileName));
while((lines = readBuffer.readLine()) != null)
{
projectData.append(lines);
}
readBuffer.close();
}
catch(IOException e)
{
System.err.println("Error in opening file.");
}
projectDataString = projectData.toString();
tokenizer.tokenization(projectDataString);
}
}
}
======================================
/**
* @author: aniruddha<br>
* @date: May 3, 2007, 3:00:39 PM<br>
* @source: MainScreen.java<br>
* @project: HelpForum<br>
*/
package N00dles;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class MainScreen extends JFrame implements ActionListener
{
public JPanelpanelInfo;
public JTextAreadisplayCompanyName, displayAddress;
private JMenuBarmenuBar;
private JMenumenuFile;
private JMenuItemmenuFileItem2;
private TokenizerClasstokenizer;
public static void main(String[] args)
{
MainScreen fp = new MainScreen();
fp.setSize(300, 400);
fp.setVisible(true);
fp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public MainScreen()
{
buildLayout();
buildMenu();
}
public void buildLayout()
{
BorderLayout borderLayout1 = new BorderLayout();
panelInfo = new JPanel();
panelInfo.setLayout(borderLayout1);
displayCompanyName = new JTextArea("Hello");
displayCompanyName.setEditable(false);
displayAddress = new JTextArea("Hi");
displayAddress.setEditable(false);
panelInfo.add(displayCompanyName, BorderLayout.PAGE_START);
panelInfo.add(displayAddress, BorderLayout.PAGE_END);
add(panelInfo);
}
public void buildMenu()
{
menuBar = new JMenuBar();
menuFile = new JMenu("File");
menuFile.setMnemonic(KeyEvent.VK_F);
menuFileItem2 = new JMenuItem("Open Project");
menuFileItem2.addActionListener(this);
setJMenuBar(menuBar);
menuBar.add(menuFile);
menuFile.add(menuFileItem2);
}
public void textSetter(String tokenCN, String tokenAdd)
{
displayCompanyName.setText(tokenCN);
displayAddress.setText(tokenAdd);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == menuFileItem2)
{
tokenizer = new TokenizerClass(this);
tokenizer.fDialogStart();
}
}
}
just go through the changed code. i hope you will be able to understand.
regards
Aniruddha
Thanks, Aniruddha-Here! This is really useful. Finally it works the way it should. Also thanks for the rest of the clean-up (explicit import statements and such).
I rewarded you the 10 duke stars (for what they're worth :p)
I went through your code, looked at the changes and implemented them in my actual application.
But it still puzzles me, why does your method work, and mine doesn' t?
What's the difference between declaring (and initializing) a TokenizerClass in the FileDialogClass, and passing the TokenizerClass to the OpenFile method as an argument?
And why is this necessary?
public TokenizerClass (MainScreen a_fp2){
fp2 = a_fp2;
}
hi!
in your application there was more then one instance of all three classes. the main thing is you need just one initialisation of each class for one application. So that is why you need to set pass the argument or there are many other way to do this. as i told you earlier you may go through once more about object referencing. That will help you. i am not a good teacher, i am sorry.
regards
Aniruddha