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?
26 Replies - 1072 Views - Last Post: 13 April 2011 - 03:35 PM
Replies To: JComboBox & Time Specific Content
#2
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.
Look at the Oracle Tutorial for more info.
#3
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.
I'll have a read in the morning and try and crack out some code! anymore tips would be useful thankyou.
#4
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.
#5
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.
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.
#6
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
#7
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 );
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 );
#8
Re: JComboBox & Time Specific Content
Posted 10 April 2011 - 04:01 PM
CasiOo, 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 );
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!
#9
Re: JComboBox & Time Specific Content
Posted 10 April 2011 - 10:54 PM
softwareEngineer(), 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.
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
#10
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?
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?
#11
Re: JComboBox & Time Specific Content
Posted 11 April 2011 - 04:10 AM
You can still do that with the Calendar class.
#12
Re: JComboBox & Time Specific Content
Posted 11 April 2011 - 04:14 AM
macosxnerd101, 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);
}
});
}
}
#13
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
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?
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
#14
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?
you cant use a for loop for this kind of statement can you?
#15
Re: JComboBox & Time Specific Content
Posted 11 April 2011 - 02:20 PM
You can use a while loop.
This takes a condition like:
This takes a condition like:
while (this is still true)
{
//do all this
}
|
|

New Topic/Question
Reply



MultiQuote






|