This is the MessageBox class. This uses the CardLayout format to display the set of messages. It is the container class. Along with the messages, we send corresponding values for minutes and seconds that are displayed on a label.
//Reference - SynforgeTutorials
public class MessageBox extends JFrame{
JPanel p=new JPanel();
CardLayout cards=new CardLayout();
int numQs;
int cardnumber;
CL1 messages[]={
new CL1("Hi! How are you doing ?",1,30,this),
new CL1("I am good thanks",2,0,this),
new CL1("Its a great dayoutside",0,30,this),
new CL1("I am going to go hiking",1,0,this),
new CL1("Maybe I will join you too",1,0,this)
};
public static void main(String[] args) {
new MessageBox();
}
public MessageBox(){
super("Message Box");
setResizable(true);
//setSize(500,400);
setBounds(100, 100, 450, 226);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p.setLayout(cards);
numQs=messages.length;
for(int i=0;i<numQs;i++){
p.add(messages[i],"q"+i);
}
cardnumber = 0;
cards.show(p,"q"+ cardnumber);
add(p);
setVisible(true);
}
This is the class that constructs the cards. It has 3 panels - the top panel where the message is displayed, the center panel where the Minutes and Seconds are displayed on a label, and the bottom panel where the OK button is present. On clicking the 'OK' button, the next card is displayed.
public class CL1 extends JPanel implements ActionListener {
MessageBox mb;
//Message
JPanel qPanel=new JPanel();
//Timer
JPanel tPanel=new JPanel();
JLabel timeLbl = new JLabel("New label");
JButton btnPause=new JButton("Pause");
JButton btnResume=new JButton("Resume");
//bottom
JPanel botPanel=new JPanel();
JButton OK=new JButton("OK");
public CL1(String q, int userMinutes, int userSeconds, MessageBox mb){
this.mb=mb;
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
//Message
qPanel.add(new JLabel(q));
add(qPanel);
//Timer
tPanel.add(timeLbl);
tPanel.add(btnPause);
tPanel.add(btnResume);
add(tPanel);
//bottom
//OK.setBounds(323, 139, 97, 25);
OK.addActionListener(this);
botPanel.add(OK);
add(botPanel);
}
////////////////////////////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent e){
Object src=e.getSource();
//OK button
if(src.equals(OK)){
mb.OK();}
}
}
I want to be able to send the messages, minutes and seconds values from another class, and be able to call the MessageBox class multiple times, like shown below. This class sends a list of messages, the minutes and seconds for each corresponding message. I want to call the MessageBox class multiple times with different sets of messages.
Calling class
{
public static void main(String[] args) {
String[] displaymsg1 = {"Hi! How are you doing ?","I am good thanks","Its a great dayoutside","I am going to go hiking","Maybe I will join you too"};
int[] mins1 = {1,2,0,1,1};
int[] secs1 = {30,0,30,0,0};
new MessageBox(displaymsg1 ,mins1,secs1);
String[] displaymsg2 = {"Monday","Tuesday","Wednesday","Thursday","Friday"};
int[] mins2 = {0,1,2,3,2};
int[] secs2 = {0,25,0,10,20};
new MessageBox(displaymsg2,mins2,secs2);
}
}
How can I go about this? I changed the constructor of MessageBox to accept the arguements and created a for loop. But this doesn't recognize the array of objects 'messages[i]'.
public MessageBox(String displaymsg[],int mins[],int secs[]){
super("Step Message");
setResizable(true);
CL1 messages[];
for(int i=0;i<messages.length;i++)
{
messages[i]={
new CL1(displaymsg[i],mins[i],secs[i],this)
};
}
Any suggestions would be appreciated. Thanks.
Your error/problem has nothing to do with Swing and appears to have all to do with how to create arrays on the fly. I think that what you want to do is to initialize your messages variable as a new CL1 array of length displaymsg.length, and then in the for loop, create items for each array:
public MessageBox(String displaymsg[], int mins[], int secs[]) {
super("Step Message");
setResizable(true);
messages = new CL1[displaymsg.length];
for (int i = 0; i < messages.length; i++) {
messages[i] = new CL1(displaymsg[i], mins[i], secs[i], this);
}
}
Having said this, I'd probably do things a bit differently. If most of what you're doing is swapping messages, then you may not even need CardLayout, but instead could simply swap text held in a single JLabel. Hard to say without knowing more about your actual requirements.