//boutton
ent.setBounds(490,400,100,50);
ent.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String text;
text=chatbox.getText().toLowerCase();
chatarea.append("\t\t\t\t"+text+" <- YOU \n");
chatbox.setText("");
if(text.contains("hii") || text.contains("hey") || text.contains("hi"))
{
bot("hey there");
bot("how can i help you?");
}
else if(text.contains("info") || text.contains("help") || text.contains("i need information") || text.contains("help me"))
{
bot("sure i am here to help you");
bot("what you want to know about?");
bot("1 info about anything?");
bot("2 info about place?");
bot("enter your choice : ");
if(text.contains("1")|| text.contains("info about anything") || text.contains("number 1") || text.contains("first one") || text.contains("first"))
{
bot("sure which blood group info you are looking for?");
}else if(text.contains("2") || text.contains("info about place") || text.contains("number 2") || text.contains("second one") || text.contains("second"))
else
{
bot("I Don't Understand you");
}
}
}
});
}
private void bot(String string)
{
chatarea.append("BOT -> "+string+"\n");
}
public static void main(String[] args)
{
new bot();
}
}
I am making a chat bot in which when I click on the JButton it takes the value from JTextField and posts it on a JTextArea. But the problem is I want my code to be continue not stop but every time click on button it start the code from starting.
How to make code continue from same place in action performed when I second time click on the button?
Add a boolean member to your botclass to keep track of the state (first time / not first time) and initialise it to true
boolean isFirstTimeButtonWasPressed = true
public void actionPerformed(ActionEvent e) {
if (isFirstTimeButtonWasPressed) {
//do stuff that should only happens the firs time
//...
isFirstTimeButtonWasPressed = false;
}
//do stuff that should be done every time the button is pressed
});
Related
I'm trying to make a timer that has a jbutton called Short Break and a jbutton called Customize.
By clicking the default Short Break button the time will be set to 5.00 minutes.
But the user can customize the time with the help of JSpinner by clicking on the customize button.
But if the user wants to reduce the short break time by clicking on the customize button, then clicking on the short break button after customizing will show the customized time.
But after customizing, the customized time is shown, but clicking on the short break button without customizing does not show the default value. Means that the if condition only work but the else condition doesn't.
My JSpinner Code:
spinnerShortBreak.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
value2 = Integer.parseInt(String.valueOf(spinnerShortBreak.getValue()));
}
});
The Short Break button Code:
btnShortBreak.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
timer.stop();
second = 00;
minute = 05;
if (spinnerShortBreak !=null && spinnerShortBreak.getModel().getValue().equals(value2)) {
lblMinute.setText(String.valueOf(value2));
}
else if(spinnerShortBreak == null && spinnerShortBreak.getValue() == null) {
btnStartStop.setText("Start");
lblMinute.setText("05");
lblSecond.setText("00");
}
}
});
I find a solution. If i check the value is equal to 0 or greater than 0 the code will work perfectly.
here the button code:
btnShortBreak.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
timer.stop();
if(value2 > 0){
second = 00;
lblMinute.setText("0"+String.valueOf(value2));
btnStartStop.setText("Start");
lblSecond.setText("00");
}
else if(value2 == 0) {
second = 00;
minute = 05;
btnStartStop.setText("Start");
lblMinute.setText("05");
lblSecond.setText("00");
}
}
});
How can I transfer data into my other frame cause I'm doing If else statements in one Jbutton and so that the confirm button will also check the text fields that are empty and I also want it to act as a button to open my next frame then send that data to the next frame. My code doesn't seem to work other functions work and it also opens the other frame but doesn't display the data I need like: Name and will display the name in the other frame, need some help :(
JButton confirm = new JButton("Sign Up");
confirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
UserInfo a = new UserInfo();
if(!firstname.getText().trim().isEmpty() && !lastname.getText().trim().isEmpty() && password.getPassword().length != 0 && pass2.getPassword().length != 0
&& !address.getText().trim().isEmpty() ) {
String inputText = firstname.getText();
a.D1.setText(inputText);
a.frame2.setVisible(true);
frame.dispose(); }
else
if(firstname.getText().trim().isEmpty()) {
missingfirst.setText("Please Add Your First Name.");
}
if(lastname.getText().trim().isEmpty()) {
missinglast.setText("Please Add Your Last Name.");
}
if(address.getText().trim().isEmpty()) {
missingadd.setText("Please Enter An Address.");
}
if(password.getPassword().length == 0) {
missingpass.setText("Please Add A Password.");
}
if(pass2.getPassword().length == 0) {
missingrepass.setText("Please Re-Type Your Password.");
}
else if (!(password.getPassword().equals(pass2.getPassword()))) {
missingrepass.setText("Password Doesn't Match.");
}
}
This is my 2nd Frame
For example I have 2 checkbox and 1 button my code would be like this.
private class CheckBoxHandler implements ItemListener
{
#Override
public void itemStateChanged(ItemEvent e)
{
if (chckbxNewCheckBox1.isSelected() && chckbxNewCheckBox2.isSelected())
{
checkboxcheck1 = 1;
checkboxcheck2 = 1;
}
else if(chckbxNewCheckBox1.isSelected())
{
checkboxcheck1 = 1;
}
else if(chckbxNewCheckBox2.isSelected())
{
checkboxcheck2 = 1;
}
}
}
private class ButtonHandler implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e)
{
if (checkboxcheck1 == 1 && checkboxcheck2 == 1)
{
textFieldSum.setText(String.valueOf(counter));
textFieldSum1.setText(String.valueOf(counter1));
}
else if(checkboxcheck1 == 1)
{
textFieldSum.setText(String.valueOf(counter));
}
else if (checkboxcheck2 == 1)
{
textFieldSum1.setText(String.valueOf(counter1));
}
else
{
checkboxcheck1 = 0;
}
}
}
But then what if I have more than 2 checkbox like 10 or more.It would take forever to to make the if statement in CheckBoxHandler and ButtonHandler. Anybody know how to make it work if I have more than 2 checkbox ?
My program is read the file and count the specific character in the file and then display it. The way to display it is click on the checkbox and click the yes button. But it will take forever for me to do the if statement. You guys have any idea? Thanks yall so much for help.
Declare an array of checkboxes like so
CheckBox[] checkboxes = new CheckBox[10]; // or JCheckBox if using swing
And in the constructor, create them with
String[] filenames = { ... }; // have a list of files?
for(int i=0;i<checkboxes.length;i++){
// for each box, create and add it to your panel
mypanel.add(checkboxes[i]=new CheckBox(filename[i]));
checkboxes[i].addItemListener(mylistener);
}
then in the listener, you can have
public void itemStateChanged(ItemEvent e) {
boolean state = ((CheckBox)e.getSource()).isSelected();
// do something with this state if needed
}
But notice that you don't actually need the item listeners at all.
In the button listener you can do
public void actionPerformed(ActionEvent e) {
for (int i=0;i<checkboxes.length;i++){
if(checkboxes[i].isSelected()){
// do something with file i
}
}
}
Note that if your file operation takes time, you have to do the processing in a new thread, not just in the button handler!
I have an action listener attached to a JButton in my program.
When the button is clicked, a dialogue box opens that requests the user to enter a number, then click ok.
This part works fine, what I am having trouble doing is calling upon that number that the user has entered to use as part of an if statement.
Could someone please tell me how I call upon this number that the user has enterd, here is my code so far.
public void actionPerformed(ActionEvent e) {
if (e.getSource() == t1) {
String Message = "Enter an Amount ";
String number = JOptionPane.showInputDialog(null, Message,
JOptionPane.QUESTION_MESSAGE);
}
if () { // ideally here i would want to say if the user number is bigger
// than 0 then do this...
}
}
after the changes my code now looks like this
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == t1)
{
String Message = "Enter an Amount ";
String number =
JOptionPane.showInputDialog(null,
Message,
JOptionPane.QUESTION_MESSAGE);
if(Integer.valueOf(number) > 0)
{
String s = a.gettext();
getContentPane().removeAll();
repaint();
new TaxiFrame(Integer.parseInt(s));
}
else{}
}
else{}
}
}
try {
//.Convert string to Integer
if (Integer.valueOf(number) > 0) {
//. Do that
} else {
//. Otherwise
}
} catch (NumberFormatException err) {
err.printStackTrace();
//.Conversion failed. The user entered a non numeric string
}
I was trying to manipulate an amateur animation using Swing in Java. The goal was to start an animation if I hover over a certain button. That was easy. But the problem is, I want to manipulate the speed of the timer of the animation according to the coordinates of the button.
For example, the more right I go on the button the more speedy the animation gets (delay time decreases) and vice-versa. The code I wrote works if I move the arrow out of the button then enter again. Then the delay changes according to co-ordinates. But not if I move the arrow within the region of the button. I used mouseEntered here. That may cause cause the problem. But the mouseMove function seems not to work too.
Here's my code:
private void jButton1MouseEntered(java.awt.event.MouseEvent evt)
{
// TODO add your handling code here:
PointerInfo inf = MouseInfo.getPointerInfo();
Point a = inf.getLocation();
double x = a.getX();
final int n = (int) (x - 161);
//String str=""+x;
//Output.setText(str);
ActionListener taskPerformer = null;
taskPerformer = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent evt)
{
//...Perform a task...
//if(t>0)timer.setDelay(1000);
if (t > 0)
{
timer.setDelay(500 + 50 * n);
}
if (label1 == 0)
{
jTextField1.setText(" A");
}
else if (label1 == 1)
{
jTextField1.setText(" B");
}
else if (label1 == 2)
{
jTextField1.setText(" C");
}
else if (label1 == 3)
{
jTextField1.setText(" D");
}
else if (label1 == 4)
{
jTextField1.setText(" E");
}
else if (label1 == 5)
{
jTextField1.setText(" F");
}
else if (label1 == 6)
{
jTextField1.setText(" G");
}
if (label1 >= 7)
{
label1 = 0;
}
else
{
label1++;
}
t++;
//System.out.printf("Reading SMTP Info. %d\n",x);
//System.out.printf("Reading SMTP Info. %d\n",label1t);
//jLabel3.setText("fsd");
}
private Object getContentPane()
{
throw new UnsupportedOperationException("Not supported yet.");
}
};
timer = new Timer(500, taskPerformer);
timer.start();
//timer.setRepeats(false);
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
Logger.getLogger(Keyboard_Gui.class.getName()).log(Level.SEVERE, null, ex);
}
}
But the mouseMove function seems not to work too.
The mouseMoved event is generated by a MouseMotionListener (not a MouseListener) so you will also need to implement that listener to manipulate the Timer interval.