generating the correct xml from jaxb - java

I am generating a xml by taking input from the user in the swing form , now I want that xml parameters value should be set as what user enters for example user enter the following values for the following parameters...
route cna="trk"
typ="e_pwap"
ver="96"
sk_cna="ltp6"
sk_typ="55"
sk_ver="26"
prefilterfilename="sjj"
prefiltertype="true"
postfilterfilename=""
postfiltertype=""
xfm=""
gelcatnumbersource="16"
gelcatnumbersink="40"
so the final xml should be generated would be...
<route cna="trk" typ="e_pwap" ver="96" sk_cna="ltp6" sk_typ="55" sk_ver="26" prefilterfilename="sjj" prefiltertype="true" postfilterfilename="" postfiltertype="" xfm="" gelcatnumbersource="16" gelcatnumbersink="40" />
right now it is not happening even if the user enters the value, right now it is printing as ...
<route cna="cnavalue" typ="typ" ver="ver" sk_cna="sk_cna" sk_typ="sk_typ" sk_ver="sk_ver" prefilterfilename="prefilterfilename" prefiltertype="prefiltertype" postfilterfilename="postfilterfilename" postfiltertype="postfiltertype" gelcatnumbersource="gelcatnumbersource" gelcatnumbersink="gelcatnumbersink"/>
please advise how to correct this. Below is my pojo..
package abcd;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessType;
#XmlRootElement(name = "route")
#XmlAccessorType(XmlAccessType.FIELD)
public class Route {
#XmlAttribute(name = "cna")
protected String cna;
public String getCna(){ return cna; }
public void setCna( String value ){ cna = value; }
#XmlAttribute(name = "typ")
protected String typ;
public String getTyp() {
return typ;
}
public void setTyp(String typ) {
this.typ = typ;
}
#XmlAttribute(name = "ver")
protected String ver;
public String getVer() {
return ver;
}
public void setVer(String ver) {
this.ver = ver;
}
#XmlAttribute(name = "sk_cna")
protected String sk_cna;
public String getSk_cna() {
return sk_cna;
}
public void setSk_cna(String sk_cna) {
this.sk_cna = sk_cna;
}
#XmlAttribute(name = "sk_typ")
protected String sk_typ;
public String getSk_typ() {
return sk_typ;
}
public void setSk_typ(String sk_typ) {
this.sk_typ = sk_typ;
}
#XmlAttribute(name = "sk_ver")
protected String sk_ver;
public String getSk_ver() {
return sk_ver;
}
public void setSk_ver(String sk_ver) {
this.sk_ver = sk_ver;
}
#XmlAttribute(name = "prefilterfilename")
protected String prefilterfilename;
public String getPrefilterfilename() {
return prefilterfilename;
}
public void setPrefilterfilename(String prefilterfilename) {
this.prefilterfilename = prefilterfilename;
}
#XmlAttribute(name = "prefiltertype")
protected String prefiltertype;
public String getPrefiltertype() {
return prefiltertype;
}
public void setPrefiltertype(String prefiltertype) {
this.prefiltertype = prefiltertype;
}
#XmlAttribute(name = "postfilterfilename")
protected String postfilterfilename;
public String getPostfilterfilename() {
return postfilterfilename;
}
public void setPostfilterfilename(String postfilterfilename) {
this.postfilterfilename = postfilterfilename;
}
#XmlAttribute(name = "postfiltertype")
protected String postfiltertype;
public String getPostfiltertype() {
return postfiltertype;
}
public void setPostfiltertype(String postfiltertype) {
this.postfiltertype = postfiltertype;
}
#XmlAttribute(name = "gelcatnumbersource")
protected String gelcatnumbersource;
public String getGelcatnumbersource() {
return gelcatnumbersource;
}
public void setGelcatnumbersource(String gelcatnumbersource) {
this.gelcatnumbersource = gelcatnumbersource;
}
#XmlAttribute(name = "gelcatnumbersink")
protected String gelcatnumbersink;
public String getGelcatnumbersink() {
return gelcatnumbersink;
}
public void setGelcatnumbersink(String gelcatnumbersink) {
this.gelcatnumbersink = gelcatnumbersink;
}
}
and below is my form class..
package abcd;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.StringWriter;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class sdr {
private JFrame frmArgonNetworkModel;
private final JButton btnGenerateagonnetworkmodel = new JButton("GenerateAgonNetworkModel");
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_8;
private JTextField textField_9;
private JTextField textField_10;
private JTextField textField_11;
private JLabel lblVer;
private JTextField textField_12;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
sdr window = new sdr();
window.frmArgonNetworkModel.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public sdr() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmArgonNetworkModel = new JFrame();
frmArgonNetworkModel.getContentPane().setForeground(new Color(188, 143, 143));
frmArgonNetworkModel.setForeground(new Color(245, 222, 179));
frmArgonNetworkModel.setTitle("Argon Network Model");
frmArgonNetworkModel.getContentPane().setBackground(UIManager.getColor("ToolBar.light"));
frmArgonNetworkModel.getContentPane().setLayout(null);
btnGenerateagonnetworkmodel.setBackground(new Color(224, 255, 255));
btnGenerateagonnetworkmodel.setBounds(226, 358, 196, 29);
frmArgonNetworkModel.getContentPane().add(btnGenerateagonnetworkmodel);
JLabel lblNewLabel = new JLabel("Route CNA");
lblNewLabel.setBounds(22, 11, 71, 14);
frmArgonNetworkModel.getContentPane().add(lblNewLabel);
JLabel lblTyp = new JLabel("typ");
lblTyp.setBounds(22, 36, 71, 14);
frmArgonNetworkModel.getContentPane().add(lblTyp);
JLabel lblSkcna = new JLabel("sk_cna");
lblSkcna.setBounds(22, 83, 71, 14);
frmArgonNetworkModel.getContentPane().add(lblSkcna);
JLabel lblSktyp = new JLabel("sk_typ");
lblSktyp.setBounds(22, 108, 71, 14);
frmArgonNetworkModel.getContentPane().add(lblSktyp);
JLabel lblSkver = new JLabel("sk_ver");
lblSkver.setBounds(22, 133, 71, 14);
frmArgonNetworkModel.getContentPane().add(lblSkver);
JLabel lblPrefilterfilename = new JLabel("prefilterfilename");
lblPrefilterfilename.setBounds(22, 158, 105, 14);
frmArgonNetworkModel.getContentPane().add(lblPrefilterfilename);
textField = new JTextField();
textField.setBounds(122, 8, 377, 20);
frmArgonNetworkModel.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(122, 33, 377, 20);
frmArgonNetworkModel.getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(122, 80, 377, 20);
frmArgonNetworkModel.getContentPane().add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(122, 105, 377, 20);
frmArgonNetworkModel.getContentPane().add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(122, 130, 377, 20);
frmArgonNetworkModel.getContentPane().add(textField_4);
textField_4.setColumns(10);
textField_5 = new JTextField();
textField_5.setBounds(122, 155, 377, 20);
frmArgonNetworkModel.getContentPane().add(textField_5);
textField_5.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("prefiltertype");
lblNewLabel_1.setBounds(22, 183, 88, 14);
frmArgonNetworkModel.getContentPane().add(lblNewLabel_1);
textField_6 = new JTextField();
textField_6.setBounds(122, 180, 377, 20);
frmArgonNetworkModel.getContentPane().add(textField_6);
textField_6.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("postfilterfilename");
lblNewLabel_2.setBounds(22, 208, 88, 14);
frmArgonNetworkModel.getContentPane().add(lblNewLabel_2);
textField_7 = new JTextField();
textField_7.setBounds(122, 205, 377, 20);
frmArgonNetworkModel.getContentPane().add(textField_7);
textField_7.setColumns(10);
JLabel lblNewLabel_3 = new JLabel("postfiltertype");
lblNewLabel_3.setBounds(22, 233, 86, 14);
frmArgonNetworkModel.getContentPane().add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("xfm");
lblNewLabel_4.setBounds(23, 258, 46, 14);
frmArgonNetworkModel.getContentPane().add(lblNewLabel_4);
textField_8 = new JTextField();
textField_8.setBounds(122, 230, 377, 20);
frmArgonNetworkModel.getContentPane().add(textField_8);
textField_8.setColumns(10);
textField_9 = new JTextField();
textField_9.setBounds(122, 255, 377, 20);
frmArgonNetworkModel.getContentPane().add(textField_9);
textField_9.setColumns(10);
JLabel lblNewLabel_5 = new JLabel("gelcatnumbersource");
lblNewLabel_5.setBounds(22, 283, 116, 14);
frmArgonNetworkModel.getContentPane().add(lblNewLabel_5);
JLabel lblNewLabel_6 = new JLabel("gelcatnumbersink");
lblNewLabel_6.setBounds(22, 308, 88, 14);
frmArgonNetworkModel.getContentPane().add(lblNewLabel_6);
textField_10 = new JTextField();
textField_10.setBounds(122, 280, 377, 20);
frmArgonNetworkModel.getContentPane().add(textField_10);
textField_10.setColumns(10);
textField_11 = new JTextField();
textField_11.setBounds(122, 305, 379, 20);
frmArgonNetworkModel.getContentPane().add(textField_11);
textField_11.setColumns(10);
JTextPane textPane = new JTextPane();
textPane.setBounds(22, 398, 652, 157);
frmArgonNetworkModel.getContentPane().add(textPane);
frmArgonNetworkModel.setBounds(100, 100, 711, 613);
frmArgonNetworkModel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btnGenerateagonnetworkmodel.setActionCommand("generate");
lblVer = new JLabel("Ver");
lblVer.setBounds(32, 61, 46, 14);
frmArgonNetworkModel.getContentPane().add(lblVer);
textField_12 = new JTextField();
textField_12.setBounds(122, 58, 377, 20);
frmArgonNetworkModel.getContentPane().add(textField_12);
textField_12.setColumns(10);
btnGenerateagonnetworkmodel.addActionListener(new ButtonClickListener());
}
public class ButtonClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if ("generate".equals(e.getActionCommand())) {
// create the Route object // marshal to string
// ...
Route route = new Route();
route.setCna( "cnavalue" );
route.setTyp("typ");
route.setVer("ver");
route.setSk_cna("sk_cna");
route.setSk_typ("sk_typ");
route.setSk_ver("sk_ver");
route.setPrefilterfilename("prefilterfilename");
route.setPrefiltertype("prefiltertype");
route.setPostfilterfilename("postfilterfilename");
route.setPostfiltertype("postfiltertype");
route.setGelcatnumbersource("gelcatnumbersource");
route.setGelcatnumbersink("gelcatnumbersink");
try
{
JAXBContext jc = JAXBContext.newInstance( Route.class );
Marshaller m = jc.createMarshaller();
m.setProperty( "jaxb.fragment", true );
StringWriter sw = new StringWriter();
m.marshal( route, sw );
System.out.println( sw.toString() );
}catch(Exception ee)
{ee.printStackTrace();
}
}
}
}
}

Well, setting the field's value to "cnavalue" will indeed result in the XML to contain an attribute cnavalue="cnavalue".
Route route = new Route();
route.setCna( "cnavalue" );
Use something like
route.setCna( textField_1.getText() );

Related

how to print text from a JtextField by clicking a JButton

I'm creating a little program for my self to learn Java now I did make a GUI with 12 JTextFields and now I want to use the text that's in the text fields in another class where I print them out.
When I press the button class print is successful, summand I get the last System.out.println("Test if class is triggerd") in my console but I don't get the JTextFields output in the console
Class Frame1
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Frame1 {
public JFrame frmCratingRecipieGenerator;
public JTextField txtSlot;
public JTextField textField_1;
public JTextField textField_2;
public JTextField textField_3;
public JTextField textField_4;
public JTextField textField_5;
public JTextField textField_6;
public JTextField textField_7;
public JTextField textField_8;
public JTextField textField_9;
public JTextField textField_10;
public JTextField textField_11;
public JLabel lblNewLabel;
public String slot1;
public String slot2;
public String slot3;
public String slot4;
public String slot5;
public String slot6;
public String slot7;
public String slot8;
public String slot9;
public String output;
public String output_count;
public String file_num;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frmCratingRecipieGenerator.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Frame1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
frmCratingRecipieGenerator = new JFrame();
frmCratingRecipieGenerator.setResizable(false);
frmCratingRecipieGenerator.setTitle("Crating Recipie Generator Alpha 1");
frmCratingRecipieGenerator.getContentPane().setBackground(new Color(192, 192, 192));
frmCratingRecipieGenerator.setBounds(100, 100, 452, 248);
frmCratingRecipieGenerator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmCratingRecipieGenerator.getContentPane().setLayout(null);
txtSlot = new JTextField();
txtSlot.setBounds(10, 19, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(txtSlot);
txtSlot.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(106, 19, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(202, 19, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(10, 50, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(106, 50, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_4);
textField_4.setColumns(10);
textField_5 = new JTextField();
textField_5.setBounds(202, 50, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_5);
textField_5.setColumns(10);
textField_6 = new JTextField();
textField_6.setBounds(10, 81, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_6);
textField_6.setColumns(10);
textField_7 = new JTextField();
textField_7.setBounds(106, 81, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_7);
textField_7.setColumns(10);
textField_8 = new JTextField();
textField_8.setBounds(202, 81, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_8);
textField_8.setColumns(10);
textField_9 = new JTextField();
textField_9.setBounds(351, 19, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_9);
textField_9.setColumns(10);
textField_10 = new JTextField();
textField_10.setBounds(351, 81, 46, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_10);
textField_10.setColumns(10);
textField_11 = new JTextField();
textField_11.setText("1");
textField_11.setBounds(351, 50, 46, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_11);
textField_11.setColumns(10);
JLabel lblFileId = new JLabel("File ID");
lblFileId.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblFileId.setHorizontalAlignment(SwingConstants.LEFT);
lblFileId.setBounds(298, 82, 86, 14);
frmCratingRecipieGenerator.getContentPane().add(lblFileId);
JLabel lblOutput = new JLabel("Output");
lblOutput.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblOutput.setBounds(298, 20, 58, 14);
frmCratingRecipieGenerator.getContentPane().add(lblOutput);
JLabel lblCount = new JLabel("Count");
lblCount.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblCount.setBounds(298, 51, 52, 14);
frmCratingRecipieGenerator.getContentPane().add(lblCount);
JLabel lblThisJavaProgram = new JLabel("This Java program is coded by Jason");
lblThisJavaProgram.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblThisJavaProgram.setBounds(10, 179, 270, 20);
frmCratingRecipieGenerator.getContentPane().add(lblThisJavaProgram);
JLabel TestLabel = new JLabel("");
TestLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
TestLabel.setBounds(391, 179, 46, 19);
frmCratingRecipieGenerator.getContentPane().add(TestLabel);
JButton btnNewButton = new JButton("Generate the McFunction File's");
btnNewButton.setBounds(10, 108, 427, 60);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TestLabel.setText(textField_10.getText());
print name = new print();
name.Print();
}
});
frmCratingRecipieGenerator.getContentPane().add(btnNewButton);
lblNewLabel = new JLabel("Last File num");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel.setBounds(287, 182, 110, 14);
frmCratingRecipieGenerator.getContentPane().add(lblNewLabel);
this.slot1 = txtSlot.getText();
this.slot2 = textField_1.getText();
this.slot3 = textField_2.getText();;
this.slot4 = textField_3.getText();;
this.slot5 = textField_4.getText();;
this.slot6 = textField_5.getText();;
this.slot7 = textField_6.getText();;
this.slot8 = textField_7.getText();;
this.slot9 = textField_8.getText();;
this.output = textField_9.getText();
this.output_count = textField_11.getText();
this.file_num = textField_10.getText();
}
}
Class print
package example;
public class print {
public void Print() {
Frame1 slots = new Frame1();
String slot1 = slots.slot1;
String slot2 = slots.slot2;
String slot3 = slots.slot3;
String slot4 = slots.slot4;
String slot5 = slots.slot5;
String slot6 = slots.slot6;
String slot7 = slots.slot7;
String slot8 = slots.slot8;
String slot9 = slots.slot9;
String output = slots.output;
String output_count = slots.output_count;
String file_num = slots.file_num;
System.out.println(slot1);
System.out.println(slot2);
System.out.println(slot3);
System.out.println(slot4);
System.out.println(slot5);
System.out.println(slot6);
System.out.println(slot7);
System.out.println(slot8);
System.out.println(slot9);
System.out.println(output);
System.out.println(output_count);
System.out.println(file_num);
System.out.println("Test if class is triggerd");
}
}
This is happening because you create on the print class a new Object of frame1 so the variables by default are null. You can add on the Print method a list parameter that stores all the Strings in that list.
public void Print(List<String> slots) {
System.out.println(slots.get(0));
System.out.println(slots.get(1));
System.out.println(slots.get(2));
System.out.println(slots.get(3));
System.out.println(slots.get(4));
System.out.println(slots.get(5));
System.out.println(slots.get(6));
System.out.println(slots.get(7));
System.out.println(slots.get(8));
System.out.println(slots.get(9));
System.out.println(slots.get(10));
System.out.println(slots.get(11));
}
and redefine before the method call all the variables and store them in the list.
You can learn some things about Java's lists here
If you are struggling finding the code I made it for you
slot1 = txtSlot.getText();
slot2 = textField_1.getText();
slot3 = textField_2.getText();;
slot4 = textField_3.getText();;
slot5 = textField_4.getText();;
slot6 = textField_5.getText();;
slot7 = textField_6.getText();;
slot8 = textField_7.getText();;
slot9 = textField_8.getText();;
output = textField_9.getText();
List<String> slots = new ArrayList<String>();
slots.add(slot1);
slots.add(slot2);
slots.add(slot3);
slots.add(slot4);
slots.add(slot5);
slots.add(slot6);
slots.add(slot7);
slots.add(slot8);
slots.add(slot9);
slots.add(output);
slots.add(output_count);
slots.add(file_num);
output_count = textField_11.getText();
file_num = textField_10.getText();
TestLabel.setText(textField_10.getText());
print name = new print();
name.Print(slots);
and Print class:
public void Print(List<String> slots) {
System.out.println(slots.get(0));
System.out.println(slots.get(1));
System.out.println(slots.get(2));
System.out.println(slots.get(3));
System.out.println(slots.get(4));
System.out.println(slots.get(5));
System.out.println(slots.get(6));
System.out.println(slots.get(7));
System.out.println(slots.get(8));
System.out.println(slots.get(9));
System.out.println(slots.get(10));
System.out.println(slots.get(11));
System.out.println("Test if class is triggerd");
}

