Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Refreshing a value

  1. #1
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Refreshing a value

    Hi!
    I'm having some troubles with "refreshing a value". I'm calling
    Java Code:
    int sum;
    in my class. I open a new JFrame with button, but value of sum is displayed in frist JFrame.
    Java Code:
    earned = new JTextField("" + sum);
    Secod JFrame decides value of sum.
    Java Code:
    private class AddtoArraylistLytter implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			sum += sum;
    		}
    I'm closing second JFrame with
    Java Code:
     ramme2.setDefaultCloseOperation(ramme2.DISPOSE_ON_CLOSE);
    When i'm closing second JFrame, value of sum is still 0. Why?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Refreshing a value

    Without an SSCCE, we can't really help you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Re: Refreshing a value

    in your action listener try to update the text field using update function. try that
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  4. #4
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Re: Refreshing a value

    Java Code:
     package design;
    
    public class SalaryLayout {
    
    	JFrame ramme;
    	JPanel ovrePanel,midtPanel,nedrePanel;
    	JLabel addLabel, calendarLabel, statLabel, summen;
    	JButton add,calendar,statistics;
    	JTextField earned;
    	JComboBox<String> vakt,dag;
    	
    	String[] dagStrings = { "Hverdag", "Lørdag" , "Søndag" };
    	String[] vaktStrings = { "Dagvakt", "Kveldsvakt", "Nattvakt"};
    	
    	int sum;
    	double dagens;
    	
    	
    	public SalaryLayout() {
    
    		ramme = new JFrame("Salary Calculator");
    		ramme.setLayout(new BorderLayout());
    
    
    		ovrePanel = new JPanel();
    		ovrePanel.setLayout(new GridLayout(2,3));
    
    		midtPanel = new JPanel();
    		midtPanel.setLayout(new GridLayout(2,3));
    
    		nedrePanel = new JPanel();
    		nedrePanel.setLayout(new GridLayout(2,5));
    
    		add = new JButton("Add");
    		addJobtime handler = new addJobtime();
    		add.addActionListener(handler);
    		
    		calendar = new JButton("Calendar");
    		statistics = new JButton("Statistics");
    		statistics.setPreferredSize(new Dimension(10,5));
    		addLabel = new JLabel("Add new worktime");
    		calendarLabel = new JLabel("Look at calendar");
    		summen = new JLabel("             Total earned: ");
    		earned = new JTextField("" + sum);
    		
    		ovrePanel.add(addLabel);
    		ovrePanel.add(Box.createRigidArea(new Dimension(5,0)));
    		ovrePanel.add(calendarLabel);
    		ovrePanel.add(add);
    		ovrePanel.add(Box.createRigidArea(new Dimension(5,0)));
    		ovrePanel.add(calendar);
    		midtPanel.add(Box.createRigidArea(new Dimension(3,0)));
    		midtPanel.add(Box.createRigidArea(new Dimension(3,0)));
    		midtPanel.add(Box.createRigidArea(new Dimension(3,0)));
    		midtPanel.add(Box.createRigidArea(new Dimension(3,0)));
    		midtPanel.add(statistics);
    		midtPanel.add(Box.createRigidArea(new Dimension(3,0)));
    		nedrePanel.add(Box.createRigidArea(new Dimension(3,0)));
    		nedrePanel.add(Box.createRigidArea(new Dimension(3,0)));
    		nedrePanel.add(Box.createRigidArea(new Dimension(3,0)));
    		nedrePanel.add(Box.createRigidArea(new Dimension(3,0)));
    		nedrePanel.add(Box.createRigidArea(new Dimension(3,0)));
    		nedrePanel.add(Box.createRigidArea(new Dimension(3,0)));
    		nedrePanel.add(summen);
    		nedrePanel.add(earned);
    
    		ramme.add(ovrePanel, BorderLayout.NORTH);
    		ramme.add(midtPanel,BorderLayout.CENTER);
    		ramme.add(nedrePanel, BorderLayout.SOUTH);
    
    		ramme.pack();
    		ramme.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		ramme.setVisible(true);
    		ramme.setSize(500,250);
    		ramme.setLocation(380,200);
    
    
    
    	}
    	
    
    	private class addJobtime implements ActionListener {
    
    		public void actionPerformed(ActionEvent event) {
    			JButton confirm = new JButton("Confirm");
    			ConfirmLytter confirmLytt = new ConfirmLytter();
    			confirm.addActionListener(confirmLytt);
    			
    			AddtoArraylistLytter addArray = new AddtoArraylistLytter();
    			JButton addToArray = new JButton("Add to arraylist");
    			addToArray.addActionListener(addArray);
    			
    
    			JFrame ramme2 = new JFrame("Add Job-time");
    			ComboBoxDemo box = new ComboBoxDemo();
    			
    			ramme2.setLayout(new GridLayout(2,3));
    			
    			dag = new JComboBox(dagStrings);
    			dag.setSelectedIndex(0);
    			dag.setEditable(true);
    			dag.addActionListener(box);
    			
    			vakt = new JComboBox(vaktStrings);
    			vakt.setSelectedIndex(1);
    			vakt.setEditable(true);
    			vakt.addActionListener(box);
    			
    			ramme2.add(dag);
    			ramme2.add(vakt);
    			ramme2.add(confirm);
    			ramme2.add(addToArray);
    			
    			ramme2.pack();
    			ramme2.setDefaultCloseOperation(ramme2.DISPOSE_ON_CLOSE);
    			ramme2.setVisible(true);
    			ramme2.setLocation(380,200);
    		}
    	}
    	private class ComboBoxDemo implements ActionListener {
    
    		public void actionPerformed(ActionEvent e) {
    
    			
    		}
    	}
    	private class ConfirmLytter implements ActionListener {
    		
    		public void actionPerformed(ActionEvent e) {
    
    			dagens = 199*7; // VALUE
    		}
    		
    		
    	}
    	private class AddtoArraylistLytter implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			sum = (int)dagens + sum;
    			System.out.println("Summen: " + sum);
    			
    		}
    	}
    	
    	
    }

    in your action listener try to update the text field using update function. try that
    What did You mean by that? I can't find any update function (?)

  5. #5
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Re: Refreshing a value

    Can someone please help here? I have some easier code to analize.

    Here I add some buttons and label for year.
    Java Code:
    int yearValg = 2000;
    previous2 = new JButton("<");
    		next2 = new JButton(">");
    		yearChooserPlus plusYear = new yearChooserPlus();
    		yearChooserMinus minusYear = new yearChooserMinus();
    		previous2.addActionListener(minusYear);
    		next2.addActionListener(plusYear);
    
    		aar = new JLabel(" " + yearValg);
    Here's listener:
    Java Code:
    private class yearChooserPlus implements ActionListener {
    		public void actionPerformed(ActionEvent event) {
    			yearValg += 1;
    			System.out.println(yearValg);
    
    		}
    	}
    Value of "yearValg" is changing as it should, but label's value(visual element) is still "2000". I can't understand why? Code says:
    Java Code:
    aar = new JLabel(" " + yearValg)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Refreshing a value

    Value of "yearValg" is changing as it should, but label's value(visual element) is still "2000". I can't understand why? Code says:
    Java Code:
    aar = new JLabel(" " + yearValg)
    That code does not set up a connection between the yearValg variable and what is displayed in the label. Just as you had to have a System.out.println() call to print the new value of yearValg, so you will have to call setText() on the label to have it display the new value of yearValg.
    OPTYMISTA likes this.

  7. #7
    OPTYMISTA is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Re: Refreshing a value

    Quote Originally Posted by pbrockway2 View Post
    That code does not set up a connection between the yearValg variable and what is displayed in the label. Just as you had to have a System.out.println() call to print the new value of yearValg, so you will have to call setText() on the label to have it display the new value of yearValg.
    You're tottaly right! Thanks for help! (+) for You!

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. Refreshing TextArea
    By joolske in forum New To Java
    Replies: 4
    Last Post: 12-13-2012, 08:31 PM
  2. refreshing
    By willy in forum New To Java
    Replies: 0
    Last Post: 12-04-2011, 10:56 AM
  3. Method value not refreshing
    By Thalvik in forum New To Java
    Replies: 3
    Last Post: 04-27-2011, 10:21 PM
  4. Help: Container not refreshing
    By cclementi6 in forum AWT / Swing
    Replies: 2
    Last Post: 04-10-2011, 04:27 AM
  5. Bug in refreshing jsp
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-31-2007, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •