How do I transfer the data in my JTextField to a JLabel? - java

So we're supposed to be making a reservation screen for our Airline. After the user inputs his/her information and clicks on Reserve my code needs to show her the information back in on a separate frame. I have the frame set up, I just need to transfer the information IN the JTextField to the frame on JLabel. I heard I need to make it into a variable so I can use it in multiple places. I'm new to Java so please don't use words I might not be familiar with. Thank you and here is my code. I have 3 classes.
First Class:
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TicketMan {
public static void main(String[] args) {
JFrame frame = new JFrame ("Airport");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
TicketManBot panel = new TicketManBot();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true); } }
Second Class:(The class the fields are on.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class TicketManBot extends JPanel {
private JLabel name;
private JLabel dest;
private JLabel depc;
private JLabel dept;
private JLabel seat;
private JButton b;
Random rand = new Random();
private JTextField namef, destf, depcf, deptf;
public TicketManBot()
{
name = new JLabel ("Enter passenger's name:");
dest = new JLabel ("Destination: ");
depc = new JLabel ("Departure city: ");
dept = new JLabel ("Departure time: ");
seat = new JLabel ("Seat Number: " + rand.nextInt(100));
b = new JButton ("Make Reservation");
b.addActionListener (new results());
// the fields I need to move
namef = new JTextField (10);
destf = new JTextField (10);
depcf = new JTextField (10);
deptf = new JTextField (10);
add (name);
add (namef);
add (dest);
add (destf);
add (depc);
add (depcf);
add (dept);
add (deptf);
add (seat);
add (b);
setPreferredSize (new Dimension(200, 300));
setBackground (Color.gray);
}
private class results implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
int resultLabel;
String text = namef.getText();
JFrame results = new JFrame("Thank you for flying with JJ Air");
results.setVisible(true);
results.setSize (200, 300);
// I need to move the fields up there in front of these VVVVVV
JLabel nr = new JLabel ("Name of the passenger:");
JLabel dr = new JLabel ("Destination:");
JLabel depr = new JLabel ("Departure city:");
JLabel dpr = new JLabel ("Departure time:");
JLabel sr = new JLabel ("Seat number:");
JLabel t = new JLabel ("Thank you for flying with us");
JLabel label = new JLabel ("");
JPanel panel = new JPanel ();
results.add(panel);
panel.add (nr);
panel.add (dr);
panel.add (depr);
panel.add (dpr);
panel.add (sr);
panel.add (t);
}
}
}
Third Class:
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
public class results extends JPanel {
}

jLabel.setText(jTextField.getText());
Where jLabel is your label and jTextField your text field.
For example:
JLabel nr = new JLabel ("Name of the passenger:"+Namef.getText());
You should read up on the Java coding style guidelines by the way, it will help make your code more readable to others.

Jlabel is the label you want to set text to. jtextField is the tetxfield you want to get info from
jLabel.setText(jTextField.getText());
Also what you could do, is maybe gather all info from all textFields into Array and then use it to display whatever from array in desired jLabel. Or even better use Array to display data in nice looking Table :)

Related

Cannot execute my application even though the compilation was successful

Even though the compilation of the class underneath was successful, I cannot get my code to run. The GUI is just not showing up. In short, I just want a user to enter an arbitrary int into a javax.swing.JTextField, and then show an "italian" representation of it back to the user. Here's the entire code of my application.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Example extends JFrame implements ActionListener {
public ItalianStringNumberConversionFrame() {
Container c = getContentPane();
JButton button1 = new JButton("Convert");
JTextField textField = new JTextField("Enter Integer: ");
JPanel panel = new JPanel();
JFrame frame = new JFrame();
frame.setVisible(true);
panel.setVisible(true);
JLabel label1, label2;
label1 = new JLabel("Enter an Integer to convert to Italian String:" );
label2 = new JLabel("The text version of the number entered in Italian is: ");
panel.add(label1,label2);
panel.add(button1);
panel.add(textField);
c.add(panel);
}
public void actionPerformed(ActionEvent e) {
new Example();
}
}

Adding action listener to JOptionsPane w/ JTextFields