Graphical User Interface and Main Method needs sorting

Im trying to sort my GUI so it will be able to use the methods from the other classes
in the JProject i also want to call it from the main.
The action listeners i had just learnt today so i need feedback on what to do there as well.
Any Ideas??
It would be a great help to get some insight on this!
(I am new to stack so my code posted in the explanation and in the actual script idk
why).
package test;
import javax.swing.JPanel;
import javax.swing.JLabel;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import java.awt.Container;
import java.awt.SystemColor;
import java.awt.Color;
import javax.swing.JRadioButton;
import java.awt.Font;
import java.io.File;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.util.ArrayList;
import java.util.Scanner;
//Main Method
public class TextUI{
protected StormTracker var;
public TextUI(StormTracker var)
{
this.var=var;
}
public static void main(String[] args)
{
ShowGUI gui = new SHOWGUI();
while(gui.running)
if(gui.buttonPress == -1)
continue;
StormTracker obj = new StormTracker("config.txt");
WeatherAuthority wa = new WeatherAuthority("stormfile.txt");
int xcoord=0;
int ycoord=0;
TextUI uiobj = new TextUI(obj);
Scanner scan = new Scanner(System.in);
while(true){
System.out.println("---------------------------------------------------------------");
System.out.println("Welcome to the StormTracker Application.");
System.out.println("Type the following commands to carry out the associated actions.");
System.out.println("\n");
System.out.println("----------------------{Commands Menu}---------------------
-------");
System.out.println("(1) NEW STORM x y");
System.out.println("(2) UPGRADE <name of storm>");
System.out.println("(3) DOWNGRADE <name of storm>");
System.out.println("(4) TICK");
System.out.println("(5) EXIT");
System.out.println("----------------------------------------------------------------");
String option=scan.nextLine();
if (option.substring(0,4).equals("TICK")){
obj.tick();
continue;
}
if (option.substring(0,9).equals("NEW STORM")){
if(option.length()==13){
xcoord = Integer.parseInt(option.substring(10,11));
ycoord = Integer.parseInt(option.substring(12,13));
}
else if (option.length()==14){
if (option.substring(11,12).equals(" ")){
xcoord = Integer.parseInt(option.substring(10,11));
ycoord = Integer.parseInt(option.substring(12,14));
}
else{
xcoord = Integer.parseInt(option.substring(10,12));
ycoord = Integer.parseInt(option.substring(13,14));
}
}
else if (option.length()==15){
xcoord = Integer.parseInt(option.substring(10,12));
ycoord = Integer.parseInt(option.substring(13,15));
}
obj.newstorm(xcoord,ycoord);
}
if (option.substring(0,7).equals("UPGRADE")){
String storm_name = option.substring(8);
obj.upgrade(storm_name);
}
if (option.substring(0,9).equals("DOWNGRADE")){
String storm_name = option.substring(10);
obj.downgrade(storm_name);
}
}
}
//GUI Class
public ShowGUI extends JPanel {
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
System.out.println("GUI Successfully Loaded");
createAndShow();
setToolTipText("Select Storm");
setBackground(Color.WHITE);
}
private void createAndShow(){
JPanel mainFrame = new JPanel("Storm Tracker V1.0");
}
private void addComponents(Container pane){
pane.setLayout(null);
JLabel lblStormTrackerV = DefaultComponentFactory.getInstance().createTitle("
Storm Tracker V1.0");
lblStormTrackerV.setBackground(SystemColor.activeCaption);
lblStormTrackerV.setFont(new Font("Calibri", Font.PLAIN, 22));
lblStormTrackerV.setBounds(162, 11, 430, 41);
add(lblStormTrackerV);
JRadioButton rdbtnMap = new JRadioButton("MAP");
rdbtnMap.setBounds(283, 59, 72, 23);
add(rdbtnMap);
JRadioButton rdbtnBulletin = new JRadioButton("Bulletin");
rdbtnBulletin.setBounds(406, 60, 72, 23);
add(rdbtnBulletin);
textField = new JTextField();
textField.setBounds(61, 103, 72, 20);
add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(61, 134, 72, 20);
add(textField_1);
textField_1.setColumns(10);
JTextPane txtpnX = new JTextPane();
txtpnX.setText("X:");
txtpnX.setBounds(23, 103, 28, 20);
add(txtpnX);
JTextPane txtpnY = new JTextPane();
txtpnY.setText("Y:");
txtpnY.setBounds(23, 134, 12, 20);
add(txtpnY);
JTextPane txtpnSpeed = new JTextPane();
txtpnSpeed.setText("Speed:");
txtpnSpeed.setBounds(10, 165, 41, 20);
add(txtpnSpeed);
textField_2 = new JTextField();
textField_2.setBounds(61, 165, 72, 20);
add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(61, 196, 72, 20);
add(textField_3);
textField_3.setColumns(10);
JTextPane txtpnSpeed_1 = new JTextPane();
txtpnSpeed_1.setText("Speed:");
txtpnSpeed_1.setBounds(10, 196, 41, 20);
add(txtpnSpeed_1);
JTextPane txtpnBearing = new JTextPane();
txtpnBearing.setText("Bearing:");
txtpnBearing.setBounds(10, 227, 41, 20);
add(txtpnBearing);
textField_4 = new JTextField();
textField_4.setBounds(61, 227, 72, 20);
add(textField_4);
textField_4.setColumns(10);
File input = new File(new
FileReader("C:\\Users\\620086793\\Documents\\test\\src\\test"));
List<String> strings = new ArrayList<String>();
try {
String line = null;
while (( line = input.readLine()) != null){
strings.add(line);
}
}
catch (FileNotFoundException e) {
System.err.println("Error, file " + "C:\\Users\\620086793\\Documents\\test\\src\\test" + " didn't exist.");
}
finally {
input.close();
}
String[] lineArray = strings.toArray(new String[]{});
JComboBox comboBox = new JComboBox(lineArray);
JComboBox comboBox = new JComboBox();
comboBox.setToolTipText("Select Storm");
comboBox.setEditable(true);
comboBox.setBounds(20, 60, 113, 20);
add(comboBox);
JButton btnUpdate = new JButton("UPDATE");
btnUpdate.setFont(new Font("Calibri", Font.BOLD, 11));
btnUpdate.setBounds(10, 273, 110, 28);
add(btnUpdate);
JButton btnNewButton = new JButton("NEW STORM\r\n");
btnNewButton.setFont(new Font("Calibri", Font.BOLD, 11));
btnNewButton.setBounds(10, 312, 110, 28);
add(btnNewButton);
addActionListeners();
pane.add(rdbtnMap);
pane.add(rdbtnBulletin);
pane.add(textField);
pane.add(textField_1);
pane.add(textField_2);
pane.add(textField_3);
pane.add(textField_4);
pane.add(comboBox);
pane.add(btnUpdate);
pane.add(btnNewButton);
}
//Im not sure how to properly add the action listeners i first learned about them
today.
private void addActionListeners(){
rdbtnMap.addActionListener(new ActionListener){
public void actionPerformed(ActionEvent ){
}
}
rdbtnBulletin.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
textField.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
textField_2.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
textField_3.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
textField_4.addActionListener(){
public void actionPerformed(ActionEvent ){
stormTracker.tick = textField_4;
}
}
comboBox.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
btnUpdate.addActionListener(){
public void actionPerformed(ActionEvent ){
obj.upgrade() = btnUpdate;
return btnUpdate;
}
}
btnNewButton.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
}

Enable labels and textfields based on the selected item in the combo box of another class

I have two separate classes: SubDimensions.java and Subtraction.java.
I wanted to enable some components in the Subtraction class based on the selected item in the combo box of the SubDimensions class. I tried making a switch statement in the SubDimensions class and a boolean function in the Subtraction class but it didn't work. I was wondering what could be the problem.
SubDimensions.java
package gui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
public class SubDimensions {
public JFrame frmSubDimensions;
public JComboBox comboBoxA;
/**
* Launch the application.
*/
public static void SubDimensions() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SubDimensions window = new SubDimensions();
window.frmSubDimensions.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public SubDimensions() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
frmSubDimensions = new JFrame();
frmSubDimensions.setTitle("VECTOR CALCULATOR");
frmSubDimensions.setBounds(100, 100, 330, 200);
frmSubDimensions.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmSubDimensions.getContentPane().setLayout(null);
JLabel lblVectorDimension = new JLabel("Vector dimensions:");
lblVectorDimension.setHorizontalAlignment(SwingConstants.CENTER);
lblVectorDimension.setBounds(10, 21, 294, 14);
frmSubDimensions.getContentPane().add(lblVectorDimension);
comboBoxA = new JComboBox();
comboBoxA.setModel(new DefaultComboBoxModel(new String[] {"2x1", "3x1", "4x1", "5x1", "6x1", "7x1", "8x1", "9x1", "10x1"}));
comboBoxA.setBounds(110, 46, 95, 20);
frmSubDimensions.getContentPane().add(comboBoxA);
JButton btnNewButton = new JButton("Okay");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Subtraction subtraction = new Subtraction();
subtraction.Subtraction();
frmSubDimensions.setVisible(false);
switch(comboBoxA.getSelectedItem().toString()){
case "3x1":{
subtraction.x3Visibility(true);
break;}
/*case "4x1":
lblX4.setEnabled(true);
break;*/
default:{
subtraction.x3Visibility(false);
break;}
}
}
});
btnNewButton.setBounds(62, 108, 89, 23);
frmSubDimensions.getContentPane().add(btnNewButton);
JButton btnCancel = new JButton("Cancel");
btnCancel.setBounds(167, 108, 89, 23);
frmSubDimensions.getContentPane().add(btnCancel);
}
}
Subtraction.java
package gui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;
public class Subtraction {
public JFrame frmSubtraction;
public JTextField textFieldx1;
public JTextField textFieldx2;
public JTextField textFieldx3;
public JTextField textFieldx4;
public JTextField textFieldx5;
public JTextField textFieldx6;
public JTextField textFieldx7;
public JTextField textFieldx8;
public JTextField textFieldx9;
public JTextField textFieldx10;
public JTextField textFieldy1;
public JTextField textFieldy2;
public JTextField textFieldy3;
public JTextField textFieldy4;
public JTextField textFieldy5;
public JTextField textFieldy6;
public JTextField textFieldy7;
public JTextField textFieldy8;
public JTextField textFieldy9;
public JTextField textFieldy10;
public JLabel lblX1;
public JLabel lblX2;
public JLabel lblX3;
public JLabel lblX4;
public JLabel lblX5;
public JLabel lblX6;
public JLabel lblX7;
public JLabel lblX8;
public JLabel lblX9;
public JLabel lblX10;
public JLabel lblY1;
public JLabel lblY2;
public JLabel lblY3;
public JLabel lblY4;
public JLabel lblY5;
public JLabel lblY6;
public JLabel lblY7;
public JLabel lblY8;
public JLabel lblY9;
public JLabel lblY10;
/**
* Launch the application.
*/
public static void Subtraction() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Subtraction window = new Subtraction();
window.frmSubtraction.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Subtraction() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize(){
SubDimensions subDimensions = new SubDimensions();
frmSubtraction = new JFrame();
frmSubtraction.setTitle("VECTOR CALCULATOR");
frmSubtraction.setBounds(100, 100, 400, 400);
frmSubtraction.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmSubtraction.getContentPane().setLayout(null);
lblX1 = new JLabel("x1");
lblX1.setBounds(10, 32, 20, 14);
frmSubtraction.getContentPane().add(lblX1);
textFieldx1 = new JTextField();
textFieldx1.setBounds(40, 29, 50, 20);
frmSubtraction.getContentPane().add(textFieldx1);
textFieldx1.setColumns(10);
lblX2 = new JLabel("x2");
lblX2.setBounds(10, 60, 20, 14);
frmSubtraction.getContentPane().add(lblX2);
textFieldx2 = new JTextField();
textFieldx2.setColumns(10);
textFieldx2.setBounds(40, 57, 50, 20);
frmSubtraction.getContentPane().add(textFieldx2);
lblX3 = new JLabel("x3");
lblX3.setEnabled(false);
lblX3.setBounds(10, 88, 20, 14);
frmSubtraction.getContentPane().add(lblX3);
textFieldx3 = new JTextField();
textFieldx3.setEnabled(false);
textFieldx3.setColumns(10);
textFieldx3.setBounds(40, 85, 50, 20);
frmSubtraction.getContentPane().add(textFieldx3);
lblX4 = new JLabel("x4");
lblX4.setEnabled(false);
lblX4.setBounds(10, 116, 20, 14);
frmSubtraction.getContentPane().add(lblX4);
textFieldx4 = new JTextField();
textFieldx4.setEnabled(false);
textFieldx4.setColumns(10);
textFieldx4.setBounds(40, 113, 50, 20);
frmSubtraction.getContentPane().add(textFieldx4);
lblX5 = new JLabel("x5");
lblX5.setEnabled(false);
lblX5.setBounds(10, 144, 20, 14);
frmSubtraction.getContentPane().add(lblX5);
textFieldx5 = new JTextField();
textFieldx5.setEnabled(false);
textFieldx5.setColumns(10);
textFieldx5.setBounds(40, 141, 50, 20);
frmSubtraction.getContentPane().add(textFieldx5);
lblX6 = new JLabel("x6");
lblX6.setEnabled(false);
lblX6.setBounds(10, 172, 20, 14);
frmSubtraction.getContentPane().add(lblX6);
textFieldx6 = new JTextField();
textFieldx6.setEnabled(false);
textFieldx6.setColumns(10);
textFieldx6.setBounds(40, 169, 50, 20);
frmSubtraction.getContentPane().add(textFieldx6);
lblX7 = new JLabel("x7");
lblX7.setEnabled(false);
lblX7.setBounds(10, 200, 20, 14);
frmSubtraction.getContentPane().add(lblX7);
textFieldx7 = new JTextField();
textFieldx7.setEnabled(false);
textFieldx7.setColumns(10);
textFieldx7.setBounds(40, 197, 50, 20);
frmSubtraction.getContentPane().add(textFieldx7);
lblX8 = new JLabel("x8");
lblX8.setEnabled(false);
lblX8.setBounds(10, 228, 20, 14);
frmSubtraction.getContentPane().add(lblX8);
textFieldx8 = new JTextField();
textFieldx8.setEnabled(false);
textFieldx8.setColumns(10);
textFieldx8.setBounds(40, 225, 50, 20);
frmSubtraction.getContentPane().add(textFieldx8);
lblX9 = new JLabel("x9");
lblX9.setEnabled(false);
lblX9.setBounds(10, 256, 20, 14);
frmSubtraction.getContentPane().add(lblX9);
textFieldx9 = new JTextField();
textFieldx9.setEnabled(false);
textFieldx9.setColumns(10);
textFieldx9.setBounds(40, 253, 50, 20);
frmSubtraction.getContentPane().add(textFieldx9);
lblX10 = new JLabel("x10");
lblX10.setEnabled(false);
lblX10.setBounds(10, 281, 30, 14);
frmSubtraction.getContentPane().add(lblX10);
textFieldx10 = new JTextField();
textFieldx10.setEnabled(false);
textFieldx10.setColumns(10);
textFieldx10.setBounds(40, 281, 50, 20);
frmSubtraction.getContentPane().add(textFieldx10);
lblY1 = new JLabel("y1");
lblY1.setBounds(126, 32, 20, 14);
frmSubtraction.getContentPane().add(lblY1);
textFieldy1 = new JTextField();
textFieldy1.setColumns(10);
textFieldy1.setBounds(156, 29, 50, 20);
frmSubtraction.getContentPane().add(textFieldy1);
lblY2 = new JLabel("y2");
lblY2.setBounds(126, 60, 20, 14);
frmSubtraction.getContentPane().add(lblY2);
textFieldy2 = new JTextField();
textFieldy2.setColumns(10);
textFieldy2.setBounds(156, 57, 50, 20);
frmSubtraction.getContentPane().add(textFieldy2);
textFieldy3 = new JTextField();
textFieldy3.setEnabled(false);
textFieldy3.setColumns(10);
textFieldy3.setBounds(156, 85, 50, 20);
frmSubtraction.getContentPane().add(textFieldy3);
lblY3 = new JLabel("y3");
lblY3.setEnabled(false);
lblY3.setBounds(126, 88, 20, 14);
frmSubtraction.getContentPane().add(lblY3);
lblY4 = new JLabel("y4");
lblY4.setEnabled(false);
lblY4.setBounds(126, 116, 20, 14);
frmSubtraction.getContentPane().add(lblY4);
textFieldy4 = new JTextField();
textFieldy4.setEnabled(false);
textFieldy4.setColumns(10);
textFieldy4.setBounds(156, 113, 50, 20);
frmSubtraction.getContentPane().add(textFieldy4);
textFieldy5 = new JTextField();
textFieldy5.setEnabled(false);
textFieldy5.setColumns(10);
textFieldy5.setBounds(156, 141, 50, 20);
frmSubtraction.getContentPane().add(textFieldy5);
lblY5 = new JLabel("y5");
lblY5.setEnabled(false);
lblY5.setBounds(126, 144, 20, 14);
frmSubtraction.getContentPane().add(lblY5);
lblY6 = new JLabel("y6");
lblY6.setEnabled(false);
lblY6.setBounds(126, 172, 20, 14);
frmSubtraction.getContentPane().add(lblY6);
textFieldy6 = new JTextField();
textFieldy6.setEnabled(false);
textFieldy6.setColumns(10);
textFieldy6.setBounds(156, 169, 50, 20);
frmSubtraction.getContentPane().add(textFieldy6);
textFieldy7 = new JTextField();
textFieldy7.setEnabled(false);
textFieldy7.setColumns(10);
textFieldy7.setBounds(156, 197, 50, 20);
frmSubtraction.getContentPane().add(textFieldy7);
lblY7 = new JLabel("y7");
lblY7.setEnabled(false);
lblY7.setBounds(126, 200, 20, 14);
frmSubtraction.getContentPane().add(lblY7);
lblY8 = new JLabel("y8");
lblY8.setEnabled(false);
lblY8.setBounds(126, 228, 20, 14);
frmSubtraction.getContentPane().add(lblY8);
textFieldy8 = new JTextField();
textFieldy8.setEnabled(false);
textFieldy8.setColumns(10);
textFieldy8.setBounds(156, 225, 50, 20);
frmSubtraction.getContentPane().add(textFieldy8);
lblY9 = new JLabel("y9");
lblY9.setEnabled(false);
lblY9.setBounds(126, 256, 20, 14);
frmSubtraction.getContentPane().add(lblY9);
textFieldy9 = new JTextField();
textFieldy9.setEnabled(false);
textFieldy9.setColumns(10);
textFieldy9.setBounds(156, 253, 50, 20);
frmSubtraction.getContentPane().add(textFieldy9);
textFieldy10 = new JTextField();
textFieldy10.setEnabled(false);
textFieldy10.setColumns(10);
textFieldy10.setBounds(156, 281, 50, 20);
frmSubtraction.getContentPane().add(textFieldy10);
lblY10 = new JLabel("y10");
lblY10.setEnabled(false);
lblY10.setBounds(126, 281, 30, 14);
frmSubtraction.getContentPane().add(lblY10);
JLabel lblVectorU = new JLabel("Vector u:");
lblVectorU.setBounds(10, 11, 80, 14);
frmSubtraction.getContentPane().add(lblVectorU);
JLabel lblVectorV = new JLabel("Vector v:");
lblVectorV.setBounds(126, 11, 80, 14);
frmSubtraction.getContentPane().add(lblVectorV);
JTextArea textAreaZ = new JTextArea();
textAreaZ.setEditable(false);
textAreaZ.setBounds(251, 27, 123, 274);
frmSubtraction.getContentPane().add(textAreaZ);
JButton btnNewButton = new JButton("Calculate");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x1 = Integer.parseInt(textFieldx1.getText());
int x2 = Integer.parseInt(textFieldx2.getText());
int x3 = Integer.parseInt(textFieldx3.getText());
int x4 = Integer.parseInt(textFieldx4.getText());
int x5 = Integer.parseInt(textFieldx5.getText());
int x6 = Integer.parseInt(textFieldx6.getText());
int x7 = Integer.parseInt(textFieldx7.getText());
int x8 = Integer.parseInt(textFieldx8.getText());
int x9 = Integer.parseInt(textFieldx9.getText());
int x10 = Integer.parseInt(textFieldx10.getText());
int y1 = Integer.parseInt(textFieldy1.getText());
int y2 = Integer.parseInt(textFieldy2.getText());
int y3 = Integer.parseInt(textFieldy3.getText());
int y4 = Integer.parseInt(textFieldy4.getText());
int y5 = Integer.parseInt(textFieldy5.getText());
int y6 = Integer.parseInt(textFieldy6.getText());
int y7 = Integer.parseInt(textFieldy7.getText());
int y8 = Integer.parseInt(textFieldy8.getText());
int y9 = Integer.parseInt(textFieldy9.getText());
int y10 = Integer.parseInt(textFieldy10.getText());
int z1 = x1 - y1;
int z2 = x2 - y2;
int z3 = x3 - y3;
int z4 = x4 - y4;
int z5 = x5 - y5;
int z6 = x6 - y6;
int z7 = x7 - y7;
int z8 = x8 - y8;
int z9 = x9 - y9;
int z10 = x10 - y10;
textAreaZ.setText(Integer.toString(z1) + "\n" + Integer.toString(z2) + "\n" + Integer.toString(z3) + "\n" + Integer.toString(z4) + "\n" + Integer.toString(z5) + "\n" + Integer.toString(z6) + "\n" + Integer.toString(z7) + "\n" + Integer.toString(z8) + "\n" + Integer.toString(z9) + "\n" + Integer.toString(z10));
}
});
btnNewButton.setBounds(87, 327, 89, 23);
frmSubtraction.getContentPane().add(btnNewButton);
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
subDimensions.SubDimensions();
frmSubtraction.setVisible(false);
}
});
btnCancel.setBounds(208, 327, 89, 23);
frmSubtraction.getContentPane().add(btnCancel);
JLabel lblVectorUv = new JLabel("Vector u+v:");
lblVectorUv.setBounds(251, 11, 123, 14);
frmSubtraction.getContentPane().add(lblVectorUv);
}
public void x3Visibility (boolean enabled){
lblX3.setEnabled(enabled);
textFieldx3.setEnabled(enabled);
lblY3.setEnabled(enabled);
textFieldy3.setEnabled(enabled);
}
/*public void x4Visibility (boolean visibility){
lblX4.setEnabled(true);
textFieldx4.setEnabled(true);
lblY4.setEnabled(true);
textFieldy4.setEnabled(true);
}*/
}
You are not using the parameter enabled in Subtraction.java, change to:
public void x3Visibility (boolean enabled){
lblX3.setEnabled(enabled);
textFieldx3.setEnabled(enabled);
lblY3.setEnabled(enabled);
textFieldy3.setEnabled(enabled);
}
Edit: looks like you lack the fundamental understanding of object-oriented programming (OOP).
When you call
Subtraction subtraction = new Subtraction();
subtraction.Subtraction();
You created another instance of Subtraction called window:
public static void Subtraction() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Subtraction window = new Subtraction();
window.frmSubtraction.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
substraction is not visible while window is. But window is not in the scope of actionPerformed and hence you cannot access it. For now, you can change the actionPerformed to below to resolve your issue temporarily. But I strongly recommend you to: 1) revisit the concept of OOP 2) try to use arrays or collections for your labels and textfields instead of hardcoding them one by one
public void actionPerformed(ActionEvent e) {
Subtraction subtraction = new Subtraction();
subtraction.frmSubtraction.setVisible(true);
frmSubDimensions.setVisible(false);
switch(comboBoxA.getSelectedItem().toString()){
case "3x1":{
subtraction.x3Visibility(true);
break;}
/*case "4x1":
lblX4.setEnabled(true);
break;*/
default:{
subtraction.x3Visibility(false);
break;}
}
}

