JComboBox & Time Specific Content

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1753 Views - Last Post: 13 April 2011 - 03:35 PM Rate Topic: -----

#1 seaneyb  Icon User is offline

  • D.I.C Head

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

JComboBox & Time Specific Content

Posted 10 April 2011 - 12:16 PM

Right, this is my final piece to my project! i basically need to have a drop down to set a specific time for a boiler to switch on and off

It will have 2 time slots typically for the morning and evening

Now i want to use a drop box is there a way to set a time variable in a JCOMBOBOX?
Is This A Good Question/Topic? 0
  • +

Replies To: JComboBox & Time Specific Content

#2 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: JComboBox & Time Specific Content

Posted 10 April 2011 - 01:31 PM

No, but you can make a thread and delay how long it takes until something is executed. For example, the person enters the time, and you start the boiler. Then, you stop it after the time specified.

Look at the Oracle Tutorial for more info.
Was This Post Helpful? 0
  • +
  • -

#3 seaneyb  Icon User is offline

  • D.I.C Head

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

Re: JComboBox & Time Specific Content

Posted 10 April 2011 - 02:49 PM

thanks! i wish there was i just want to set a start and end time and let it run around that.
I'll have a read in the morning and try and crack out some code! anymore tips would be useful thankyou.
Was This Post Helpful? 0
  • +
  • -

#4 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: JComboBox & Time Specific Content

Posted 10 April 2011 - 03:08 PM

Some dedicated Googling could help you there. Other than that, I am out of ideas.
Was This Post Helpful? 0
  • +
  • -

#5 seaneyb  Icon User is offline

  • D.I.C Head

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

Re: JComboBox & Time Specific Content

Posted 10 April 2011 - 03:24 PM

Well i think im going to use dropbox

then convert the string into military time then set the time around an if statement so when military time on = real time turn on boiler

else switch off boiler when it reaches military time off....

something like that.
Was This Post Helpful? 0
  • +
  • -

#6 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: JComboBox & Time Specific Content

Posted 10 April 2011 - 03:31 PM

That sounds like a reasonable way to do it. Google can also be a big help, like I said before. Give me a plus please
Was This Post Helpful? -1
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: JComboBox & Time Specific Content

Posted 10 April 2011 - 03:38 PM

Is this what you mean?
JComboBox takes any Object[] in its constructor, so yeah I guess you can use a time variables :)

String[] elements = { "00:00:00", "05:00:00" };

JComboBox = new JComboBox( elements );
Was This Post Helpful? 1
  • +
  • -

#8 seaneyb  Icon User is offline

  • D.I.C Head

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

Re: JComboBox & Time Specific Content

Posted 10 April 2011 - 04:01 PM

View PostCasiOo, on 10 April 2011 - 04:38 PM, said:

Is this what you mean?
JComboBox takes any Object[] in its constructor, so yeah I guess you can use a time variables :)

String[] elements = { "00:00:00", "05:00:00" };

JComboBox = new JComboBox( elements );


yes indeed! something very much like that have you got a good tutorial? id still call myself n00b programmer but im loving the learning curve!
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10768
  • View blog
  • Posts: 40,100
  • Joined: 27-December 08

Re: JComboBox & Time Specific Content

Posted 10 April 2011 - 10:54 PM

View PostsoftwareEngineer(), on 10 April 2011 - 04:31 PM, said:

No, but you can make a thread and delay how long it takes until something is executed. For example, the person enters the time, and you start the boiler. Then, you stop it after the time specified.

Look at the Oracle Tutorial for more info.

Swing is not Thread-safe. You should not be mixing Swing and Threads.

For scheduling tasks in Swing, there are other tools to use including:
-The java.util.Date class
-The Calendar class
-The Swing Timer class
Was This Post Helpful? 1
  • +
  • -

#10 seaneyb  Icon User is offline

  • D.I.C Head

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

Re: JComboBox & Time Specific Content

Posted 11 April 2011 - 03:03 AM

