I have two classes, one does something like this:
public ClassOne:
package classes;
public class ClassOne {
public javax.swing.JTextArea progressListing
progressListing = new javax.swing.JTextArea();
public void files(File file){
Class method = new Class();
method.methodInOtherClass(files);
}
public void progressUpdate (String fileOutput){
progressListing.insert(fileOutput,0);
}
}
which then goes to the other class that has the following:
Other Class:
package classes;
public class OtherClass extends ClassOne{
public void methodInOtherClass(file){
String fileOutput
fileOutput = file.getName();
ClassOne input = new ClassOne();
input.progressUpdate(fileOutput);
}
}
It is not updating the progessListing field when the program runs. Is there a better way to do this or am I missing something?
What OtherClass does is it creates pdf files that need to show up in the text area(ie the file path with the file name). ClassOne is the swing interface. Even when it's extended into the other class it doesn't modify the text field when I need it to.
Correct me if I'm wrong, but looking at the code you have posted, I think that you are trying to read a file and put the data from the file into the text area. Again, please correct me if I'm wrong. I think you should use the following code:
BufferedReader br = new BufferedReader(new FileReader("fileName.txt");
String data = br.readLine();
jTextArea1.append("\n"+data);
Please tell me if it works.
Cheers.
PS. If you post your whole code I will be able to help better.
I figured it out. I used a getter method in the other class to assign the variable and returned it to the main class. I set the String inside the loop to have an assign and add function. Works like a charm.
Related
Using JavaFX, I have the user input information into text fields.
My MainController class allows the user to save that inputted text to a txt file and save it at a given location.
Im wondering if its possible to pass what was entered in that text file into another class so I can parse and have my program use its data.
I used strings and .getText() then used a filewriter and bufferedWriter.
Can I get the .getText input into another class?
If your class is public than yes,you can use it in any class in your application;
You should research for Java - Access Modifiers
but as an ideia,
public class MainController {
private String savedFilePath;
public String getSavedFilePath() {
return savedFilePath;
}
public void setSavedFilePath(String savedFilePath) {
this.savedFilePath = savedFilePath;
}
}
and from the other class, when you save the file you can call:
MainController controller = new MainController();
controller.setSavedFilePath("file path");
If you don't want to create a new object MainController, you can define the savedFilePath as static
public class MainController {
private static String savedFilePath;
public static void setSavedFilePath(String paramSavedFilePath) {
savedFilePath = paramSavedFilePath;
}
}
and just call:
MainController.setSavedFilePath("file path");
What I'm trying to do is code a method that takes any kind of text input like
"words.text"
What I imagined it would look like would be
public static wordcount(File afile){....}
I want the method to be called such as
wordcount("words.txt");
I tried looking for the answer but couldn't find it. How do I do this?
Make the method with the following signature
public static void wordCount(String fileName){...}
Then inside the mthod use the string to make a File object.
public static void wordCount(String fileName){
File aFile = new File(fileName);
}
I'm trying to get values from variables in my bean class into iReport (using JavaBean as datasoure). I have the bean class which in its constructor calls a loadReceipt() method - which loads data from a .dat file the user saves. I then have other methods in the bean class which use the data from loadReceipt() to calculate figures and saves them into double variables.
Each of the variables has a getMethod which I call in the factory class and is added to a JavaBean collection which iReport uses.
However my problem is that when I drag the fields into my PDF template and Preview it in Netbeans, I get "File not found" errors for the .dat file that the bean class needs, and thus the fields are always 0.0.
Here's an excerpt from my bean class:
public class Calculations implements Serializable {
//declare data members
private double amt[] = new double [100];
private String cat[] = new String [100];
public double bankIntReceived;
private String category;
private ArrayList <Receipts> rec = new ArrayList<Receipts>();
public Calculations(){
rec = new ArrayList<Receipts>();
loadReceipts();
category = "";
bankIntReceived = calcReceipts("Bank Interest Received");
}
public double getBankIntReceived() {
return bankIntReceived;
}
public void setBankIntReceived(double bankIntReceived) {
this.bankIntReceived = bankIntReceived;
}
Any my Factory class:
public class BeanFactory implements Serializable {
//collection for javabeans
public static Collection getCalcs() {
Vector calculations = new Vector();
try {
Calculations calc = new Calculations();
calc.getBankIntReceived();
calculations.add(calc);
} catch(Exception ex) {
System.out.println(ex);
}
return calculations;
}
}
bankIntReceived will always show 0.0. If I set it manually to a different figure it will show up, so it's a problem loading the .dat file.
Does anyone know why this is happening and how I could get the .dat file to load?
Any help really appreciated! I have been wracking my brain trying to figure this out for the last week. Please excuse any messiness in the code I am a beginner to java. Really would appreciated some help with this!
I figured it out. The file will not load under preview as the program is not running. Once I added the PDF via Jasper to the program and ran it, the figures appeared as they should :)
i am on the creation of an app in android. its a calculator app. the main activity is where the user could input the equation, and the second activity is where the user can add/edit/delete variables. so i made a new class in another file named Global.java. then i extended it to application, imported everything i need, made s private string, made some public functions, edited the manifest, and initialized it right on my main activity. everything works fine while im only using a string to be passed by the functions but when i started adding what i need, an ArrayList, and made some functions so i could access the list then run it, the app closes. i think its because the arraylist is not allowed to be passed to different classes? am i right or am i just missing something?
please dont downvote my post if i didn't post something needed. i am using aide so there is no log output. code:
Global.java
...
import android.app.*;
import java.util.*;
public class Global extends Application
{
private String s;
public static ArrayList<String> sList;
public String getS() {
return s;
}
public void setS(String ss) {
s=ss;
}
public void add() {
sList.add(s);
}
}
MainActivity.java
...
String s;
...
global=(Global)getApplicationContext();
...
global.setS("jian"); //this one works
global.sList.add("jian"); // this one dont
...
Are you sure you initialized sList, like this:
sList = new ArrayList<String>();
If you didn't, you might want to change its declaration to include this initialization.
public static ArrayList<String> sList = new ArrayList<String>();
Just do
global.add("jian");
since you have an add function to take care of the addition of item to arraylist.
Also, try with this:
public void add(String ss) {
sList.add(ss);
}
You are not instantiating your arraylist.
public static ArrayList<String> sList = new Arraylist<String>();
Also you should read beginner tutorials on Java and android, using a public extension of application like this is a bad idea and you can get log outputs from different apps if Aide doesn't provide that, search play store
I'm using netbeans to program something with a user interface...
I hava a main class that named "NewJFrame.java"(A) and one more class
that named "NewClass.java"(B). Class A is extended to class B like this:
public class NewClass extends NewJFrame{
...
}
Contents of ClassA are public static like this:
public static javax.swing.JTextField TextBox1;
I also has a button in classA .So when I click the button, it will call a function
from the classB and that function needs to edit TextBox1's text...
Here is whats going on when I click the button:
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String Str1;
NewClass nc = new NewClass();
Str1=nc.call();
}
Here is the funcion in ClassB:
public String call()
{
String Str;
Str = TextBox1.getText();
TextBox1.setText(Str + "1"); //This part isn't work.
JOptionPane.showConfirmDialog(null,Str,"22222222",JOptionPane.PLAIN_MESSAGE);
return Str;
}
So I can read the text of TextBox1 and show it in a messagebox but cannot edit his text.
If I put this code in main class it works perfectly but in another class it doesn't work.
Can someone help me to reslove this problem?
(I'm using netbeans 6.9.1)
I Just Trying to use some another class to add my code because I dont want all the codes stay in same file this is not usefull... Come on someone needs to know how to do that you can't be writing all the codes in a *.java file right?
The problem you are facing has nothing to do with NetBeans IDE,
you will face the same problem with any IDE for this code.
One way of achieving this is by aggregating the NewJFrame class in the NewClass
instead of extending it:
Let me exlplain with some code:
public class NewClass {
private NewJFrame frame = null;
public NewClass(NewJFrame frame) {
this.frame = frame;
}
public String call()
{
String text;
text = frame.TextBox1.getText();
frame.TextBox1.setText(text + "1"); //This will work now.
JOptionPane.showConfirmDialog(null,text,"22222222",JOptionPane.PLAIN_MESSAGE);
return text;
}
}
Here we will receive a reference to the calling JFrame class and will use fields
defined in that class.
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String Str1;
NewClass nc = new NewClass(this); // see the parameter we are passing here
Str1=nc.call();
}
When we create an object of class NewClass we will pass the reference of the
currently calling NewJFrame object
This will work check it.
Now coming to why your code is not working. When NewClass is extending NewJFrame
and when you create a new object of NewClass class it contains a separate
copy of the NewJFrame which is different from the calling NewJFrame reference hence
the field is getting set in another JFrame and not what you wanted.
with regards
Tushar Joshi, Nagpur
AFAIK Netbeans prevents you from editing by hand GUI's and behaves diferrently depending on strange issues like the one you have... but it was months ago, I dont know if current version sucks that much yet.
I really don't understand why you are forcing yourself to use a new class for this? Even if you NEED to, I don't understand why NewClass extends NewJFrame since you are only creating an instance to call a method that has nothing to do with GUI.
I think creating NewClass isn't necessary. Writing all the code in one class isn't bad by itself. This really depends on MANY factors: how much is "all the code"? Does it make sense to separate responsibilities? Etc, etc...
So make the JTextField and JButton NOT static and NOT public, and simply do everything in there:
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String str = TextBox1.getText();
TextBox1.setText(str + "1"); //This part isn't work.
JOptionPane.showConfirmDialog(null,Str,"22222222",JOptionPane.PLAIN_MESSAGE);
}
P.S.: variable names are start in lowercase: String str, not String Str.
I Found a solution. I'm throwing the contents whereever I'll use. Here is an Example:
Main class:
private void formWindowOpened(WindowEvent evt) {
Tab1Codes tc1 = new Tab1Codes();
if(!tc1.LockAll(TabMenu1))
System.exit(1);
tc1.dispose();
}
Another class where I added some of my codes:
public boolean LockAll(javax.swing.JTabbedPane TabMenu){
try
{
TabMenu.setEnabledAt(1, false);
TabMenu.setEnabledAt(2, false);
TabMenu.setEnabledAt(3, false);
TabMenu.setEnabledAt(4, false);
}catch(Exception e)
{
JOptionPane.showConfirmDialog(null, "I can't Lock the tabs!",
"Locking tabs...",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
So, I can edit the contents in another class but it's little useless to send every content I want to read and edit.
If someone knows any short way please write here.