I am trying to create a JForm using NetBeans's Design mode and then developing it in Source mode. I have two buttons in my form and I want to check whether the generateNumber button is clicked or not before the makeGuess button performs its task. Here are the related code lines.
private void generateNumberActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void makeGuessActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
if (!generateNumber.getModel().isPressed()) {
throw new GameNotStartedException();
}
} catch (GameNotStartedException ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(this, "Generate a number first");
}
}
My problem is the program always falls into the GameNotStartedException, not matter if I click to generateNumber button or I do not. Can anyone help me, please?
I think the problem is the usage of generateNumber.getModel().isPressed() itself. This tells you if the button is currently pressed or not.
As you have it in the method makeGuessActionPerformed it is like this:
You are pressing the button makeGuess
The method makeGuessActionPerformed is executed
As part of the method you check if button generateNumber is clicked (which is not true, as you clicked on button makeGuess)
You could e.g. use a boolean value to keep track of the status. The value of the boolean reflects if the button generateNumbers was already pressed.
A click on the button generateNumbers sets the boolean value to true.
In the method makeGuessActionPerformed you can check for the value of the boolean. If false, then throw the exception. Otherwise continue with your execution.
Related
I have most of my java application done already, so I am now logging all user activity. One thing I want to keep track of is whenever one of my checkboxes is checked and unchecked. I am able to read at the end if the object is checked or unchecked, however I want to know each time the checkbox is used in real time. I want to know how many times the user checks and unchecks the box. Right now I am using something similar to this syntax, but it doesn't print a statement each time the checkbox is checked and unchecked.
public CheckboxAction(String text) {
super(text);
}
#Override
public void actionPerformed(ActionEvent event) {
JCheckBox cbLog = (JCheckBox) event.getSource();
if (cbLog.isSelected()) {
System.out.println("Logging is enabled");
} else {
System.out.println("Logging is disabled");
}
}
An ItemListener seems appropriate here
yourJCheckBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
if (yourJCheckBox.isSelected()) {
// Code to execute when it's selected
}
else {
// Code to execute when not selected
}
}
});
First and foremost, do not override actionPerformed. If you need to - at least call super.actionPerformed before performing your action. Best way is to use addActionListener or in this case as #BoDidely mentioned use an addItemListener.
I'm using java WWsdk.
I am expecting the SelectListener to respond to user clicks on the map when user clicks just the map (i.e. not an icon or placemark,etc..).
It works fine for me when i click on my objects, but it doesn't trigger when i click "empty space". i.e. like water/land.
The docs for SelectListener says
If no object is under the cursor but the cursor is over terrain, the select event will >identify the terrain as the picked object and will include the corresponding geographic >position
This statement makes it sound like i should get an event whenever i don't click an object,but i don't get this.
Am i supposed to add some other kind of layer to get clicks on map to trigger select events?
I use this which is working for me for actions i need performed on objects that get clicked:
this.worldWindowGLCanvas1.addSelectListener(new SelectListener()
{
public void selected(SelectEvent event)
{
//Never goes here for clicks on map, just clicks
//on objects i have already created.
doStuff();
}
}
Add a MouseListener using addMouseListener():
this.worldWindowGLCanvas1.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
doStuff();
}
...
}
You may also want to add the listener to the AWTInputHandler instead of the WorldWindowGLCanvas if you want to prevent World Wind from doing something else with the click. More details in this question.
my question is. Is possible to add a component like a button (button has a functionality that triggered when it is clicked) inside a list component?
This image explain better what I refer:
http://2.bp.blogspot.com/-HThpKcgDyRA/URI_FdpffMI/AAAAAAAAAUI/SficZAPXaCw/s1600/1.png
Yes but it requires some handcoding and it will only work for touch (since you won't be able to assign focus to it).
We normally recommend just using Component/Container hierarchies for these cases rather than dealing with lists but obviously this isn't always practical.
The key is to always use the list action listener to trigger events, nothing else. So when you are in the action handling code of the list you would want to know if it was triggered by your button...
If you are in the GUI builder this is pretty easy:
Button b = ((GenericListCellRenderer)list.getRenderer()).extractLastClickedComponent();
if(b != null && b == myButton) {
// your event code here for the button, the selected entry is list.getSelectedItem()/Index()
}
The handcoded approach is pretty similar with one major caveat, you don't have the extractLastClickedComponent method. So assuming you have a component within the renderer just add an action listener to it. Within the action listener just set a flag e.g.:
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
buttonWasClicked = true;
}
});
// within the list listener we do the exact same thing:
if(buttonWasClicked) {
// for next time...
buttonWasClicked = false;
// your event code here for the button, the selected entry is list.getSelectedItem()/Index()
}
Yes!!! Try it, this is pretty easy:....
http://www.codenameone.com/how-do-i---create-a-list-of-items-the-easy-way.html
http://www.codenameone.com/how-do-i---create-a-list-of-items-the-hard-way-gui-builder-renderer.html
Clicking the button should toggle between setting the textfield as editable or not. When the GUI opens the textfield should be editable.
public void actionPerformed( ActionEvent evt)
{
if(inputField.setEditable() == (true))
{
inputField.setEditable(false);
}
else
{
inputField.setEditable(true);
resultMessage.setText("");
resultMessage.setText("Edit Button Pressed");
}
What am i doing wrong here?
I know that the else statement is right just the start of the IF is wrong, i'm not sure where i'm going wrong.
Additional question:
public void actionPerformed( ActionEvent evt)
{
if(inputField.isEditable() == (true))
{
inputField.setEditable(false);
}
else
{
inputField.setEditable(true);
inputField.setText("");
resultMessage.setText("Edit Button Pressed");
}
if(inputField.getBackground() == Color.RED)
{
inputField.setBackground(Color.WHITE);
}
else
{
inputField.setBackground(Color.RED);
resultMessage.setText("Colour Button Pressed");
}
}
I now currently have 2 IFs but obviously the first IF will stop the second IF from working how do i get around this so that when i press one button that will do the setEditable section and when i press the other button it will do my change colour?
You're checking if setEditable() is true which really makes no sense; after all, it returns void and requires a boolean parameter. The bigger problem is that you shouldn't be checking the editable status via a "setter" field but rather a "getter" field, here isEditable()
if (inputField.isEditable()) {
//....
} else {
//...
}
As MadProgrammer points out, this information is all readily available in the Java API, something you'll want to get very familiar with.
Edit, regarding your new question:
I now currently have 2 IFs but obviously the first IF will stop the second IF from working how do i get around this so that when i press one button that will do the setEditable section and when i press the other button it will do my change colour?
No, the first if/else will have no effect on the second if/else block. Your problem lies elsewhere. Consider asking this with additional information as a new question on stackoverflow if still suck.
Also note that this is unnecessarily wordy:
if(inputField.isEditable() == (true))
and is better written as I have it above:
if (inputField.isEditable())
I tried this
private void botaoConfIOMouseClicked(java.awt.event.MouseEvent evt) {
ConfigurarIO popup = new ConfigurarIO();
popup.setVisible(true);
botaoConfIO.setEnabled(false); //this line to avoid multiple dialogues
setIO=popup.getConfig(); //i need to get this boolean from the dialogue "ConfigurarIO"
//part of the program only to make my logic from the setIO
if(setIO[0]==false){
jToggleButton1.setEnabled(false);
jToggleButton1.setText("SaĆda");
}
else{
jToggleButton1.setEnabled(true);
if(jToggleButton1.isSelected()) jToggleButton1.setText("Pino 1 ON");
else jToggleButton1.setText("Pino 1 OFF");
}
}
And this is the dialogue
public class ConfigurarIO extends javax.swing.JFrame {
boolean[] inOut=new boolean[8];
boolean ok=false;
/** Creates new form ConfigurarIO */
public ConfigurarIO() {
initComponents();
}
public boolean[] getConfig(){
return inOut;
}
public boolean getOK(){
return ok;
}
public void setOK(){
ok=false;
}
//the logic was emited
private void botaoOKMouseClicked(java.awt.event.MouseEvent evt) {
dispose();
ok=true;
System.out.println(ok);
}
The problem is that the setIO is not modified by the second interface and, If I set this to make a loop broken only by the "ok" boolean, the window with the setting interface doesn't open. This is a very explored problem but I am new to Netbeans and I couldn't find it on Google. Thanks for the attention
Print screen: http://4.bp.blogspot.com/-B7VWmPelJek/T2ysJV8PJcI/AAAAAAAABqQ/0waWxxEEHkw/s320/temp.png
You haven't said whether a frame is required for some reason, or whether a dialog would do, or whether whatever it is needs to be modal.
The reason the frame doesn't show up if you loop is that you're on the Swing dispatch thread (since you are in a routine that responded to a mouse click), and until it returns, it isn't going to update the screen.
You cannot just call a method on the "frame dialog" to get a value until you know that the dialog has set the value. I would pass my calling class to the dialog as a parameter on the constructor, and then have the dialog code call a method on the calling class when it's all done. If you need to know when this happens, then you'll have to treat it as an event in your calling class; I can't guess what you need for that without knowing more about what you're trying to do overall.
If you need to wait until the dialog is done, and don't need the user to be able to do anything until it is done, then what you want is a "modal" dialog, and I recommend looking at JOptionPane and its various dialog options for what you want to do. THEN the call from your class can be synchronous, i.e., you can call the dialog and, when the call completes, the dialog is all done. Then you don't need to pass the calling class to the frame, since it doesn't need to notify you that it's done; you know it's done when your call completes, and you can call a method such as you have already done to get the value that you want.
Incidentally, your subclass-of-JFrame constructor doesn't call super(); I recommend you do that...
rc
// we will make this modal=true, to block access to the parent frame
public class ConfigurarIO extends javax.swing.JDialog {
For more details, see:
How to Make Dialogs
How to Use Modality in Dialogs