I am attempting to create another GUI screen for an ongoing work project. This GUI screen if triggered will open a JOptionsPane containing three JTextFields and one confirm button (that is part of the actual JOptionsPane).
I am having trouble getting the action listener on the JTextField to work. I want the textfield to pick up whatever value is entered in it when the user clicks confirm. Then through various conditions the program will continue. I've created a test class to plan and create this GUI isolated from the rest of my program so I am able to share it below. Let me also add that I apologize for the way I created this GUI. I am not very good at the visual aspect of GUI's so I used the method that was easiest for me which was losts and lots of JPanels with border layouts. May not be the best but it works :)
Here is my code:
package guitesting;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GUITesting
{
public static int tester = 0;
public static String resultString = null;
static JTextField tRDFI = new JTextField("RDFI Number Here",30);
public static void NOCGUI(){
String text = "<html>"
+ "This is where the entry detail will go"+
"<br><br>"
+"</html>";
JLabel RDFI = new JLabel("RDFI Number:");
JLabel DDA = new JLabel("DDA:");
JLabel TCode = new JLabel("Transaction Code: ");
tRDFI.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
resultString = tRDFI.getText();
}
});
JTextField tDDA = new JTextField("DDA Number Here",30);
JTextField tTCode = new JTextField("TCode Here",30);
JLabel label = new JLabel(text);
JPanel mainPanel = new JPanel();
JPanel fieldPanel = new JPanel();
JPanel textPanel = new JPanel();
JPanel northFieldPanel = new JPanel();
JPanel southFieldPanel = new JPanel();
JPanel tCodePanel = new JPanel();
JPanel RDFIPanel = new JPanel();
JPanel DDAPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
fieldPanel.setLayout(new BorderLayout());
textPanel.setLayout(new BorderLayout());
northFieldPanel.setLayout(new BorderLayout());
southFieldPanel.setLayout(new BorderLayout());
tCodePanel.setLayout(new BorderLayout());
RDFIPanel.setLayout(new BorderLayout());
DDAPanel.setLayout(new BorderLayout());
textPanel.add(label,BorderLayout.CENTER);
RDFIPanel.add(RDFI,BorderLayout.WEST);
RDFIPanel.add(tRDFI,BorderLayout.EAST);
tCodePanel.add(TCode,BorderLayout.WEST);
tCodePanel.add(tTCode,BorderLayout.EAST);
DDAPanel.add(DDA,BorderLayout.WEST);
DDAPanel.add(tDDA,BorderLayout.EAST);
northFieldPanel.add(RDFIPanel,BorderLayout.NORTH);
northFieldPanel.add(DDAPanel,BorderLayout.SOUTH);
southFieldPanel.add(tCodePanel,BorderLayout.NORTH);
fieldPanel.add(northFieldPanel,BorderLayout.NORTH);
fieldPanel.add(southFieldPanel,BorderLayout.SOUTH);
mainPanel.add(textPanel,BorderLayout.NORTH);
mainPanel.add(fieldPanel,BorderLayout.SOUTH);
String options[] = {"Confirm"};
int result = JOptionPane.showOptionDialog(null, mainPanel, "NOC Builder", JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE,null, options, options[0]);
if(result==0 && resultString.equals("A")){
System.out.println("pass");
}
}
public static void main(String args[]){
GUITesting.NOCGUI();
}
}
I don't think an ActionListener is what you want here. It will only be called if the user presses Return when the textfield has focus.
If you want to listen for any and all changes in your textfields, look into DocumentListener.
textField.getDocument().addDocumentListener(documentListener);
But why not just reference the actual textfield when the dialog returns? This might be the simplest solution. Try this:
if (result == 0) {
System.out.println("tRDFI: " + tRDFI.getText());
System.out.println("tDDA: " + tDDA.getText());
System.out.println("tTCode: " + tTCode.getText());
}

JTextField Does not show up initially, only when clicking on it?

