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 15 of 15

Thread: Updating The Components

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Updating The Components

    Hi,

    Here is my problem: I wanted to make a GUI for a code that solves Hailstone Sequence (The Hailstone Sequence).
    It is an easy one, so I could solve it but I was printing to the console.
    I prepared the window using Windowpro. Here is my code (There are two classes because Hail class is basicly the copy of my console app):

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTextPane;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
     
    public class mainWindow extends JFrame {
     
    	private JPanel contentPane;
    	private JTextField Number;
    	private JTextField Count;
    	public JTextPane textPane;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					mainWindow frame = new mainWindow();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the frame.
    	 */
    	public mainWindow() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
     
    		JLabel lblNumber = new JLabel("Number");
    		lblNumber.setBounds(10, 11, 46, 14);
    		contentPane.add(lblNumber);
     
    		Number = new JTextField();
    		Number.setBounds(66, 8, 86, 20);
    		contentPane.add(Number);
    		Number.setColumns(10);
     
    		JLabel lblCount = new JLabel("Count");
    		lblCount.setBounds(10, 44, 46, 14);
    		contentPane.add(lblCount);
     
    		Count = new JTextField();
    		Count.setBounds(66, 41, 86, 20);
    		contentPane.add(Count);
    		Count.setColumns(10);
     
    		JButton btnStart = new JButton("START");
    		btnStart.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String s1 = Number.getText();
    				int num = Integer.parseInt(s1);
    				String s2 = Count.getText();
    				int count = Integer.parseInt(s2);
    				Hail h = new Hail();
    				h.test(num, count);
     
    			}
    		});
    		btnStart.setBounds(162, 7, 89, 23);
    		contentPane.add(btnStart);
     
    		textPane = new JTextPane();
    		textPane.setEditable(false);
    		textPane.setBounds(10, 69, 414, 181);
    		contentPane.add(textPane);
    	}
    public void setYazi(String s){
    	textPane.setText(s);
    	repaint();
    }
    }

    This is the hail class, which does the job:
    import java.util.Scanner;
     
     
    public class Hail {
     
    	public void test(int c, int d){
    		int sayac=0;
     
     
    		System.out.println("Number "+c);
    		System.out.println("How many repetitions?");
     
     
     
    			System.out.println("Will do.");
     
    		System.out.println("Started.");
    		while(c!=1){
    			if(c%2==0){
    				c = c/2;
    				System.out.println("Number/2: "+c);
    			}else{
    				c = (3*c)+1;
    				System.out.println("Number*3+1:"+c);
    			}
    			sayac++;
    			if(sayac==d){
    				break;
    			}
    		}
    		mainWindow m = new mainWindow();
    		if(c==1){			
    			m.setYazi("Suitable. Count: "+sayac+");
    		System.out.println("Suitable. Count: "+sayac+");
     
    		}else{			
    			System.out.println("Not suitable or not enough repetition");
    			m.setYazi("Not suitable or not enough repetition");
     
    		}
    		}
     
    	}

    I am gonna be honest. If I return a String from Hail class, I can update the textPane. But I want to print out all of the process to the textPane. That is why I cannot use it.

    System.out.println codes are there to test it out. What I want is, to pass everything I am writing to console to textPane.

    What am I doing wrong?

    Possible reason might be creating an instance of mainWindow from inside of Hail, but how else could I do that?


  2. #2
    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: Updating The Components

    How many MainWindow objects are created by the program? Add a println statement in the constructor that prints a message so you will know.

    How many should it create?

    What if one is visible and the others are not?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Updating The Components

    Thanks Norm. It does create another window instead of just updating the textPane in the existing one. And when I set the new window (presented by "m" in Hail class), it shows another window with the updated textPane.

    However, this makes my app faulty. I don't want to create another window. How else can I update the textPane to show the info?

  4. #4
    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: Updating The Components

    I don't want to create another window.
    You need to make a reference to the existing, visible window available to the code that wants to use it.
    One way would be to pass the reference in the call to the Hail class's constructor and have the constructor save the reference in a class variable.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Updating The Components

    Norm I am sorry but I don't understand you. I thought I did, but I sat in front of my code to make a change but it turns out, I did not understand you

  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: Updating The Components

    Where is there a reference to the existing instance of the MainWindow class? If the code in the MainWindow class is calling the constructor, use the this variable. this refers to the class where the code is executing.
    Pass that in the Hail class's constructor. Have the constructor save the arg that is passed to it in a class variable.

    Which part of this don't you understand?
    Pass a variable in a constructor:
     ...   = new TheClass(theVarToPass);
    Save value in constructor:
    class TheClass {
      ThePassedArg theArg;  // save arg here
      public TheClass(ThePassedArg theArg) {  //  the constructor with arg
        this.theArg = theArg;  // save arg in class variable
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Updating The Components

    I still can't get my head around it.

    Actually I am not sure anymore if I don't understand you or I don't know how to implement your idea.

    What I am doing here is that, the mainWindow class basicly constructs the window. And there is one button. This button runs hail class. The hail class runs the procedure with the given number value for the given count times. Then it gives the result to the console and tells me if the number fits with the Hailstone sequence.

    Now, if the method in the hail class returns a String, it is easy to write it to textPane. But I don't want this, because I want to write other things to the textPane, too. That is, if you look at the code, it says "Starting", "Number/2" or things like that. These should be put to textPane too.

    About your suggestion, believe me I tried. But I couldn't find a way to write it WITHOUT creating a new mainWindow object. The mainWindow is the first running part of the program, I don't know how to change the values there. And to be honest, I could not implement what I think you are saying. If I don't make a new mainWindow object, my setYazi method is useless too. So basicly, I don't actually have a method to write down to textPane.

    I still need help. Is there something horribly wrong with my code?

  8. #8
    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: Updating The Components

    y to write it WITHOUT creating a new mainWindow object.
    Use the reference to the MainWindow object that is passed to the constructor.
    m.setYazi("Suitable. Count: "+sayac+");
    In the above, m should be the reference that was passed to the Hail class.
    In my code example, the reference was: theArg
    theArg.setYazi("Suitable. Count: "+sayac+");

    Do NOT create a new instance of MainWindow to get the needed reference. Pass the reference into the constructor as shown above.

    About your suggestion, believe me I tried.
    Post the code where you tried my suggestion.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    beer-in-box (December 16th, 2012)

  10. #9
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Updating The Components

    I got it. However, I am not sure if this is what you told me to do. But I passed an JTextPane to hail class' test method.

    I am sharing my final code here, and it is cleaned from the System.out.println lines.

    If this is not what you meant, please you share your version when you have time and let me learn another way of doing this.

    OK, this is my mainWindow class, I didn't change much here:
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTextPane;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
     
    public class mainWindow extends JFrame {
     
    	private JPanel contentPane;
    	private JTextField Number;
    	private JTextField Count;
    	public JTextPane textPane;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					mainWindow frame = new mainWindow();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the frame.
    	 */
    	public mainWindow() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
     
    		JLabel lblNumber = new JLabel("Number");
    		lblNumber.setBounds(10, 11, 46, 14);
    		contentPane.add(lblNumber);
     
    		Number = new JTextField();
    		Number.setBounds(66, 8, 86, 20);
    		contentPane.add(Number);
    		Number.setColumns(10);
     
    		JLabel lblCount = new JLabel("Count");
    		lblCount.setBounds(10, 44, 46, 14);
    		contentPane.add(lblCount);
     
    		Count = new JTextField();
    		Count.setBounds(66, 41, 86, 20);
    		contentPane.add(Count);
    		Count.setColumns(10);
     
    		textPane = new JTextPane();
    		textPane.setEditable(false);
    		textPane.setBounds(10, 69, 414, 181);
    		contentPane.add(textPane);
     
    		JButton btnStart = new JButton("START");
    		btnStart.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String s1 = Number.getText();
    				int num = Integer.parseInt(s1);
    				String s2 = Count.getText();
    				int count = Integer.parseInt(s2);
    				Hail h = new Hail();
    				String result = textPane.getText();
    				h.test(num, count, textPane);
    				repaint();
     
    			}
    		});
    		btnStart.setBounds(162, 7, 89, 23);
    		contentPane.add(btnStart);
     
     
    	}
    }

    And this is the Hail class. It passes the String variable resulter to the textPane component of mainWindow:
    import javax.swing.JTextPane;
     
     
    public class Hail {
    	String resulter = "";
     
    	public void test(int c, int d, JTextPane tp){
    		int sayac=0;
     
    		while(c!=1){
    			if(c%2==0){
    				c = c/2;
    				resulter = resulter + "Number is even. So I divide it by two. Result: "+c+"\n";
    				tp.setText(resulter);
    			}else{
    				c = (3*c)+1;
    				resulter = resulter + "Number is odd. So I multiply it and add 1. Result: "+c+"\n";
    				tp.setText(resulter);
    			}
    			sayac++;
    			if(sayac==d){
    				break;
    			}
    		}
     
    		if(c==1){			
     
    			resulter = resulter + "Suitable. Count: "+sayac+"\n";
    			tp.setText(resulter);
     
    		}else{			
     
    			resulter = resulter + "Not suitable or not enough repetition"+"\n";
    			tp.setText(resulter);
     
    		}
    		}
     
    	}

  11. #10
    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: Updating The Components

    That's another way to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Updating The Components

    Yes but can you give a short example of your way or just implement it with my code? I'm very interested in learning your way.

  13. #12
    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: Updating The Components

    See post #6 for a short piece of code that passes a reference to a constructor and has the constructor save the reference in a class variable.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Updating The Components

    Thanks. So, just to be clear on this, considering my code;

    -You say you would pass a variable of the mainWindow class to the constructor of Hail class, right? I did not use a constructor. Maybe that is why I could not understand you in the first place?

    -I pass a component to the method of Hail class. But I could also pass a String to store the information, then I would just write it to the JTextPane component within the mainWindows class. Is that right?

  15. #14
    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: Updating The Components

    As you have discovered there is more than one way to solve the problem:
    Pass a reference in:
    the constructor where it is saved for later use
    the method call where it is used
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    beer-in-box (December 16th, 2012)

  17. #15
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Updating The Components

    Thanks again

Similar Threads

  1. Components?
    By Gravity Games in forum Java Theory & Questions
    Replies: 31
    Last Post: August 5th, 2012, 04:20 PM
  2. [SOLVED] Need help with updating label
    By Neacel in forum AWT / Java Swing
    Replies: 5
    Last Post: January 22nd, 2012, 02:38 PM
  3. Updating JList
    By KILL3RTACO in forum Java Theory & Questions
    Replies: 3
    Last Post: October 6th, 2011, 07:17 AM
  4. Updating timer
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 26th, 2011, 07:02 AM
  5. updating database
    By gurpreetm13 in forum JDBC & Databases
    Replies: 3
    Last Post: October 9th, 2009, 11:43 AM