Java – How do I use counter data for analytics?

I have a JFrame with three tabs: Home, Statistics, and About.
On the Home tab, I have a counter that tracks how many times you click your left or right arrow, and it updates a counter (I use the KeyListener method):
On the Statistics tab, I want to somehow import these Left and Right values that I generate from the counter.
But, I don't know how to. Can someone help me out here?
Edit:
Here is the code for the Home tab (Left/Right counter)
import java.awt.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class HomePanel extends JPanel implements KeyListener {
// Variables
private int counter = 0;
private int counter2 = 0;
private JLabel counterLabel;
private JLabel counterLabel2;
private JButton countUpButton;
private JButton countDownButton;
// Panel Elements
public HomePanel() {
countUpButton = new JButton ("Left");
countUpButton.setBounds(195, 121, 75, 29);
countUpButton.addKeyListener(this);
countDownButton = new JButton ("Right");
countDownButton.setBounds(195, 162, 77, 29);
countDownButton.addKeyListener(this);
counterLabel = new JLabel("" + counter);
counterLabel.setBounds(282, 126, 34, 16);
counterLabel2 = new JLabel("" + counter2);
counterLabel2.setBounds(282, 167, 34, 16);
setLayout(null);
add (countUpButton);
add (counterLabel);
add (countDownButton);
add (counterLabel2);
}
// Key Listener
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
// Ensures counter updates once per stroke
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
counter++;
counterLabel.setText("" + counter);
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
counter2++;
counterLabel2.setText("" + counter2);
}
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
And here is the code for the Statistics tab:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class StatsPanel extends JPanel {
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
public StatsPanel() {
setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(41, 43, 371, 234);
add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("LEFT:");
lblNewLabel.setBounds(115, 39, 33, 16);
panel.add(lblNewLabel);
JLabel lblRight = new JLabel("RIGHT:");
lblRight.setBounds(105, 67, 43, 16);
panel.add(lblRight);
JLabel lblNewLabel_2 = new JLabel("TOTAL:");
lblNewLabel_2.setBounds(102, 95, 46, 16);
panel.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("% LEFT:");
lblNewLabel_3.setBounds(102, 123, 46, 16);
panel.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("% RIGHT: ");
lblNewLabel_4.setBounds(91, 151, 60, 16);
panel.add(lblNewLabel_4);
textField_2 = new JTextField();
textField_2.setBounds(160, 33, 134, 28);
panel.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(160, 61, 134, 28);
panel.add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(160, 89, 134, 28);
panel.add(textField_4);
textField_4.setColumns(10);
textField_5 = new JTextField();
textField_5.setBounds(160, 117, 134, 28);
panel.add(textField_5);
textField_5.setColumns(10);
textField_6 = new JTextField();
textField_6.setBounds(160, 145, 134, 28);
panel.add(textField_6);
textField_6.setColumns(10);
JButton btnCalculate = new JButton("Calculate");
btnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Variables
double num1, num2, ans, percEq1, percEq2, percLeft, percRight;
try {
num1 = Double.parseDouble(textField_2.getText());
num2 = Double.parseDouble(textField_3.getText());
ans = num1 + num2;
textField_4.setText(Double.toString(ans));
percEq1 = (num1/(num1+num2))*100;
percLeft = Math.round(percEq1);
textField_5.setText(Double.toString(percLeft));
percEq2 = (num2/(num1+num2))*100;
percRight = Math.round(percEq2);
textField_6.setText(Double.toString(percRight));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Please enter a valid number!");
}
}
});
btnCalculate.setBounds(79, 185, 117, 29);
panel.add(btnCalculate);
JButton btnRest = new JButton("Reset");
btnRest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField_2.setText("");
textField_3.setText("");
textField_4.setText("");
textField_5.setText("");
textField_6.setText("");
}
});
btnRest.setBounds(197, 185, 117, 29);
panel.add(btnRest);
}
}
I suggest using variables in the parent container so every tab can access them or, from the statistics tab, ask the values to the parent frame and the parent asks to the home tab.
It's not a short code and you haven't attached yours, so I hope your get the idea.
In the main container you have something like this:
KeyPressStats statistics = new KeyPressStats();
HomePanel home = new HomePanel(statistics);
StatsPanel stats = new StatsPanel(statistics);
JTabbedPane tabbed = new JTabbedPane();
tabbed.addTab("Home", home);
tabbed.addTab("Stats", stats);
tabbed.setVisible(true);
KeyPressStats is as follows:
public class KeyPressStats {
private int leftCount = 0;
private int rightCount = 0;
public int getLeftCount() {
return leftCount;
}
public void setLeftCount(int leftCount) {
this.leftCount = leftCount;
}
public int getRightCount() {
return rightCount;
}
public void setRightCount(int rightCount) {
this.rightCount = rightCount;
}
public void incrementRight() {
rightCount++;
}
public void incrementLeft() {
leftCount++;
}
}
And the code in the Home and stats tabs will have these changes:
public class HomePanel extends JPanel implements KeyListener {
...
private KeyPressStats stats;
public HomePanel(KeyPressStats stats) {
this.stats = stats;
countUpButton = new JButton("Left");
countUpButton.setBounds(195, 121, 75, 29);
...
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
stats.incrementLeft();
counterLabel.setText("" + stats.getLeftCount());
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
stats.incrementRight();
counterLabel2.setText("" + stats.getRightCount());
}
}
public class StatsPanel extends JPanel {
...
public StatsPanel(KeyPressStats stats) {
...
textField_2 = new JTextField(""+stats.getLeftCount());
textField_2.setBounds(160, 33, 134, 28);
...
textField_3 = new JTextField(""+stats.getRightCount());
textField_3.setBounds(160, 61, 134, 28);
...
}
}