So this problem is really giving me a headache because for some reason last night when i was working on it, my code ran perfectly and my textfields would show up without a problem...
Go to bed, wake up, time to work on it again aaaaaand bam. Now my JtextFields only show up when i highlight them or click them or something...I was wondering what could be wrong?
My code is really just messy and crappy at this point while i figure out a better way to design my program...
I thought it was just eclipse but netbeans is giving me the same issue.
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.awt.*;
import javax.swing.border.*;
public class DiffuserCalc {
//create the class data fields
private double Qts;
private double Qes;
private double Vas;
JFrame ProgramBounds = new JFrame();
JLabel label1= new JLabel("Qts");
JLabel label2= new JLabel("Qes");
JLabel label3= new JLabel("Fs");
JLabel label4= new JLabel("BL");
JLabel label5= new JLabel("Xmax");
JLabel label6= new JLabel("Fs");
JLabel label7= new JLabel("Vas");
JLabel label8= new JLabel("Diameter");
JLabel label9= new JLabel("Pmax (RMS)");
JTextField QtsParam = new JTextField("Value");
JTextField QesParam = new JTextField("Value");
JTextField FsParam = new JTextField(" ");
JTextField BLParam = new JTextField(" ");
JTextField XmaxParam = new JTextField(" ");
Font myFont = new Font("Tahoma", Font.BOLD, 20);
DiffuserCalc()
{
ProgramBounds.setTitle("Box Designer");
JPanel ParameterMenu = new JPanel();
JPanel FieldInputs = new JPanel();
ParameterMenu.setBounds(30, 0, 1180, 120);
FieldInputs.setBounds(0,0, 1280, 720);
ProgramBounds.add(ParameterMenu);
ProgramBounds.add(FieldInputs);
ProgramBounds.setSize(1280,720);
// LAYOUT
ParameterMenu.setLayout(new FlowLayout(FlowLayout.CENTER, 60, 10));
FieldInputs.setLayout(null);
Border lineBdr = BorderFactory.createLineBorder(Color.BLACK);
Border BlackBorder = BorderFactory.createTitledBorder(lineBdr, " T/S Parameters ", TitledBorder.CENTER, TitledBorder.TOP, myFont, Color.black);
//FIELD PROPERTIES
label1.setFont(myFont);
label2.setFont(myFont);
label3.setFont(myFont);
label4.setFont(myFont);
label5.setFont(myFont);
label6.setFont(myFont);
label7.setFont(myFont);
label8.setFont(myFont);
label9.setFont(myFont);
// PARAMETER BOUNDS
int XLoc = 150;
int YLoc = 70;
QtsParam.setBounds(XLoc, YLoc, 40, 20);
QesParam.setBounds(XLoc+95, YLoc, 40, 20);
FsParam.setBounds(XLoc+190, YLoc, 40, 20);
// ADD FIELDS
ParameterMenu.add(label1);
ParameterMenu.add(label2);
ParameterMenu.add(label3);
ParameterMenu.add(label4);
ParameterMenu.add(label5);
ParameterMenu.add(label6);
ParameterMenu.add(label7);
ParameterMenu.add(label8);
ParameterMenu.add(label9);
ParameterMenu.setBorder(BlackBorder);
FieldInputs.add(QtsParam);
FieldInputs.add(QesParam);
FieldInputs.add(FsParam);
FieldInputs.add(BLParam);
FieldInputs.add(XmaxParam);
// set everything proper
QtsParam.requestFocus();
ParameterMenu.setVisible(true);
FieldInputs.setVisible(true);
ProgramBounds.setVisible(true);
}
public double BoxDimension(int x, int y)
{
return x;
}
public static void main(String[] args) {
DiffuserCalc MainProgram = new DiffuserCalc();
}
}
Your code only sets the bounds for 3 text fields but you add 5 text fields to the panel.
Don't use a null layout!!!
Use a proper layout manager and then you don't have to worry about making mistakes like this.
Also, follow Java naming conventions. Variable names do NOT start with an upper case character.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class DiffuserCalc {
//create the class data fields
private double qts;
private double qes;
private double vas;
private JFrame programBounds = new JFrame();
private JLabel label1= new JLabel("Qts");
private JLabel label2= new JLabel("Qes");
private JLabel label3= new JLabel("Fs");
private JLabel label4= new JLabel("BL");
private JLabel label5= new JLabel("Xmax");
private JLabel label6= new JLabel("Fs");
private JLabel label7= new JLabel("Vas");
private JLabel label8= new JLabel("Diameter");
private JLabel label9= new JLabel("Pmax (RMS)");
private JTextField qtsParam = new JTextField("Value");
private JTextField qesParam = new JTextField("Value");
private JTextField fsParam = new JTextField("");
private JTextField bLParam = new JTextField("");
private JTextField xmaxParam = new JTextField("");
private Font myFont = new Font("Tahoma", Font.BOLD, 20);
DiffuserCalc()
{
programBounds.setTitle("Box Designer");
JPanel parameterMenu = new JPanel();
JPanel labelPanel = new JPanel();
JPanel fieldInputs = new JPanel();
// LAYOUT
programBounds.setLayout(new BorderLayout());
parameterMenu.setLayout(new BorderLayout());
fieldInputs.setLayout(new FlowLayout(FlowLayout.LEFT));
fieldInputs.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
labelPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
labelPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
programBounds.add(parameterMenu, BorderLayout.NORTH);
parameterMenu.add(labelPanel, BorderLayout.NORTH);
parameterMenu.add(fieldInputs, BorderLayout.SOUTH);
// programBounds.add(fieldInputs);
programBounds.setSize(1280,720);
Border lineBdr = BorderFactory.createLineBorder(Color.BLACK);
Border BlackBorder = BorderFactory.createTitledBorder(lineBdr, " T/S Parameters ", TitledBorder.CENTER, TitledBorder.TOP, myFont, Color.black);
//FIELD PROPERTIES
label1.setFont(myFont);
label2.setFont(myFont);
label3.setFont(myFont);
label4.setFont(myFont);
label5.setFont(myFont);
label6.setFont(myFont);
label7.setFont(myFont);
label8.setFont(myFont);
label9.setFont(myFont);
// ADD FIELDS
labelPanel.add(label1);
labelPanel.add(label2);
labelPanel.add(label3);
labelPanel.add(label4);
labelPanel.add(label5);
labelPanel.add(label6);
labelPanel.add(label7);
labelPanel.add(label8);
labelPanel.add(label9);
parameterMenu.setBorder(BlackBorder);
qtsParam.setColumns(3);
fieldInputs.add(qtsParam);
qesParam.setColumns(3);
fieldInputs.add(qesParam);
fsParam.setColumns(2);
fieldInputs.add(fsParam);
bLParam.setColumns(2);
fieldInputs.add(bLParam);
xmaxParam.setColumns(2);
fieldInputs.add(xmaxParam);
// set everything proper
qtsParam.requestFocus();
programBounds.pack();
programBounds.setVisible(true);
}
public double BoxDimension(int x, int y)
{
return x;
}
public static void main(String[] args) {
DiffuserCalc MainProgram = new DiffuserCalc();
}
}
So I rewrote the class for you to be more according to the Java style standard. Next not using a Layout manager is asking for trouble. And even though your requirements are like you say it's still better to put the effort to use a Layout manager as you'll keep running into problems like these. Read more about layout managers here. Furthermore don't call setVisible on JPanels that you added to a frame. When you call setVisible on the JFrame it will call setVisible on all of it child components.
Next call the method setColumns on the JTextField instead of initializing it with spaces for more consistent and predictable behaviour.

