Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: How do I put action with my auto-gen UI?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I put action with my auto-gen UI?

    Can anyone help put action in my code. I need cant really understand about the action listener or event listener. I dont even know where or what to put in it. I already tried reading some tutorial but it didnt help me at all. Can anyone help me or just give advice with my problem?

    ci788.png

    Here is my the auto generated code of that from the JFormDesigner:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.GroupLayout;
    /*
     * Created by JFormDesigner on Fri Jan 27 12:02:45 CST 2012
     */
     
     
     
    /**
     * @author 
     */
    public class test extends JFrame {
    	public test() {
    		initComponents();
    	}
     
    	private void initComponents() {
    		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
    		// Generated using JFormDesigner Evaluation license - 
    		label1 = new JLabel();
    		label2 = new JLabel();
    		textField1 = new JTextField();
    		textField2 = new JTextField();
    		button1 = new JButton();
    		button2 = new JButton();
    		button3 = new JButton();
    		button4 = new JButton();
    		button5 = new JButton();
    		textField3 = new JTextField();
    		textField4 = new JTextField();
     
    		//======== this ========
    		setFont(new Font("Dialog", Font.PLAIN, 14));
    		setTitle("Simple Calculator");
    		Container contentPane = getContentPane();
     
    		//---- label1 ----
    		label1.setText("INPUT1");
    		label1.setFont(new Font("Tahoma", Font.PLAIN, 14));
     
    		//---- label2 ----
    		label2.setText("INPUT2");
    		label2.setFont(new Font("Tahoma", Font.PLAIN, 14));
     
    		//---- button1 ----
    		button1.setText("+");
     
    		//---- button2 ----
    		button2.setText("-");
     
    		//---- button3 ----
    		button3.setText("*");
     
    		//---- button4 ----
    		button4.setText("/");
     
    		//---- button5 ----
    		button5.setText("=");
     
    		GroupLayout contentPaneLayout = new GroupLayout(contentPane);
    		contentPane.setLayout(contentPaneLayout);
    		contentPaneLayout.setHorizontalGroup(
    			contentPaneLayout.createParallelGroup()
    				.addGroup(contentPaneLayout.createSequentialGroup()
    					.addGap(46, 46, 46)
    					.addGroup(contentPaneLayout.createParallelGroup()
    						.addGroup(contentPaneLayout.createSequentialGroup()
    							.addComponent(button5)
    							.addGap(18, 18, 18)
    							.addComponent(textField3, GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))
    						.addGroup(contentPaneLayout.createSequentialGroup()
    							.addGroup(contentPaneLayout.createParallelGroup()
    								.addComponent(textField1, GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
    								.addComponent(label1))
    							.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
    								.addGroup(contentPaneLayout.createSequentialGroup()
    									.addGap(62, 62, 62)
    									.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
    										.addComponent(button3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    										.addComponent(button1, GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
    									.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
    									.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
    										.addComponent(button4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    										.addComponent(button2, GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE))
    									.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE))
    								.addGroup(contentPaneLayout.createSequentialGroup()
    									.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    									.addComponent(textField4, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
    									.addGap(87, 87, 87)))
    							.addGroup(contentPaneLayout.createParallelGroup()
    								.addComponent(textField2, GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
    								.addComponent(label2, GroupLayout.Alignment.TRAILING))))
    					.addGap(54, 54, 54))
    		);
    		contentPaneLayout.setVerticalGroup(
    			contentPaneLayout.createParallelGroup()
    				.addGroup(contentPaneLayout.createSequentialGroup()
    					.addGap(20, 20, 20)
    					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
    						.addComponent(label1)
    						.addComponent(label2)
    						.addComponent(button1)
    						.addComponent(button2, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
    					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    					.addGroup(contentPaneLayout.createParallelGroup()
    						.addGroup(contentPaneLayout.createSequentialGroup()
    							.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
    								.addComponent(button4, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    								.addComponent(button3, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    							.addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    						.addComponent(textField1, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
    						.addComponent(textField2, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE))
    					.addGap(22, 22, 22)
    					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
    						.addComponent(button5)
    						.addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    					.addContainerGap(56, Short.MAX_VALUE))
    		);
    		pack();
    		setLocationRelativeTo(getOwner());
    		// JFormDesigner - End of component initialization  //GEN-END:initComponents
    	}
     
    	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    	// Generated using JFormDesigner Evaluation license -
    	private JLabel label1;
    	private JLabel label2;
    	private JTextField textField1;
    	private JTextField textField2;
    	private JButton button1;
    	private JButton button2;
    	private JButton button3;
    	private JButton button4;
    	private JButton button5;
    	private JTextField textField3;
    	private JTextField textField4;
    	// JFormDesigner - End of variables declaration  //GEN-END:variables
    }

    Here is the code of how it will work:
    import java.util.Scanner;
     
     
    public class Calculator {
     
    	public static void main(String [] args){
    		 int x = 0;
    		 int y = 0;
    		 int control = 0;
    		 int ans = 0;
     
    		Scanner in = new Scanner(System.in);  
    		System.out.println("Input first number:");
    		x = in.nextInt();
    		System.out.print(x);
    		System.out.println("\nInput second number:");
    		y = in.nextInt();
    		System.out.print(y);
    		System.out.print("\n\nNow choose operation: \n1.Add\n2.Subtract\n3.Multiply\n4.Divide:\n\n");
    		control = in.nextInt();
     
     
    		switch (control){	
    			case 1:	ans = x + y;
    					break;			
    			case 2:	ans = x - y;
    					break;
    			case 3:	ans = x * y;
    					break;
    			case 4:	ans = x / y;
    					break;
    			default:System.out.print("ERROR");
    	        		break;
    		}
    		System.out.print("\nThe answer is:" + ans);
    	}
     
    	}

    Basically its just a simple calculator that adds, subtract, multiply, divide.

    I just dont know how to incorporate actions with my code with the GUI. I am not really asking for a finish code, I just want some little help here.


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I put action with my auto-gen UI?

    i realized that in JFormDesigner you can actually add ActionListener... now my new problem is I am not that knowledgeable what usually is inside that. So I would like to ask if what is the appropriate code for this one:

    private void textField1ActionPerformed(ActionEvent e) {
    		// TODO add your code here
    	}

    so in the text field 1 I want the value written there to be the one as int x. How do I do that? This is my speculation which is wrong: (Hope someone can correct me):

    private void textField1ActionPerformed(ActionEvent e) {
    		x = in.nextInt();
    	}

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I put action with my auto-gen UI?

    in the text field 1 I want the value written there to be the one as int x.
    Please explain what you mean.
    Take a look at the tutorial:
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    Your code is calling a nextInt() method. What class is that in? If you are attempting to read from the console in a GUI program, that is not a good design. You shouldn't be mixing console input and GUI.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I put action with my auto-gen UI?

    Quote Originally Posted by Norm View Post
    Please explain what you mean.
    Take a look at the tutorial:
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    Your code is calling a nextInt() method. What class is that in? If you are attempting to read from the console in a GUI program, that is not a good design. You shouldn't be mixing console input and GUI.
    Yup I know that it is wrong. I read that tutorial already but thanks for the link.

    I still need help well my code does receive the input x and y but does not produce the answer. Can anyone tell me what is wrong in the private void button5ActionPerformed(ActionEvent e)

    public class Calculator extends JFrame {
    	int x ;
    	int y ;
    	int ans;
    	public static void main(String[] args){
    		Calculator calc = new Calculator();
    		calc.setVisible(true);
    	}
    	private static final long serialVersionUID = -1113582265865921787L;
    	public Calculator() {
    		initComponents();
    	}
     
    	private void textField1ActionPerformed(ActionEvent e) {
    		x = Integer.parseInt(textField1.getText());
    	}
     
    	private void textField2ActionPerformed(ActionEvent e) {
    		y = Integer.parseInt(textField2.getText());
    	}
     
    	private void button1ActionPerformed(ActionEvent e) {
    		textField4.setText("+");
    	}
     
    	private void button2ActionPerformed(ActionEvent e) {
    		textField4.setText("-");
    	}
     
    	private void button3ActionPerformed(ActionEvent e) {
    		textField4.setText("*");
    	}
     
    	private void button4ActionPerformed(ActionEvent e) {
    		textField4.setText("/");
    	}
     
    	private void button5ActionPerformed(ActionEvent e) {
    		if (textField4.equals("+"))
    			ans = x + y;
    		else if (textField4.equals("-"))
    			ans = x - y;
    		else if (textField4.equals("*"))
    			ans = x * y;
    		else if (textField4.equals("/"))
    			ans = x / y;
    		else
    			textField3.setText("Input Value in field 1 and 2");
     
    		textField3.setText(Integer.toString(ans));
    	}
    Last edited by thisbeme; January 29th, 2012 at 03:08 AM.

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: How do I put action with my auto-gen UI?

    it seems like a JFormDesigner is kind of beans that generates a code and a ready to use application for a drag-and-drop swing components? have you ever coded a GUI by your hand before? well if you DONT man, you will definitely have trouble digging into these bunch of codes. Based on your question your problem is adding an action to a component, and it seems like you want to put Actions in your buttons? if you want to put an action, you really have to start at the link Norm has posted.
    Last edited by chronoz13; January 29th, 2012 at 06:44 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I put action with my auto-gen UI?

    what is wrong i
    Please explain why you think something is wrong. Do you get errors? Please copy and post the full text of the message here.
    If the code executes, explain what it does do or does not do and what the problem is.

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I put action with my auto-gen UI?

    Quote Originally Posted by chronoz13 View Post
    it seems like a JFormDesigner is kind of beans that generates a code and a ready to use application for a drag-and-drop swing components? have you ever coded a GUI by your hand before? well if you DONT man, you will definitely have trouble digging into these bunch of codes. Based on your question your problem is adding an action to a component, and it seems like you want to put Actions in your buttons? if you want to put an action, you really have to start at the link Norm has posted.
    Yeah thats what JFormDesigner does.
    No I havent coded a GUI by myself before.

    I already read that link and a few books about Action Events. Plus theres already actions in my button and its already working. I am only asking what is wrong with my other button since it is not functioning how it is supposed to be.

  8. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I put action with my auto-gen UI?

    Quote Originally Posted by Norm View Post
    Please explain why you think something is wrong. Do you get errors? Please copy and post the full text of the message here.
    If the code executes, explain what it does do or does not do and what the problem is.
    My bad. I forgot to put what happens. Anyways thanks for the reply

    Well my code does run and it does not give any error message.
    When I run it, the other components are working, except for the private void button5ActionPerformed(ActionEvent e) (which i posted the code above post). It does not give error message when I run it but when I click the button it should give me the answer of my two inputs but it only gives out the value 0.

    Example:

    xlksiq.jpg

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I put action with my auto-gen UI?

    When you click the button, what code is executed? Add some printlns to show where the execution flow goes.
    Is the method you want to be executed called? If nothing prints out then your printlns are not being executed.
    What code calls the button5ActionPerformed method?
    Where do you add an actionListener to the button?

  10. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I put action with my auto-gen UI?

    Quote Originally Posted by Norm View Post
    When you click the button, what code is executed? Add some printlns to show where the execution flow goes.
    Is the method you want to be executed called? If nothing prints out then your printlns are not being executed.
    What code calls the button5ActionPerformed method?
    Where do you add an actionListener to the button?
    When I click the button with equals the button5ActionPerformed method is executed.
    I dont have any printlns in my code. I added the action Listener below the action perform methods.

    here is my code by the way (please ignore my first post code above):
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.GroupLayout;
    /*
     * Created by JFormDesigner on Fri Jan 27 12:02:45 CST 2012
     */
     
     
     
    /**
     * @author 
     */
    public class Calculator extends JFrame {
    	int x ;
    	int y ;
    	int ans;
    	public static void main(String[] args){
    		Calculator calc = new Calculator();
    		calc.setVisible(true);
    	}
    	private static final long serialVersionUID = -1113582265865921787L;
    	public Calculator() {
     
    		initComponents();
    	}
     
    	private void textField1ActionPerformed(ActionEvent e) {
    		x = Integer.parseInt(textField1.getText());
    	}
     
    	private void textField2ActionPerformed(ActionEvent e) {
    		y = Integer.parseInt(textField2.getText());
    	}
     
    	private void button1ActionPerformed(ActionEvent e) {
    		textField4.setText("+");
    	}
     
    	private void button2ActionPerformed(ActionEvent e) {
    		textField4.setText("-");
    	}
     
    	private void button3ActionPerformed(ActionEvent e) {
    		textField4.setText("*");
    	}
     
    	private void button4ActionPerformed(ActionEvent e) {
    		textField4.setText("/");
    	}
     
    	private void button5ActionPerformed(ActionEvent e) {
     
    		if (textField4.equals("+")){
    			ans = x + y;
    			textField3.setText(Integer.toString(ans));}
    		else if (textField4.equals("-")){
    			ans = x - y;
    			textField3.setText(Integer.toString(ans));}
    		else if (textField4.equals("*")){
    			ans = x * y;
    			textField3.setText(Integer.toString(ans));}
    		else if (textField4.equals("/")){
    			ans = x / y;
    			textField3.setText(Integer.toString(ans));}
    		else
    			textField3.setText("Input Value in field 1 and 2");
     
     
    	}
     
    	private void textField4ActionPerformed(ActionEvent e) {
     
    	}
     
    	private void textField3ActionPerformed(ActionEvent e) {
     
     
    	}
     
     
    	private void initComponents() {
    		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
    		// Generated using JFormDesigner Evaluation license - 
    		label1 = new JLabel();
    		label2 = new JLabel();
    		textField1 = new JTextField();
    		textField2 = new JTextField();
    		button1 = new JButton();
    		button2 = new JButton();
    		button3 = new JButton();
    		button4 = new JButton();
    		button5 = new JButton();
    		textField3 = new JTextField();
    		textField4 = new JTextField();
     
    		//======== this ========
    		setFont(new Font("Dialog", Font.PLAIN, 14));
    		setTitle("Simple Calculator");
    		Container contentPane = getContentPane();
     
    		//---- label1 ----
    		label1.setText("INPUT1");
    		label1.setFont(new Font("Tahoma", Font.PLAIN, 14));
     
    		//---- label2 ----
    		label2.setText("INPUT2");
    		label2.setFont(new Font("Tahoma", Font.PLAIN, 14));
     
    		//---- textField1 ----
    		textField1.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				textField1ActionPerformed(e);
    				textField1ActionPerformed(e);
    				textField1ActionPerformed(e);
    			}
    		});
     
    		//---- textField2 ----
    		textField2.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				textField2ActionPerformed(e);
    			}
    		});
     
    		//---- button1 ----
    		button1.setText("+");
    		button1.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				button1ActionPerformed(e);
    			}
    		});
     
    		//---- button2 ----
    		button2.setText("-");
    		button2.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				button2ActionPerformed(e);
    			}
    		});
     
    		//---- button3 ----
    		button3.setText("*");
    		button3.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				button3ActionPerformed(e);
    			}
    		});
     
    		//---- button4 ----
    		button4.setText("/");
    		button4.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				button4ActionPerformed(e);
    			}
    		});
     
    		//---- button5 ----
    		button5.setText("=");
    		button5.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				button5ActionPerformed(e);
    			}
    		});
     
    		//---- textField3 ----
    		textField3.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				textField3ActionPerformed(e);
    			}
    		});
     
    		//---- textField4 ----
    		textField4.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				textField4ActionPerformed(e);
    			}
    		});
     
    		GroupLayout contentPaneLayout = new GroupLayout(contentPane);
    		contentPane.setLayout(contentPaneLayout);
    		contentPaneLayout.setHorizontalGroup(
    			contentPaneLayout.createParallelGroup()
    				.addGroup(contentPaneLayout.createSequentialGroup()
    					.addGap(46, 46, 46)
    					.addGroup(contentPaneLayout.createParallelGroup()
    						.addGroup(contentPaneLayout.createSequentialGroup()
    							.addComponent(button5)
    							.addGap(18, 18, 18)
    							.addComponent(textField3, GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))
    						.addGroup(contentPaneLayout.createSequentialGroup()
    							.addGroup(contentPaneLayout.createParallelGroup()
    								.addComponent(textField1, GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
    								.addComponent(label1))
    							.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
    								.addGroup(contentPaneLayout.createSequentialGroup()
    									.addGap(62, 62, 62)
    									.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
    										.addComponent(button3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    										.addComponent(button1, GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
    									.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
    									.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
    										.addComponent(button4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    										.addComponent(button2, GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE))
    									.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE))
    								.addGroup(contentPaneLayout.createSequentialGroup()
    									.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    									.addComponent(textField4, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
    									.addGap(87, 87, 87)))
    							.addGroup(contentPaneLayout.createParallelGroup()
    								.addComponent(textField2, GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
    								.addComponent(label2, GroupLayout.Alignment.TRAILING))))
    					.addGap(54, 54, 54))
    		);
    		contentPaneLayout.setVerticalGroup(
    			contentPaneLayout.createParallelGroup()
    				.addGroup(contentPaneLayout.createSequentialGroup()
    					.addGap(20, 20, 20)
    					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
    						.addComponent(label1)
    						.addComponent(label2)
    						.addComponent(button1)
    						.addComponent(button2, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
    					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    					.addGroup(contentPaneLayout.createParallelGroup()
    						.addGroup(contentPaneLayout.createSequentialGroup()
    							.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
    								.addComponent(button4, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    								.addComponent(button3, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    							.addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    						.addComponent(textField1, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
    						.addComponent(textField2, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE))
    					.addGap(22, 22, 22)
    					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
    						.addComponent(button5)
    						.addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    					.addContainerGap(56, Short.MAX_VALUE))
    		);
    		pack();
    		setLocationRelativeTo(getOwner());
    		// JFormDesigner - End of component initialization  //GEN-END:initComponents
    	}
     
    	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    	// Generated using JFormDesigner Evaluation license - 
    	private JLabel label1;
    	private JLabel label2;
    	private JTextField textField1;
    	private JTextField textField2;
    	private JButton button1;
    	private JButton button2;
    	private JButton button3;
    	private JButton button4;
    	private JButton button5;
    	private JTextField textField3;
    	private JTextField textField4;
    	// JFormDesigner - End of variables declaration  //GEN-END:variables
    }

    the above code is working. you can try running it but the problem is that it still just output the answer 0. even if you put 13 + 9... it will still give 0. O is the only output.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I put action with my auto-gen UI?

    the button5ActionPerformed method is executed.
    I dont have any printlns in my code.
    You need to Add some printlns to the code to show what that method is doing. Print out a message when the method is entered and print out a message when any of the if statements are true and print out a message when any variable's value is changed.

  12. #12
    Junior Member
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I put action with my auto-gen UI?

    Quote Originally Posted by Norm View Post
    You need to Add some printlns to the code to show what that method is doing. Print out a message when the method is entered and print out a message when any of the if statements are true and print out a message when any variable's value is changed.
    what if i used setText? isnt that the same with printlns?

    like in the answer field i did this:

    textField3.setText(Integer.toString(ans))

    isnt this already printing out the message?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I put action with my auto-gen UI?

    isnt this already printing out the message?
    Are you able to see what the methods are doing that way?
    Does it show that the method is executed?
    Does it show which if test is true?
    Does it show the values of x and y?
    Does it show the contents of textField4?

    If you have the answers to the above questions, then you know why:
    it will still give 0.

  14. #14
    Junior Member
    Join Date
    Jan 2012
    Location
    Estonia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I put action with my auto-gen UI?

    This should work.
    import javax.swing.*;
     
    public class Calculator {
     
    	public static void main(String [] args){
     
    		//Shows window
    		test window = new test();
    		window.setVisible(true);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    	}
    }

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    /*
     * Created by JFormDesigner on Fri Jan 27 12:02:45 CST 2012
     */
     
    /**
     * @author 
     */
    public class test extends JFrame {
     
    	//Creates variables for calculating, z = x op y
    	double x, y, z;
    	int op;
     
    	//Creates interface components
    	private JLabel label1 = new JLabel();
    	private JLabel label2 = new JLabel();
    	private JTextField textField1 = new JTextField();
    	private JTextField textField2 = new JTextField();
    	private JButton button1 = new JButton();
    	private JButton button2 = new JButton();
    	private JButton button3 = new JButton();
    	private JButton button4 = new JButton();
    	private JButton button5 = new JButton();
    	private JTextField textField3 = new JTextField();
    	private JTextField textField4 = new JTextField();
     
    	public test() {
    		//Sets layout
    		initComponents();
     
    		//Sets handler to required gui component
    		actionHandler Handler = new actionHandler();
    		button1.addActionListener(Handler);
    		button2.addActionListener(Handler);
    		button3.addActionListener(Handler);
    		button4.addActionListener(Handler);
    		button5.addActionListener(Handler);
    		textField1.addActionListener(Handler);
    		textField2.addActionListener(Handler);
    		textField3.addActionListener(Handler);
    	}
     
    	//Handler class
    	private class actionHandler implements ActionListener {
     
    		public void actionPerformed(ActionEvent event) {
     
    			//When +, -, *, / is pressed
    			if(event.getSource()==button1) { //When button is pressed
    				textField4.setText("+"); //Shows on textField4 which operation you pressed
    				op = 1; //Sets operation
    			}
    			if(event.getSource()==button2) {
    				textField4.setText("-");
    				op = 2;
    			}  
    			if(event.getSource()==button3) {
    				textField4.setText("*");
    				op = 3;
    			} 
    			if(event.getSource()==button4) {
    				textField4.setText("/");
    				op = 4;
    			} 
     
     
    			if(event.getSource()==button5) { //when = is pressed
    				if(textField1.getText() != null && textField2.getText() != null && textField4.getText() != null) //Makes sure no textbox is empty
    					setData(//Sends x, y and op values to setData
    							Double.parseDouble(textField1.getText()), //x
    							Double.parseDouble(textField2.getText()), //y
    							op 
    							);
    					textField3.setText(Double.toString(z)); //Displays final result
     
    				}
     
     
     
    		}
    	}
     
    	void setData(double x, double y, int op) { //Calculating part
    		switch (op) {
    		case 1:
    			z = x + y;
    			break;
    		case 2:
    			z = x - y;
    			break;
    		case 3:
    			z = x * y;
    			break;
    		case 4:
    			z = x / y;
    			break;
    		}		
    	}
     
    	private void initComponents() {
    		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
    		// Generated using JFormDesigner Evaluation license - 
     
    		//======== this ========
    		setFont(new Font("Dialog", Font.PLAIN, 14));
    		setTitle("Simple Calculator");
    		Container contentPane = getContentPane();
     
    		//---- label1 ----
    		label1.setText("INPUT1");
    		label1.setFont(new Font("Tahoma", Font.PLAIN, 14));
     
    		//---- label2 ----
    		label2.setText("INPUT2");
    		label2.setFont(new Font("Tahoma", Font.PLAIN, 14));
     
    		//---- button1 ----
    		button1.setText("+");
     
    		//---- button2 ----
    		button2.setText("-");
     
    		//---- button3 ----
    		button3.setText("*");
     
    		//---- button4 ----
    		button4.setText("/");
     
    		//---- button5 ----
    		button5.setText("=");
     
    		GroupLayout contentPaneLayout = new GroupLayout(contentPane);
    		contentPane.setLayout(contentPaneLayout);
    		contentPaneLayout.setHorizontalGroup(
    			contentPaneLayout.createParallelGroup()
    				.addGroup(contentPaneLayout.createSequentialGroup()
    					.addGap(46, 46, 46)
    					.addGroup(contentPaneLayout.createParallelGroup()
    						.addGroup(contentPaneLayout.createSequentialGroup()
    							.addComponent(button5)
    							.addGap(18, 18, 18)
    							.addComponent(textField3, GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))
    						.addGroup(contentPaneLayout.createSequentialGroup()
    							.addGroup(contentPaneLayout.createParallelGroup()
    								.addComponent(textField1, GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
    								.addComponent(label1))
    							.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
    								.addGroup(contentPaneLayout.createSequentialGroup()
    									.addGap(62, 62, 62)
    									.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
    										.addComponent(button3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    										.addComponent(button1, GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
    									.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
    									.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
    										.addComponent(button4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    										.addComponent(button2, GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE))
    									.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE))
    								.addGroup(contentPaneLayout.createSequentialGroup()
    									.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    									.addComponent(textField4, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
    									.addGap(87, 87, 87)))
    							.addGroup(contentPaneLayout.createParallelGroup()
    								.addComponent(textField2, GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
    								.addComponent(label2, GroupLayout.Alignment.TRAILING))))
    					.addGap(54, 54, 54))
    		);
    		contentPaneLayout.setVerticalGroup(
    			contentPaneLayout.createParallelGroup()
    				.addGroup(contentPaneLayout.createSequentialGroup()
    					.addGap(20, 20, 20)
    					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
    						.addComponent(label1)
    						.addComponent(label2)
    						.addComponent(button1)
    						.addComponent(button2, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
    					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    					.addGroup(contentPaneLayout.createParallelGroup()
    						.addGroup(contentPaneLayout.createSequentialGroup()
    							.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
    								.addComponent(button4, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    								.addComponent(button3, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    							.addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    						.addComponent(textField1, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
    						.addComponent(textField2, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE))
    					.addGap(22, 22, 22)
    					.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
    						.addComponent(button5)
    						.addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    					.addContainerGap(56, Short.MAX_VALUE))
    		);
    		pack();
    		setLocationRelativeTo(getOwner());
    		// JFormDesigner - End of component initialization  //GEN-END:initComponents
    	}
     
    	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    	// Generated using JFormDesigner Evaluation license -
     
    	// JFormDesigner - End of variables declaration  //GEN-END:variables
     
    }
    Last edited by kekku500; February 1st, 2012 at 09:33 AM.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I put action with my auto-gen UI?

    @kekku500
    Please don't spoonfeed.
    You should help the OP understand what is wrong with the code and how he needs to fix it.
    Posting code doesn't help the OP learn how to program.
    Copy and paste doesn't provide what the OP needs to do to learn programming.

  16. #16
    Junior Member
    Join Date
    Jan 2012
    Location
    Estonia
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I put action with my auto-gen UI?

    @Norm
    If he truly wants to learn, then he will look what i wrote into the code and try to understand it himself. I just thought that he could learn programming by looking at the finished code. That's how i have been learning to program so far and i believe it works just fine.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I put action with my auto-gen UI?

    Some do and some don't.
    Some copy and paste and are done.

Similar Threads

  1. program auto 'typing'
    By frozen java in forum Java Theory & Questions
    Replies: 3
    Last Post: October 22nd, 2011, 06:11 PM
  2. Auto Select
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 31st, 2011, 04:55 AM
  3. auto-increment of primary key
    By hundu in forum Enterprise JavaBeans
    Replies: 1
    Last Post: May 15th, 2010, 10:55 AM
  4. Auto Fill the TextField
    By malladiG in forum Java Theory & Questions
    Replies: 0
    Last Post: April 6th, 2010, 07:32 AM
  5. Auto contrast and auto brightness
    By oxxxis in forum Java Theory & Questions
    Replies: 0
    Last Post: January 21st, 2010, 03:00 PM