Cannot execute my application even though the compilation was successful - java

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();
}
}

Related

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());
}

I cant move the jlabels to the correct location on my panel/GUI java code

i have made a panel/GUI using BlueJ with multiple JLabels and Entry boxes along with a button, however when i execute it the labels appear next to each other and the Confirm button appears on the same row as the last entry box, ive tried changing the coordinates in the code however it doesnt make a difference, how would i go about having each label next to its own entry box and my confirm button at the bottom of the application ?
My code is below:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Log extends JFrame {
public static void main(String[] args){
Log frameTable1= new Log();
}
JPanel panel = new JPanel();
JLabel name = new JLabel("Name", JLabel.LEFT);
JTextField FullName = new JTextField(15);
JPanel panel1 = new JPanel();
JLabel address = new JLabel("Address", JLabel.LEFT);
JTextField Address1line = new JTextField(15);
JTextField postcode = new JTextField(15);
JTextField Destination = new JTextField(15);
JTextField Date = new JTextField(15);
JTextField MilesTravelling = new JTextField(15);
JButton Confirm = new JButton("Confirm");
Log(){
super("Customer GUI");
setSize(300,400);
setLocation(400,250);
panel.setLayout(new FlowLayout());
FullName.setBounds(70,30,150,20);
Address1line.setBounds(70,80,150,20);
postcode.setBounds(70,130,150,20);
Destination.setBounds(70,180,150,20);
Date.setBounds(70,230,150,20);
MilesTravelling.setBounds(70,280,150,20);
Confirm.setBounds(105,290,80,40);
name.setBounds(40,30,150,20);
address.setBounds(40, 80, 150, 20);
getContentPane().add(name);
getContentPane().add(panel);
panel.add(name);
getContentPane().add(address);
getContentPane().add(panel);
panel.add(address);
panel.add(FullName);
panel.add(Address1line);
panel.add(postcode);
panel.add(Destination);
panel.add(Date);
panel.add(MilesTravelling);
panel.add(Confirm);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
Thankyou

Trying to find error in Java Swing program

So this is my program... It's a way to enter and list marathon runners. Now I'm getting an error when using the "Ny" button (http://gyazo.com/e29517af6befd6242d86e6fe1dc5aae1). Here's the error code: http://gyazo.com/9f80885d41db38cfa5502fe911f6a893.
I think the problems is between the "Form" panel and the listener. There may be unreachable code somewhere? I had this working the other day but I lost the code. Now it doesn't work.
The idea is that the "ny" button shows the user a panel "Form", but instead I get the rror.
I'm a huge noob, so I expect it's some obvious syntax error I just can't seem to spot.
Any feedback is appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Maraton extends JFrame{
JTextArea display;
JButton visa;
ArrayList <Tävlande> list = new ArrayList <Tävlande>();
Maraton(){
super("Kista Maraton");
display = new JTextArea();
display.setEditable(false);
add(display, BorderLayout.CENTER);
add(new JScrollPane(display),BorderLayout.CENTER);
setLocationRelativeTo(null);
setSize(300, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel nedre = new JPanel ();
JPanel höger = new JPanel();
add(nedre, BorderLayout.SOUTH);
add(höger, BorderLayout.EAST);
höger.setLayout(new BoxLayout(höger, BoxLayout.Y_AXIS));
nedre.setBackground( new Color(246,246,246) );
nedre.setBorder(BorderFactory.createLineBorder(new Color(200,200,200)));
JButton ny = new JButton("Ny");
ny.addActionListener(new NyLis());
JButton visa = new JButton("Visa");
visa.addActionListener(new VisaLis());
visa.setEnabled(false);
JButton nyTid = new JButton("Ny Tid");
nedre.add(ny);
nedre.add(visa);
nedre.add(nyTid);
JRadioButton StartNrRb = new JRadioButton("Startnr");
JRadioButton NamnRb = new JRadioButton("Namn");
JRadioButton ÅlderRb = new JRadioButton("Ålder");
JRadioButton TidRb = new JRadioButton("Tid");
höger.add(StartNrRb);
höger.add(NamnRb);
höger.add(ÅlderRb);
höger.add(TidRb);
ButtonGroup bg1 = new ButtonGroup();
bg1.add(NamnRb);
bg1.add(StartNrRb);
bg1.add(ÅlderRb);
bg1.add(TidRb);
}
class Form2 extends JPanel{
JTextField startNrFält;
JTextField tidFält;
Form2(){
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
JPanel rad0 = new JPanel();
rad0.add(new JLabel("Start Nummer: "));
rad0.add(new JLabel("Tid: "));
rad0.setLayout(new BoxLayout(rad0, BoxLayout.Y_AXIS));
rad0.add(startNrFält);
rad0.add(tidFält);
add(rad0);
}
}
class Form extends JPanel{
JTextField namnFält;
JTextField landFält;
JTextField ålderFält;
Form(){
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JPanel rad1 = new JPanel();
rad1.add(new JLabel("Namn: "));
namnFält = new JTextField(15);
rad1.add(namnFält);
add(rad1);
JPanel rad2 = new JPanel();
rad2.add(new JLabel("Land: "));
landFält = new JTextField(15);
rad2.add(landFält);
add(rad2);
JPanel rad3 = new JPanel();
rad3.add(ålderFält);
rad3.add(new JLabel("Ålder: "));
ålderFält = new JTextField(5);
rad3.add(ålderFält);
add(rad3);
}
}
class NyLis implements ActionListener{
public void actionPerformed(ActionEvent ave){
Form f = new Form();
int svar = JOptionPane.showConfirmDialog(null, f);
String namn = f.namnFält.getText();
String land = f.landFält.getText();
int ålder = Integer.parseInt(f.ålderFält.getText());
Tävlande tv = new Tävlande (namn,land,ålder);
list.add(tv);
visa.setEnabled(true);
}
}
class VisaLis implements ActionListener{
public void actionPerformed(ActionEvent ave) {
display.setText("");
for (Tävlande t : list){
display.append(t.toString()+"\n");
}
}
}
class NyTidLis implements ActionListener{
public void actionPerformed(ActionEvent ave) {
Form f2 = new Form();
JOptionPane.showMessageDialog(null, f2);
}
}
public static void main (String []args){
new Maraton();
}
}
The problem is you're trying to add a null object to your JPanel when you click the ny button. The offending code is found in the constructor for your Form object:
rad3.add(ålderFält);
ålderFält = new JTextField(5); //NO! Create the JTextFieldObject first
rad3.add(ålderFält);
Alter the code to the following:
ålderFält = new JTextField(5);
rad3.add(ålderFält);
rad3.add(ålderFält);
And you should have no problems (or at least the code runs for me).
You also have a problem with your visa button. You're declaring an entirely new JButton in your constructor, which will lead to more NullPointerExceptions when you try to enable it.
In the future, read your stack trace a little more closely. Sometimes you have to dig through a few lines of it to find out where, exactly, in your code you're going wrong. This is especially true when you're doing graphical stuff.
You are redefining the JButton visa in the constructor of your class on this line:
JButton visa = new JButton("Visa");
This is distinct from the visa variable defined class level (here this.visa, and visa represent two seperate JButtons), which you attempt to access (uninitialized) in your NyLis actionListener.
Change the aforementioned line to:
visa = new JButton("Visa");

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

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 :)

How do you display info on a jframe that you typed in using text fields?

This is my code...
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Scanner;
public class gui extends JFrame
{
public gui()
{
// creates a frame with thee buttons if i choose the register one it will lead me to the next frame
super ("Property Tax Management System");
JFrame gui = new JFrame("PCMS user interface ");
JButton Register = new JButton("Register",register);
JButton View = new JButton("View",view);
JButton Management = new JButton("Management query's",management)
JButton Exit = new JButton("Exit",exit);
gui.setLayout(new FlowLayout());
Register.setToolTipText("Click if you would like to register");
View.setToolTipText("Click if you would like to view your property file");
Management.setToolTipText("Click if you would like to view statistics")
View.setToolTipText("Click if you would like to exit the programm")
Registeroption one = new Registeroption();
Viewoption two = new Viewoption();
Managementoption three = new Managementoption();
Exitoption four = new Exitoption();
Register.addActionListener(one);
View.addActionListener(two);
Management.addActionListener(two);
Exit.addActionListener(two);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(350,110);
gui.setVisible(true);
gui.add(Register);
gui.add(View);
gui.add(Management);
gui.add(Exit);
}
// I input my information i want to be displayed
private class Registeroption implements ActionListener{
public void actionPerformed(ActionEvent register){
JFrame gui = new JFrame("Register");
JButton Enter = new JButton("View",view);
JTextField Name = new JTextField("Please enter your full name here");
JTextField Address = new JTextField("Please enter your address");
JTextField PPR = new JTextField("Please if you are the principle private residant?yes\no")
JTextField PEV = new JTextField("Please enter your estimated property value");
JTextField Location = new JTextField("Please enter your location ");
JTextField Paid = new JTextField("Please enter if your ta is paid");
Enteroption one = new Enteroption();
Enter.addActionListener(one);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(350,110);
gui.setVisible(true);
gui.add(Enter);
gui.add(Name);
gui.add(Address);
gui.add(PVR);
gui.add(Location);
gui.add(PEV);
gui.add(Paid);
}
}
I want to dispay the info I typed into the other frame and insert a method. This is what I've started with:
private class Enteroption{
public void actionPerformed(ActionEvent enter){
JFrame gui = new JFrame("property details");
JLabel label = new JLabel("");
frame.add(label);

Categories