4 Replies - 860 Views - Last Post: 04 February 2011 - 10:16 PM Rate Topic: -----

#1 seaneyb   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

setText problem....

Posted 04 February 2011 - 02:02 PM

Hi everyone i havent programmed in a while and im trying to design a gui for a zigbee sensor network... currently im making a prototype board and designing the gui... So far so good! however i think im making a big stupid mistake here. As a rule you cannot use an interger in a set text variable correct

for example
please ignore that its reading to txtcurrenttemp im just using that to read the values in for now.
your help will be greatly appreciated.

import javax.swing.*;

import java.awt.Color;
import java.awt.LayoutManager;
import java.awt.event.*;
import java.util.Set;


public class GUI
{
	public JFrame frame;
	public JPanel panel;
	public JLabel logo;
	public JButton temprefresh, tempup, tempdown;
	public JTextField txtcurrenttemp;
	public JLabel roomtemp;
	int temp= 19;
	int inctemp= 0;
	int inc = 1;
public	GUI() {

	frame = new JFrame();
	frame.setTitle("Zigbee Thermostat Control");
	frame.setSize(700, 580);

	//sets up panel
	panel = new JPanel();
	panel.setBackground(Color.WHITE);
	panel.setLayout(null);
	frame.getContentPane().add(panel);
	//Add Logo
	ImageIcon image = new ImageIcon("src/logo.jpg");
	logo = new JLabel(image);
	logo.setBounds(0,0,120,100);
	panel.add(logo);
	
	//Menu Bar
	JMenuBar mb = new JMenuBar();
	frame.setJMenuBar(mb);
	
	//File option added
	JMenu file = new JMenu("File");
	mb.add(file);
	
	//About option added
	JMenu about = new JMenu("About");
	mb.add(about);
	
	//Adds Exit Function
	JMenuItem exit = new JMenuItem ("Exit");
	file.add(exit);
	exit.addActionListener(new ActionListener()
	{public void actionPerformed(ActionEvent event) 
	{
		System.exit(0);
	}
	});
	roomtemp = new JLabel("Current Room Temperature");
	roomtemp.setBounds(10,100,200,100);
	panel.add(roomtemp);
	
	//Add text field
	txtcurrenttemp = new JTextField();
	txtcurrenttemp.setBounds(175,135,150,35);
	panel.add(txtcurrenttemp);
	
	temprefresh = new JButton("Refresh");
	temprefresh.setBounds(50,170,100,25);
	panel.add(temprefresh);
	frame.setVisible(true);
	panel.setVisible(true);
	temprefresh.addActionListener(new ActionListener()
	{public void actionPerformed(ActionEvent event) 
	{
		txtcurrenttemp.setText("19°c");
	}
	});
	
	tempup = new JButton("UP");
	tempup.setBounds(500,50,100,50);
	panel.add(tempup);
	frame.setVisible(true);
	panel.setVisible(true);
	tempup.addActionListener(new ActionListener()
	{public void actionPerformed(ActionEvent event) 
	{
		inctemp = temp + inc;
		inctemp = temp;
		txtcurrenttemp.setText(temp);
		
	}
	});
	tempdown = new JButton("DOWN");
	tempdown.setBounds(500,110,100,50);
	panel.add(tempdown);
	tempdown.addActionListener(new ActionListener()
	{public void actionPerformed(ActionEvent event) 
	{
		inctemp = temp - inc;
		inctemp = temp;
	        txtcurrenttemp.setText(temp);
		
	}
	});
	frame.setVisible(true);
	panel.setVisible(true);
	}

	public static void main(String[] args) {
		new GUI();

	}

	}






Is This A Good Question/Topic? 0
  • +

Replies To: setText problem....

#2 darek9576   User is offline

  • D.I.C Lover

Reputation: 204
  • View blog
  • Posts: 1,747
  • Joined: 13-March 10

Re: setText problem....

Posted 04 February 2011 - 02:12 PM

setText(String tex) takes a string.
Was This Post Helpful? 0
  • +
  • -

#3 seaneyb   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: setText problem....

Posted 04 February 2011 - 02:17 PM

i dont want to take a string

i want to be able to display the int temp in a textfield

you simply cannot do txtcurrenttemp.setText(temp); as temp is an int
Was This Post Helpful? 0
  • +
  • -

#4 seaneyb   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: setText problem....

Posted 04 February 2011 - 02:24 PM

done it in the end by doing this.....
{public void actionPerformed(ActionEvent event) 
	{
		inctemp = temp + inc;
		temp = inctemp;
		mystring= Integer.toString(temp);
		txtcurrenttemp.setText(mystring);
		
	}
	});

Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: setText problem....

Posted 04 February 2011 - 10:16 PM

Or more simply (lousy)
public void actionPerformed(ActionEvent event) 
	{
		temp += inc;
		txtcurrenttemp.setText("" + temp);
		
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1