Toggle Java JPanels - each panel deifferent class

I'm a beginer in Java GUI, and i have an issue with trying to toggle between panel in other class.
My main Class "MyBoxGUI" has 2 Panels, "Login" and "UserMenu"
The default is "Login" panel, when pressing "OK" the window moved to "UserMenu" Panel.
So far so good while in the same class.
The issue is, when i set a new panel in new class and setvisibe that panel, i can't go back to "UserMenu" panel - (The actionlistener is working fine).
Can someone help me please?
MyBoxGUI Class
package main;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.CardLayout;
import java.awt.Component;
//import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Toolkit;
import GUIs.*;
//import ocsf.client.*;
public class MyBoxGUI extends JPanel
{
private Mtds mtd;
public JFrame frmMybox;
private static JTextField UserName;
private static JTextField port;
private static JTextField IP;
private static JPasswordField passwordField;
public final JPanel Login = new JPanel();
public final JPanel UserMenu = new JPanel();
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
MyBoxGUI window = new MyBoxGUI();
window.frmMybox.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MyBoxGUI()
{
mtd = new Mtds();
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize()
{
frmMybox = new JFrame();
frmMybox.setFont(new Font("Tempus Sans ITC", Font.BOLD, 15));
// frmMybox.setIconImage(Toolkit.getDefaultToolkit().getImage(MyBoxGUI.class.getResource("/images/gift-ideas-gift-card-bridal-gift-baby-shower-gift-gift-box-groom-gift-christmas-gift-party-gift-gift-for-wedding-friend-gift-birthday-gift-baby-gift-good-gift-box-ideas-for-friend-necklace-gift-box.jpg")));
frmMybox.setBounds(100, 100, 800, 500);
frmMybox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMybox.getContentPane().setLayout(new CardLayout(0, 0));
frmMybox.setTitle("MyBox");
frmMybox.getContentPane().add(Login);
Login.setLayout(null);
JButton btnNewButton = new JButton("OK");
btnNewButton.addActionListener(new ActionListener()
{
#SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent arg0)
{
Boolean ok = false;
String UName, Pass, Prt, SIP;
UName = UserName.getText();
Pass = passwordField.getText();
Prt = port.getText();
SIP = IP.getText();
if( ( mtd.isUserValid(UName) && mtd.isPasswordValid(Pass) && mtd.isPortValid(Prt) && mtd.isIPValid(SIP))
&& ( !UName.equals("") && !Pass.equals("") && !Prt.equals("") && !SIP.equals("") ) )
ok = true;
else
{
if(!mtd.isUserValid(UName) || UName.equals(""))
JOptionPane.showMessageDialog(frmMybox,"User Name Can Contain Only The Following Characters: ( 0-9 , a-z , A-Z , _ )"
+ " ","Invalid characters",JOptionPane.WARNING_MESSAGE);
else if(!mtd.isPasswordValid(Pass) || Pass.equals(""))
JOptionPane.showMessageDialog(frmMybox,"Invalid characters","Error Message",JOptionPane.WARNING_MESSAGE);
else if(!mtd.isPortValid(Prt) || Prt.equals(""))
JOptionPane.showMessageDialog(frmMybox,"Port Can Contain Only Numbers","Invalid characters",JOptionPane.WARNING_MESSAGE);
else if(!mtd.isIPValid(SIP) || SIP.equals(""))
JOptionPane.showMessageDialog(frmMybox,"IP Address Can Contain Only Numbers seperated By Dots '.' ","Invalid characters",JOptionPane.WARNING_MESSAGE);
}
if(ok)
{
//LoginController user = new LoginController();
//chat= new ClientGUI(IP.getText(),port.getText());
//client = ClientGUI.getClient();
//msg=new Msg("login",user);
//client.handleMessageFromClientUI(msg);
setScreen(Login,UserMenu);
// frmMybox.getContentPane().add(UserMenu, "UserMenu");
// UserMenu.setVisible(true);
// Login.setVisible(false);
}
}
});
btnNewButton.setBounds(344, 313, 82, 48);
Login.add(btnNewButton);
JLabel lblWellcomeToMybox = new JLabel("Wellcome to MyBox");
lblWellcomeToMybox.setForeground(new Color(51, 204, 255));
lblWellcomeToMybox.setFont(new Font("Arial", Font.PLAIN, 36));
lblWellcomeToMybox.setBounds(224, 23, 327, 42);
Login.add(lblWellcomeToMybox);
JLabel lblUserName = new JLabel("User Name:");
lblUserName.setBounds(268, 109, 89, 14);
lblUserName.setFont(new Font("Arial", Font.PLAIN, 14));
Login.add(lblUserName);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(268, 139, 69, 14);
lblPassword.setFont(new Font("Arial", Font.PLAIN, 14));
Login.add(lblPassword);
JLabel lblServerPort = new JLabel("Server port:");
lblServerPort.setBounds(268, 197, 82, 14);
lblServerPort.setFont(new Font("Arial", Font.PLAIN, 14));
Login.add(lblServerPort);
JLabel lblServerIp = new JLabel("Server IP:");
lblServerIp.setBounds(266, 222, 71, 14);
lblServerIp.setFont(new Font("Arial", Font.PLAIN, 14));
Login.add(lblServerIp);
UserName = new JTextField();
UserName.setBounds(406, 107, 96, 20);
Login.add(UserName);
UserName.setColumns(10);
port = new JTextField();
port.setBounds(406, 195, 96, 20);
Login.add(port);
port.setColumns(10);
IP = new JTextField();
IP.setBounds(406, 220, 96, 20);
Login.add(IP);
IP.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(406, 137, 96, 20);
Login.add(passwordField);
frmMybox.getContentPane().add(UserMenu, "User Menu");
UserMenu.setLayout(null);
JButton LeaveGOI = new JButton("Back");
LeaveGOI.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
setScreen(UserMenu,Login);
}
});
LeaveGOI.setBounds(10, 66, 153, 23);
UserMenu.add(LeaveGOI);
JButton btnJoinAGoi = new JButton("Join a GOI");
btnJoinAGoi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
}
});
btnJoinAGoi.setBounds(10, 100, 153, 23);
UserMenu.add(btnJoinAGoi);
JButton btnSearchForA = new JButton("Search for a GOI");
btnSearchForA.setBounds(10, 134, 153, 23);
UserMenu.add(btnSearchForA);
JButton btnCreateAGoi = new JButton("Create a GOI");
btnCreateAGoi.setBounds(10, 32, 153, 23);
UserMenu.add(btnCreateAGoi);
JButton btnFilesSharedWith = new JButton("Files shared with you");
btnFilesSharedWith.setBounds(271, 66, 153, 23);
UserMenu.add(btnFilesSharedWith);
JButton btnUploadAFile = new JButton("Upload a file");
btnUploadAFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
JPanel UpLoad = new UploadFile();
setScreen(UserMenu,UpLoad);
// frmMybox.getContentPane().add(UpLoad, "UpLoad");
// UserMenu.setVisible(false);
// UpLoad.setVisible(true);
}
});
btnUploadAFile.setBounds(271, 32, 153, 23);
UserMenu.add(btnUploadAFile);
JButton btnSignout = new JButton("Sign-Out");
btnSignout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int dialog=JOptionPane.showConfirmDialog(Login, getUserName()+", are you sure you wants to leave?", "Do you want to leave?", JOptionPane.YES_NO_OPTION);
if(dialog==0){
//client.quit();
}
}
});
btnSignout.setBounds(293, 227, 131, 23);
UserMenu.add(btnSignout);
JButton btnHelpme = new JButton("Help-Me");
btnHelpme.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JOptionPane.showMessageDialog(Login,"Hello "+ getUserName()+", wellcom to Mybox!\n"
+ "Inside GOIs buttons you can ask to join, leave and even create a GOI.\n"
+ "Inside Files buttons you can check and edit files people shared with you.\n"
+ "You can even become an uploader and share files you own.\n"
+ "We wish you good luck and have fun using MyBox!");
}
});
btnHelpme.setBounds(10, 227, 131, 23);
UserMenu.add(btnHelpme);
}
public static String getUserName(){
return UserName.getText();
}
#SuppressWarnings("deprecation")
public static String getPassword(){
return passwordField.getText();
}
public static String getIP(){
return IP.getText();
}
public static String getPort(){
return port.getText();
}
public void setScreen(JPanel fls, JPanel tru)
{
frmMybox.getContentPane().add(tru);
tru.setVisible(true);
fls.setVisible(false);
}
}
UploadFile Class
package GUIs;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import main.MyBoxGUI;
public class UploadFile extends MyBoxGUI {
private JTextField textField;
/**
* Create the panel.
*/
public static final JPanel UpLoad = new JPanel();
public UploadFile()
{
setBounds(100, 100, 800, 500);
setLayout(null);
JButton btnNewButton = new JButton("Back");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
setScreen(UpLoad,UserMenu);
System.out.print("A+++");
}
});
btnNewButton.setBounds(126, 145, 205, 30);
add(btnNewButton);
textField = new JTextField();
textField.setBounds(409, 150, 300, 20);
add(textField);
textField.setColumns(10);
JButton btnDone = new JButton("Done");
btnDone.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnDone.setBounds(365, 223, 89, 30);
add(btnDone);
JButton btnHelpMe = new JButton("Help Me");
btnHelpMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnHelpMe.setBounds(188, 390, 122, 52);
add(btnHelpMe);
JButton SignOut = new JButton("Sign-Out");
SignOut.setBounds(501, 392, 122, 48);
add(SignOut);
}
}
public void setScreen(JPanel fls, JPanel tru)
{
frmMybox.getContentPane().add(tru);
tru.setVisible(true);
fls.setVisible(false);
frmMybox.getContentPane().revalidate();
frmMybox.getContentPane().repaint();
}
update your setScreen method as above.

Categories

Resources