Resize JButton within mu Jframe to put it on the bottom of the page horizantally

I need to write a code that generates library number and resize the JButton to put it to the bottom of the frame.
I need to write a code that generates library number from 1001 and each subsequent number should be one greater. Once the library number has been generated and shown in the text field, the button text should change to ‘Confirm’.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class HW4GUI extends JFrame implements ActionListener
{
private JButton jbtAction;
private JTextField jtfFName;
private JTextField jtfLName;
private JTextField jtfLibNo;
private int nextLibNo;
private JPanel textPanel;
public HW4GUI()
{
setTitle("HW4 GUI");
makeFrame();
showFrame();
}
public void actionPerformed(ActionEvent e)
{
I need to write a code that generates library number from 1001 and each subsequent number should be one greater. Once the library number has been generated and shown in the text field, the button text should change to ‘Confirm’.
}
public void makeFrame() Make frame method
{
setLayout( new GridLayout(4,0) );
jtfFName = new JTextField( 15 ) ;
JLabel aLabel = new JLabel("First Name: ");
jtfFName.setActionCommand("First Name");
jtfFName.setHorizontalAlignment(aLabel.RIGHT);
jtfLName = new JTextField( 15 ) ;
JLabel aLabel1 = new JLabel("Last Name: ");
jtfLName.setActionCommand("Last Name");
jtfLName.setHorizontalAlignment(aLabel1.RIGHT);
jtfLibNo = new JTextField( 10 ) ;
JLabel aLabel2 = new JLabel("Library Number: ");
jtfLibNo.setActionCommand("Library Number");
jtfLibNo.setEditable(false);
jtfLibNo.setHorizontalAlignment(aLabel2.RIGHT);
add(aLabel);
add(jtfFName);
add(aLabel1);
add(jtfLName);
add(aLabel2);
add(jtfLibNo);
JPanel textPanel = new JPanel();
jbtAction = new JButton("Add Borrower");
jbtAction.setLayout(new BoxLayout(jbtAction, BoxLayout.PAGE_AXIS));
add(jbtAction); I need to resize this button
jtfFName.addActionListener( this );
jtfLName.addActionListener( this );
jtfLibNo.addActionListener( this );
jbtAction.addActionListener( this );
}
public void showFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400,200);
setVisible(true);
}
}

