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");
}
Related
Im trying to insert the Jcombobox value into my sqlite database but whenever I run the program, it only inserts '9' in to my database. Please advise. I am trying to import the integer that the user picks into my sqlite database. For some reason, even with the action listener, the default value, 9, is still being inputed into the table and im not sure why. Here is the full code, not including the connection and the Jtable.
public class create extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
create frame = new create();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection connect = null;
private JTextField textField_2;
String uniqueString = UUID.randomUUID().toString();
private JTextField textField_3;
/**
* Create the frame.
*/
public create() {
connect = connection.dbConnector();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(new Color(204, 204, 204));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblStudentName = new JLabel("Student ID:");
lblStudentName.setBounds(96, 69, 72, 16);
contentPane.add(lblStudentName);
textField = new JTextField();
textField.setBounds(173, 64, 216, 26);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblGrade = new JLabel("Grade:");
lblGrade.setBounds(125, 107, 47, 16);
contentPane.add(lblGrade);
JComboBox comboBox = new JComboBox();
comboBox.addItem(9);
comboBox.addItem(10);
comboBox.addItem(11);
comboBox.addItem(12);
comboBox.setBounds(173, 102, 72, 29);
contentPane.add(comboBox);
comboBox.setSelectedItem(9);
comboBox.addActionListener(comboBox);
int selectedNumber = (int)comboBox.getSelectedItem();
JLabel lblInputBookOver = new JLabel("Teacher:");
lblInputBookOver.setBounds(111, 146, 61, 21);
contentPane.add(lblInputBookOver);
textField_1 = new JTextField();
textField_1.setBounds(173, 143, 216, 26);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton button = new JButton("<");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int count = 0; count <= 0; count++) {
//dispose();
options sc = new options();
sc.setVisible(true);
}
}
});
button.setBounds(6, 6, 30, 29);
contentPane.add(button);
JLabel lblStudentname = new JLabel("Student Name:");
lblStudentname.setBounds(74, 31, 99, 16);
contentPane.add(lblStudentname);
textField_2 = new JTextField();
textField_2.setBounds(173, 26, 216, 26);
contentPane.add(textField_2);
textField_2.setColumns(10);
JLabel lblEmail = new JLabel("Email:");
lblEmail.setBounds(125, 180, 42, 26);
contentPane.add(lblEmail);
textField_3 = new JTextField();
textField_3.setBounds(173, 180, 216, 26);
contentPane.add(textField_3);
textField_3.setColumns(10);
JButton btnCheckout = new JButton("Checkout");
btnCheckout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final String uniqueString = UUID.randomUUID().toString().replace("-", "");
try {
String query = "insert into data ('Name', 'Student ID', 'Teacher', 'Grade', 'Email', 'Ebook') values (?,?,?,?,?,?)";
PreparedStatement pst = connect.prepareStatement(query);
pst.setString(1,textField_2.getText() );
pst.setString(2,textField.getText() );
pst.setString(3,textField_1.getText() );
pst.setInt(4, selectedNumber);
pst.setString(5,textField_3.getText() );
pst.setString(6, uniqueString);
for (int count = 0; count <= 0; count++) {
//dispose();
confirm sc = new confirm();
sc.setVisible(true);
count = 0;
}
pst.execute();
pst.close();
}
catch (Exception w){
w.printStackTrace();
}
}
});
btnCheckout.setBounds(173, 218, 117, 29);
contentPane.add(btnCheckout);
}
}
It inserts 9 to your database because the selected item is always 9, and this because you add it first to your combobox. In order to insert the selected value, you will have to use an ActionListener. Then you can take user's selection and do whatever you want.
An example of its usage:
import java.awt.FlowLayout;
import java.io.FileNotFoundException;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class ComboBox extends JFrame {
public ComboBox() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
getContentPane().setLayout(new FlowLayout());
Integer[] numbers = { 4, 5, 8, 123, 42, 634 };
JComboBox<Integer> comboBox = new JComboBox<>(numbers);
comboBox.setSelectedItem(42); // The initial selection is 42.
comboBox.addActionListener(e -> {
int selectedNumber = (int) comboBox.getSelectedItem();
System.out.println("Selected number: " + selectedNumber);
// Do whatever with selected number
});
add(comboBox);
}
public static void main(String[] args) throws FileNotFoundException {
SwingUtilities.invokeLater(() -> new ComboBox().setVisible(true));
}
}
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 ){
}
}
}
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;}
}
}
I am having an issue that is giving me an error message and a blank applet is appearing. The error message says load:class Myapp.class not found and java.lang.ClassNotFoundException and then a list of lines saying # and then some other jargon.
If I run try to run the program as a Jcreator applet like I expected I get the prior stated error and if I run it in an application it just gives me the main method error.
Here is my program:
package MyPack;
import javax.swing.*;
import java.awt.event.*;
public class Myapp extends JApplet implements ItemListener{
JButton jbtnOne;
JButton jbtnTwo;
JLabel jlab;
JComboBox comboBox;
JComboBox comboBox_1;
JCheckBox chckbxPizza;
JCheckBox chckbxBurger;
JCheckBox chckbxSalad;
JLabel label_3;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
JTextArea txtrCommentsForSpecial;
JTextArea txtrCommentsForSpecial_2;
JTextArea txtrCommentsForSpecial_1;
JInternalFrame internalFrame;
public Myapp() {
}
public void init() {
setSize(700,640);
// setVisible(true);
try {
SwingUtilities.invokeAndWait(new Runnable () {
public void run() {
guiInit(); // initialize the GUI
}
});
} catch(Exception exc) {
System.out.println("Can't create because of "+ exc);
}
}
// Called second, after init(). Also called
// whenever the applet is restarted.
public void start() {
// Not used by this applet.
}
// Called when the applet is stopped.
public void stop() {
// Not used by this applet.
}
// Called when applet is terminated. This is
// the last method executed.
public void destroy() {
// Not used by this applet.
}
// Setup and initialize the GUI.
private void guiInit() {
// Create two buttons and a label.
// setLayout(null);
String a[] = { "Chiken Tikka", "Fajita", "Vegetable"};
comboBox = new JComboBox(a);
comboBox.setBounds(115, 103, 100, 20);
comboBox.addItemListener(this);
getContentPane().add(comboBox);
String b[] = { "small", "medium","large","extra large"};
comboBox_1 = new JComboBox(b);
// comboBox_1.addItemListener(this);
// comboBox_1.setEnabled(false);
getContentPane().add(comboBox_1);
comboBox_1.setBounds(290, 103, 140, 20);
// setSize(300, 300);
getContentPane().setLayout(null);
JLabel lblSelectMenu = new JLabel("Select Menu");
lblSelectMenu.setForeground(new Color(72, 61, 139));
lblSelectMenu.setFont(new Font("Segoe Script", Font.BOLD, 16));
lblSelectMenu.setBounds(58, 10, 107, 34);
getContentPane().add(lblSelectMenu);
chckbxPizza = new JCheckBox("Pizza");
chckbxPizza.setBounds(37, 64, 97, 23);
getContentPane().add(chckbxPizza);
JLabel lblFlavour = new JLabel("flavour");
lblFlavour.setBounds(60, 103, 45, 20);
getContentPane().add(lblFlavour);
JLabel lblSize = new JLabel("size");
lblSize.setBounds(253, 103, 45, 20);
getContentPane().add(lblSize);
JLabel lblQty = new JLabel("Qty");
lblQty.setBounds(451, 103, 35, 20);
getContentPane().add(lblQty);
textField = new JTextField();
textField.setBounds(485, 103, 86, 20);
getContentPane().add(textField);
textField.setColumns(10);
JLabel lblComments = new JLabel("comments");
lblComments.setBounds(139, 154, 76, 20);
getContentPane().add(lblComments);
txtrCommentsForSpecial = new JTextArea();
txtrCommentsForSpecial.setForeground(new Color(220, 220, 220));
txtrCommentsForSpecial.setText("comments for special features");
txtrCommentsForSpecial.setBounds(210, 154, 268, 61);
getContentPane().add(txtrCommentsForSpecial);
chckbxBurger = new JCheckBox("Burger");
chckbxBurger.setBounds(37, 236, 97, 23);
getContentPane().add(chckbxBurger);
JLabel label = new JLabel("flavour");
label.setBounds(60, 286, 45, 20);
getContentPane().add(label);
String c[] = { "Fish", "Vegetable", "Chicken"};
JComboBox comboBox_2 = new JComboBox(c);
comboBox_2.setBounds(115, 286, 100, 20);
getContentPane().add(comboBox_2);
JLabel label_1 = new JLabel("size");
label_1.setBounds(253, 286, 45, 20);
getContentPane().add(label_1);
JComboBox comboBox_3 = new JComboBox(b);
comboBox_3.setBounds(290, 286, 110, 20);
getContentPane().add(comboBox_3);
JLabel label_2 = new JLabel("Qty");
label_2.setBounds(451, 286, 35, 20);
getContentPane().add(label_2);
textField_1 = new JTextField();
textField_1.setBounds(485, 286, 86, 20);
getContentPane().add(textField_1);
textField_1.setColumns(10);
label_3 = new JLabel("comments");
label_3.setBounds(139, 340, 76, 20);
getContentPane().add(label_3);
txtrCommentsForSpecial_1 = new JTextArea();
txtrCommentsForSpecial_1.setForeground(new Color(220, 220, 220));
txtrCommentsForSpecial_1.setText("comments for special features");
txtrCommentsForSpecial_1.setBounds(210, 340, 268, 61);
getContentPane().add(txtrCommentsForSpecial_1);
chckbxSalad = new JCheckBox("Soft Drink");
chckbxSalad.setBounds(37, 424, 97, 23);
getContentPane().add(chckbxSalad);
JLabel label_4 = new JLabel("flavour");
label_4.setBounds(60, 475, 45, 20);
getContentPane().add(label_4);
String f[] = { "Pepsi", "Coca Cola", "Sprite","7up"};
JComboBox comboBox_4 = new JComboBox(f);
comboBox_4.setBounds(115, 475, 86, 20);
getContentPane().add(comboBox_4);
JLabel label_5 = new JLabel("size");
label_5.setBounds(253, 475, 45, 20);
getContentPane().add(label_5);
JComboBox comboBox_5 = new JComboBox(b);
comboBox_5.setBounds(290, 475, 110, 20);
getContentPane().add(comboBox_5);
JLabel label_6 = new JLabel("Qty");
label_6.setBounds(451, 475, 35, 20);
getContentPane().add(label_6);
textField_2 = new JTextField();
textField_2.setBounds(485, 475, 86, 20);
getContentPane().add(textField_2);
textField_2.setColumns(10);
JLabel label_7 = new JLabel("comments");
label_7.setBounds(139, 546, 76, 20);
getContentPane().add(label_7);
txtrCommentsForSpecial_2 = new JTextArea();
txtrCommentsForSpecial_2.setForeground(new Color(220, 220, 220));
txtrCommentsForSpecial_2.setText("comments for special features");
txtrCommentsForSpecial_2.setBounds(210, 546, 268, 61);
getContentPane().add(txtrCommentsForSpecial_2);
JButton btnNewButton = new JButton("Ok");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
internalFrame = new JInternalFrame("Order Details");
internalFrame.setBounds(100, 80, 450, 300);
internalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
internalFrame.getContentPane().setLayout(null);
internalFrame.setVisible(true);
JTextArea textArea = new JTextArea();
textArea.setBounds(67, 52, 270, 149);
internalFrame.getContentPane().add(textArea);
JButton btnOk = new JButton("Ok");
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
internalFrame.setVisible(false);
}
});
btnOk.setBounds(164, 212, 89, 23);
internalFrame.getContentPane().add(btnOk);
JLabel lblOrderYouPlaced = new JLabel("Order You Placed");
lblOrderYouPlaced.setBounds(67, 26, 98, 14);
internalFrame.getContentPane().add(lblOrderYouPlaced);
getContentPane().add(internalFrame);
}
});
btnNewButton.setBounds(162, 616, 89, 23);
getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Reset");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
textField_1.setText("");
textField_2.setText("");
chckbxPizza.setText("");
chckbxBurger.setText("");
chckbxSalad.setText("");
txtrCommentsForSpecial.setText("");
txtrCommentsForSpecial_2.setText("");
txtrCommentsForSpecial_1.setText("");
}
});
btnNewButton_1.setBounds(279, 616, 89, 23);
getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("Cancel");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnNewButton_2.setBounds(389, 616, 89, 23);
getContentPane().add(btnNewButton_2);
}
public void itemStateChanged(ItemEvent e) {
String b[] = { "Chikken Tikka", "Fajita" };
String c[] = { "Fish", "Vegetable", "Chicken"};
String d[] = { "Russian Salad", "Fruit Salad", "Beans Salad"};
String f[] = { "Pepsi", "Coca Cola", "Sprite","7up"};
if (e.getSource() == comboBox) {
if (comboBox.getSelectedItem().equals("Select")) {
comboBox_1.setEnabled(false);
} else if (comboBox.getSelectedItem().equals("Pizza")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < b.length; i++) {
comboBox_1.addItem(b[i]);
}
} else if (comboBox.getSelectedItem().equals("Burger")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < c.length; i++) {
comboBox_1.removeItem(c[i]);
comboBox_1.addItem(c[i]);
}
} else if (comboBox.getSelectedItem().equals("Salad")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < d.length; i++) {
comboBox_1.addItem(d[i]);
}
}else if (comboBox.getSelectedItem().equals("Soft Drink")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < f.length; i++) {
comboBox_1.addItem(f[i]);
}
}
}
}
}
Write the itemListener with a captial letter like this
public class Myapp extends JApplet implements ItemListener{
and import all this packages:
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
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() );