right ive read the calender class and swing timer the problem is i want it to switch on at a specific time of day not just run a delay randomly for a period of seconds or hours as such, using threads is an idea just by putting the thread to sleep when the boiler is activated which should keep the boiler status on.

I want the user to pick specific time for the boiler to switch on and off using the built in clock on the pc with relation to whatever the user specifys if you get what i mean?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10768
  • View blog
  • Posts: 40,100
  • Joined: 27-December 08

Re: JComboBox & Time Specific Content

Posted 11 April 2011 - 04:10 AM

You can still do that with the Calendar class. :)
Was This Post Helpful? 1
  • +
  • -

#12 seaneyb  Icon User is offline

  • D.I.C Head

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

Re: JComboBox & Time Specific Content

Posted 11 April 2011 - 04:14 AM

View Postmacosxnerd101, on 11 April 2011 - 05:10 AM, said:

You can still do that with the Calendar class. :)

oh cool i didnt know that :)

ive started writing a mock up bit of code to test the timer function now i know it doesnt work but this is the idea.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.text.DateFormat;

import java.text.SimpleDateFormat;

public class TimerDemo extends JFrame {
public static final DateFormat df = new SimpleDateFormat("hh:mm:ss");
 
public TimerDemo() throws HeadlessException {
initUI();
}
 
private void initUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 200);
 
Container container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.CENTER));
 
final JLabel label = new JLabel();
container.add(label);
 
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Calendar now = Calendar.getInstance();
 
String timeText = "<html><font size=\"6\" color=\"blue\">" + df.format(now.getTime()) + "</font></html>";
 
label.setText(timeText);
}
});
timer.start();
if (timer == 12:10:00){
	System.out.println("Boiler Is ON");
	
}
}
 
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TimerDemo().setVisible(true);
}
});
}
}


Was This Post Helpful? 0
  • +
  • -

#13 seaneyb  Icon User is offline

  • D.I.C Head

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

Re: JComboBox & Time Specific Content

Posted 11 April 2011 - 09:52 AM

Ok so ive had a play now im using the calender as well to generate the timer


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.text.DateFormat;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class TimerDemo extends JFrame {
public static final DateFormat df = new SimpleDateFormat("hh:mm:ss");
 
public TimerDemo() throws HeadlessException {
initUI();
}
 
private void initUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 200);
 
Container container = getContentPane();
container.setLayout(new FlowLayout(FlowLayout.CENTER));
 
final JLabel label = new JLabel();
container.add(label);
 
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Calendar now = Calendar.getInstance();
 
String timeText = "<html><font size=\"6\" color=\"blue\">" + df.format(now.getTime()) + "</font></html>";

label.setText(timeText);
}
});
timer.start();
runningtimer();
}
public void runningtimer(){
	
	Calendar cal = new GregorianCalendar();

	//Get the components of the time
	int hour24 = cal.get(Calendar.HOUR_OF_DAY);     // 0..23
	int min = cal.get(Calendar.MINUTE);             // 0..59
	
	for(;;)/>{
	if (hour24 == 17 &&  min == 50){
		System.out.println("Boiler Is ON");
		
	}
	else{
		System.out.println("Boiler Is OFF");
		
	}
	}};
	 
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TimerDemo().setVisible(true);
}
});
}
}



so i thought i'd stick it i an infinate for loop so when the exact minute approaches it will simply print out Boiler status on however it doesnt it just keeps printing out off unless i execute the program during the same minute, also the jframe doesnt appear and show a clock atm :\ which is slightly annoying :(

any ideas?

This post has been edited by seaneyb: 11 April 2011 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#14 seaneyb  Icon User is offline

  • D.I.C Head

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

Re: JComboBox & Time Specific Content

Posted 11 April 2011 - 02:13 PM

still very confused :((

you cant use a for loop for this kind of statement can you?
Was This Post Helpful? 0
  • +
  • -

#15 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: JComboBox & Time Specific Content

Posted 11 April 2011 - 02:20 PM

You can use a while loop.

This takes a condition like:


while (this is still true)

{

//do all this

}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2