JLabel not showing text in GUI

so my code ask the user for input. then converts the temp. should be simple but my code is not working. It does not output my lable 3 it just does nothing. that is the only problem i am having with my code, i just dont know how to fix it
import javax.swing.*;
public class FahrenheitPanel extends JPanel
{
private JLabel lable1;
private JLabel lable2;
private JLabel lable3;
private JTextField fahrenheit;
public FahrenheitPanel()
{
lable1 = new JLabel ("Enter Fahrenheit temperature:");
lable2 = new JLabel ("Temperature in Celsius: ");
fahrenheit = new JTextField (5);
fahrenheit.addActionListener (new TempListener());
add (lable1);
add (fahrenheit);
add (lable2);
setPreferredSize (new Dimension(300, 75));
}
private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
int fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;
lable3.setText(Integer.toString (celsiusTemp));
add ( lable3 );
}
}
public static void main (String[] args)
{
JFrame frame = new JFrame ("Fahrenheit to Celsius Converter");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
FahrenheitPanel panel = new FahrenheitPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
Don't add the labl3 JLabel to your GUI in the actionPerformed(...) method as doing this means you will be trying to add the JLabel as many times as the listener method is called, and will need to call revalidate and repaint unnecessarily. Instead add this JLabel to your GUI from the very beginning in the class's constructor.
The third label should be added to the frame from the start, with some default text.
If you add the label dynamically, then the container must be validated (by calling validate() on the panel).
Also, you should not set the preferred size of a panel. The layout manager computes the preferred size based on the components it contains.
First of all, i converted the Integer value to Double becuase it may have double numbers,
Second your are not adding your label in your class, this is the problem... run your program, if there is any problem , feel free to ask me
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FahrenheitPanel extends JPanel
{
private JLabel lable1;
private JLabel lable2;
private JLabel lable3;
private JTextField fahrenheit;
public FahrenheitPanel()
{
lable1 = new JLabel ("Enter Fahrenheit temperature:");
lable2 = new JLabel ("Temperature in Celsius: ");
lable3 = new JLabel("");
fahrenheit = new JTextField (5);
fahrenheit.addActionListener ((ActionListener) new TempListener());
add (lable1);
add (fahrenheit);
add (lable2);
add(lable3);
setPreferredSize (new Dimension(250, 75));
}
private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
double fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Double.parseDouble (text);
celsiusTemp = ((fahrenheitTemp-32) * 5/9);
lable3.setText(Double.toString (celsiusTemp));
}
}
public static void main (String[] args)
{
JFrame frame = new JFrame ("Fahrenheit to Celsius Converter");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
FahrenheitPanel panel = new FahrenheitPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}

Categories