-
need help Printing output from a JTextfield
hello all
i've made a program that takes user input and enters it into a formula. the answer form that formula is output in a JTextfield.
i'm self taught in Java, and i've only been programming for a couple weeks. (i'm a quick learner, just kinda confused with methods, threads, classes, etc..
i've looked up code on the web on how to print, but all i can find so far is how to print the entire container. i just want to print the output in a specific format from the JTextfield.
i don't have any code to put up here, cause i have NO clue what i need.
heres an example of what i want
JTextField output [3.141592654]
i want to just print this output, but formatted like "22 divided by 7 = 3.141592654"
i've tried mixing and matching code from printing the whole container, but i can't figure it out.
please help!
-
Re: need help Printing output from a JTextfield
How to Use Text Fields (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
72.5.124.55/j2se/1.4.2/docs/api/javax/swing/JTextField.html
Check those two links. java.sun.com should be your first port of call for all things java, odds are if you search a java related term in Google the first link will be to one of their pages as it has fantastic documentation.
In your case I would look at the setText() method.
EDIT: Hold on when you say print do you mean print to paper? Or print on screen?
If it's to paper read through this
Lesson: Printing (The Java™ Tutorials > 2D Graphics)
EDIT2:
OK I seen in a different thread you are looking to print to a printer I can't say I've done so myself but check out the JTextField class here
JTextField (Java Platform SE 6)
It has a method inherited from JTextComponent called print() so check that out.
JTextComponent (Java Platform SE 6)
And check out the tutorial here
How to Print Text (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
-
Re: need help Printing output from a JTextfield
Quote:
print this output, but formatted
Look at the DecimalFormat class for a tool to format real numbers.
-
Re: need help Printing output from a JTextfield
Quote:
Originally Posted by
Norm
Look at the DecimalFormat class for a tool to format real numbers.
thank you, but i know how to format my output to the swing GUI.
as far as what i want formatted...
here is the output in the GUI:
JTextfield --> [3.141592654]
printed on paper, with this format:
22 / 7 = '3.141592654'
-
Re: need help Printing output from a JTextfield
Quote:
Originally Posted by
Faz
thank you for these links.
i've already looked through them, and none of them seem to fit what i'm trying to do.
i basically have 8 panes on a Tabbed Paned container. each individual pane contains a diffrent "calculator". example: external spur gear, external helical gear, worm gear, etc... each one of these panes contains a calculator to figure out certain formulas. i engineer and design gears, and also make the blueprints. so on the "design" in java.. i want to use a JButton to print the output. i right click on the button, and select action performed. that takes me to the source code, i just don't know what code i need to print it.
that being said, i said in my earlier post that i'm completely self taught in java. i've programmed before in QBasic, which is nothing like java. i'm VERY confused on methods, classes, threads, etc... so saying something like "It has a method inherited from JTextComponent called print() so check that out." doesn't do me much good lol.
-
Re: need help Printing output from a JTextfield
Ok, I was chasing the wrong bear.
Are you looking for how to print the String: "22 / 7 = '3.141592654'" on paper?
Is that all that is to go on the paper or is there more?
-
Re: need help Printing output from a JTextfield
Quote:
Originally Posted by
Norm
Ok, I was chasing the wrong bear.
Are you looking for how to print the String: "22 / 7 = '3.141592654'" on paper?
Is that all that is to go on the paper or is there more?
no, theres more. that was just an example.
lets say i take input from the user, asking for a dimension in millimeters. i want to take that dimension and turn it into inches. the formula would be A / 25.4
so the code would be something like:
input user variable A
answer variable b
b = a / 25.4
i take b and output that to a JTextField, just as a number.
but i want to be able to print in a format like: A millimeters = B inches
thats all i want to do, i don't want anything else to print, just the JTextfields.
hopefully this explains it better
-
Re: need help Printing output from a JTextfield
Quote:
i don't want anything else to print, just the JTextfields
The contents of textfields are Strings.
So if a textfield holds: "A millimeters = B inches"
Then you want to print the String: "A millimeters = B inches"
use getText() and print the results????
I guess I'm missing something. Sounds like your looking for a method that will take the the contents of a textfield, say 4.3,
do some computation, format the results into a String, say "4 plus 0.3 = 4.5" and then print that. Without your having to code those steps.
-
Re: need help Printing output from a JTextfield
Quote:
Originally Posted by
Norm
The contents of textfields are Strings.
So if a textfield holds: "A millimeters = B inches"
Then you want to print the String: "A millimeters = B inches"
use getText() and print the results????
I guess I'm missing something. Sounds like your looking for a method that will take the the contents of a textfield, say 4.3,
do some computation, format the results into a String, say "4 plus 0.3 = 4.5" and then print that. Without your having to code those steps.
i convert the string from their input to a Double. do you have to convert back to a string in order to print? if so, how is that done? i've coded the whole program, except for the printing (and saving, but i think i'm close to that). all i need to know is how to print a JLabel and a JTextField.
-
Re: need help Printing output from a JTextfield
Quote:
Double. do you have to convert back to a string in order to print
I mentioned the DecimalFormat class and you said: "i know how to format my output"
Double is an object that holds a number. Its not directly printable. It needs to be converted to a String to print. You can let the system do it, or you can use a class to do it. Your choice.
Quote:
i need to know is how to print a JLabel and a JTextField
Not sure what that means. Do you want the output to look like those components look in the GUI? Ie with borders, shadowing etc?
Can you write a short test program that demostrates what and how you expect to print those components?
I just went to the Java Tutorial and found several example programs that print. Have you gotten them?
Their names are: PrintUIWindow.java and SimpleBook.java.
-
Re: need help Printing output from a JTextfield
Quote:
Originally Posted by
Norm
I mentioned the DecimalFormat class and you said: "i know how to format my output"
Double is an object that holds a number. Its not directly printable. It needs to be converted to a String to print. You can let the system do it, or you can use a class to do it. Your choice.
Not sure what that means. Do you want the output to look like those components look in the GUI? Ie with borders, shadowing etc?
Can you write a short test program that demostrates what and how you expect to print those components?
I just went to the Java Tutorial and found several example programs that print. Have you gotten them?
Their names are: PrintUIWindow.java and SimpleBook.java.
i know how to format my output into the JTextfield
decimalformat = new decimalformat df
(format.df(var>).set text blah blah blah
in order for me to use the input as a variable in a formula, i first have to convert the input string into a double. i know how to do that as well.
so lets say the output is the variable a and a = 5. i know how to take that variable and display it in a JTextField. i'm fine with the output.
now imagine the following is what i want printed (using printer) in a txt document:
Diametral Pitch: 5 (5 being the variable a)
Modular Pitch 2.45 (just another example)
in my gui, i have a JLabel saying Diametral Pitch, Modular pitch, etc... and next to those are the JTextFields containing the output.
sorry for being so troublesome!!
-
Re: need help Printing output from a JTextfield
oh, if i can get on the internet at work tomorrow, i'll post some code. thanks again for your help!
-
Re: need help Printing output from a JTextfield
Quote:
following is what i want printed
If that is exactly what you want printer, then why wouldn't the following work:
print("Diametral Pitch: 5 (5 being the variable a)");
and
print("Modular Pitch 2.45 (just another example)");
Is it this that you are trying to do?
There is a variable: aVar that has a value, say 5.
There is a textfield that will hold some string and the value of aVar, to be filled in later.
There is a button (next to the textfield) that when you press it, reads the value in the variable aVar, creates a String with the words: "Diametral Pitch:" and appending on the end of the String the value of aVar and puts that String into the textfield.
-
Re: need help Printing output from a JTextfield
Quote:
Originally Posted by
Norm
If that is exactly what you want printer, then why wouldn't the following work:
print("Diametral Pitch: 5 (5 being the variable a)");
and
print("Modular Pitch 2.45 (just another example)");
this is all i have to do to print?
i'm sure i'll have to import java.awt.print.*
but othersie, just print("blah blah blah)" will print to a piece of paper?
-
Re: need help Printing output from a JTextfield
No, there is more to printing than just print(...).
I posted references to two sample programs, did you get them?
Quote:
I just went to the Java Tutorial and found several example programs that print. Have you gotten them?
Their names are: PrintUIWindow.java and SimpleBook.java.
They show you how to print to paper.
-
Re: need help Printing output from a JTextfield
I think the problem is your thought process of how you want to do this. The easiest way I would think to print out something along the lines of 22/7 = 3.14..... is to put this into String so I'm assuming you don't just want to print this string ou but that it's an actual calculation depending on the users input. So lets say you have three variables a(in this case 22), b(7), c(a/b = 3.14....).
So then create a string called divString or something and say
divString = a + "/" + b + " = " + c;
And if it's not a variable but rather a JTextField you can just use the .getText() method as Norm said. However I believe you should have all the values stored in variables and then build the GUI around the basic programme but I'm not here to tell you how to do your code.
I've taken this bit of code from A Basic Printing Program (The Java™ Tutorials > 2D Graphics > Printing)
Code :
import java.awt.print.*;
import java.awt.*;
public class HelloWorldPrinter implements Printable {
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now we perform our rendering */
g.drawString("Hello world!", 100, 100);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
}
So why not start off by copying that and see if it prints out Hello World!
Then change it so that it has the 3 variables and see if you can put them in a string and print that in lieu of Hello World!
I can't test this myself and I've never printed with Java(although I will need to soon) but that certainly looks quite easy to start with and then just build from there.
-
Re: need help Printing output from a JTextfield
For testing of printing, I added a "Print to File" printer on WinXP so I don't have to actually use any paper while testing.
The output for me was in PostScript format. I had to find, download and install a PostScrirpt viewer which displays the print file in pdf format.
It the OP can find the examples from the Tutorial, they show how to print.
-
Re: need help Printing output from a JTextfield
Code :
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// External Spur Gear
// take user input and convert from char to double
double tooth = (double)((Double.parseDouble(noteeth.getText()))); //input number of teeth in gear
double pa = (double)((Double.parseDouble(pressureangle.getText()))); // input pressure angle
double dp = (double)((Double.parseDouble(diametralpitch.getText()))); //input diametral pitch
//pseudo helix angle
double h = 0;
// set constant pi = 3.141592654
double pi = 3.141592654;
//pitch diamter
double pd = tooth / dp;
//major diamter
double major = (tooth + 2) / dp;
//addendum
double add = 1 / dp;
//dedendum
double ded = 1.1571 / dp;
//base diameter
double bd = pd * Math.cos(pa * (3.141592654 / 180));
//base pitch
double bp = Math.cos(pa * (3.141592654 / 180)) * 3.141592654/dp;
//circular tooth thickness
double ctt = 1.5708 / dp;
//whole depth
double wd = 2.1571 / dp;
//circular pitch
double cp = 3.141592654 / dp;
//wire size
double wire = 1.728 / dp;
//chordal tooth thickness
double chtt = pd * (Math.sin(90/tooth * (3.141592654 / 180)));
//minor diamter
double minor = pd - (2 * ded);
//modular pitch
double mod = 25.4 / dp;
// transverse pressure angle calc.
double Ad1 =((Math.tan(pa * pi / 180)) /
(Math.cos(h * pi / 180))) ;
double Ad2 = Math.atan(Ad1);
double Ad = Ad2 * (180 / pi);
//transverse ctt on PD
double td = ctt / Math.cos(h * (pi /180));
// helix angle on Base Diameter calc
double H1 = ((Math.tan(h * pi / 180)) *
(Math.cos(Ad * pi / 180)));
double H2 = Math.atan(H1);
double H = H2 * (180 / pi);
//transverse pin diameter
double dD = wire / (Math.cos(H * pi / 180 ));
//pitch diameter calc.
double PD = tooth / (dp * Math.cos(h * pi / 180));
//base diameter calc.
double BD = PD * Math.cos(Ad * pi / 180);
//involute function of Ad
double InAd = Math.tan(Ad * pi / 180) - Ad / 180 * pi;
//pressure angle to pin center
double Bd = (td/PD)+(dD/BD)+InAd-(pi/tooth);
double seconds, degrees;
// find the involute function
for (seconds = 70000; seconds <= 90000; seconds++){
degrees = seconds / 3600;
if (Bd >= Math.tan((degrees) * pi / 180) - (degrees) * pi / 180){
//diameter of pin centers
double CC = BD / Math.cos(degrees * pi /180);
// measure over wire dimension - If statement: true = "tooth" is
// an even number.
// else statement: number is odd
double DO;
if(tooth % 2 == 0)
DO = CC + wire;
else DO = Math.cos((90/tooth) * pi / 180) * CC + wire;
//format output to 5 decimal places
DecimalFormat df = new DecimalFormat("#.#####");
//button and button variables
modularpitch.setText(df.format(mod));
circularpitch.setText(df.format(cp));
basepitch.setText(df.format(bp));
pitchdiameter.setText(df.format(pd));
basediameter.setText(df.format(bd));
minordiameter.setText(df.format(minor));
majordiameter.setText(df.format(major));
wholedepth.setText(df.format(wd));
addendum.setText(df.format(add));
dedendum.setText(df.format(ded));
chorthickness.setText(df.format(chtt));
circthickness.setText(df.format(ctt));
measureoverwire.setText(df.format(DO));
wiresize.setText(df.format(wire));
//end of spur gear
}}
this is just one of many forumulas i need to calculate certin dimensions, angles, etc... all of the setText at the end of this code is the output to a paticular Panel in a Tabbed Pane. i want this same output to Print to paper. i have looked at those articles and links you've posted but i'm confused how to integrate it into my program.
-
Re: need help Printing output from a JTextfield
Quote:
i want this same output to Print to paper
Can you make an image of what you want the printed output to look like and attach it here?
I mean EXACTLY what you want it to look like.
have you looked at this code yet:
Quote:
went to the Java Tutorial and found several example programs that print. Have you gotten them?
Their names are: PrintUIWindow.java and SimpleBook.java.
In the PrintUIWindow program there is one call that prints the contents of the JFrame:
Quote:
/* Now print the window and its visible contents */
frameToPrint.printAll(g);
You would replace frameToPrint with the component that you want to print.
-
Re: need help Printing output from a JTextfield
Quote:
Originally Posted by
Norm
Can you make an image of what you want the printed output to look like and attach it here?
I mean EXACTLY what you want it to look like.
Modular Pitch: <whatever number is stored in var mod>
Circular Pitch: <cp>
Base Pitch: <bp>
Pitch Dameter: <pd>
Base Diameter: <bd>
Minor Diameter: <minor>
Major Diameter: <major>
Whole Depth: <wd>
Addendum: <add>
Dedendum: <ded>
Chordal Tooth Thickness: <chtt>
Circular Tooth Thickness: <ctt>
Measure Over Wire: <DO>
Wire Size: <wire>
thats it. just to be clear, i want the number stored in the variable to print, not the <variable>
so if the Major diameter was calculated to be 6.315448, i'd want it to print:
Major Diameter: 6.315448 not: Major Diameter: <major>
and i want it spaced like how it is above as well.
thanks again both of you.
-
Re: need help Printing output from a JTextfield
Download, compile and execute the PrintUIWindow.java program. It prints EXACTLY what is shown on the screen.
In your program create a method that uses the graphics drawString() method to create EXACTLY what you want to see on the paper. Call that method with the graphics object passed to frameToPrint.printAll(g);
-
Re: need help Printing output from a JTextfield
Quote:
Originally Posted by
Norm
Download, compile and execute the PrintUIWindow.java program. It prints EXACTLY what is shown on the screen.
In your program create a method that uses the graphics drawString() method to create EXACTLY what you want to see on the paper. Call that method with the graphics object passed to frameToPrint.printAll(g);
thank you so much!
-
Re: need help Printing output from a JTextfield
Quote:
Originally Posted by
Norm
Download, compile and execute the PrintUIWindow.java program. It prints EXACTLY what is shown on the screen.
i did this, and i got these errors
run:
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImag eSource.java:99)
at sun.awt.image.URLImageSource.getDecoder(URLImageSo urce.java:113)
at sun.awt.image.InputStreamImageSource.doFetch(Input StreamImageSource.java:240)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher. java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:1 36)
-
Re: need help Printing output from a JTextfield
Did you leave off some of the error message. It doesn't show where in the program the error occurred.
My version of PrintUIWindow doesn't have any images to load in it.
Could you explain what you did?
-
Re: need help Printing output from a JTextfield
downloaded the file, compiled, and executed it. nothing printed, and i got those error messages.