setEditable using ActionListener - java

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())

Related

Can not check whether a JButton is clicked using NetBeans

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.

Keeping Track of Every Time that a Checkbox is checked and unchecked

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.

Java ListSelectionListener interface with keyboard

I have implemented ListSelectionListener as you can see below, so that after a specific line in the first table is being chosen, the second table gets updated accordingly.
class SelectionListener implements ListSelectionListener {
public SelectionListener(){}
#Override
public void valueChanged(ListSelectionEvent e)
{
if (e.getSource() == myTrumpsAndMessages.jTable1.getSelectionModel()
&& myTrumpsAndMessages.jTable1.getRowSelectionAllowed()
&& e.getValueIsAdjusting())
{
int selected = myTrumpsAndMessages.jTable1.getSelectedRow();
clearjTable(jTable4);
showSubscribers(selected);
}
}
}
Is there a way to invoke the listener not only when the mouse is choosing, but also when the choice is being made from the keyboard?
The reason for the unusual experience - no notification on selection via keyboard - is a subtle different setting of valueIsAdjusting for keyboard vs. mouse-triggered selection events:
keyboard triggered selection (even with modifiers) only fires once (with adjusting == false)
mouse triggered selection always fires twice (first with true, second with false)
That fact combined with the unusual logic (which #Robin spotted, +1 to him :-)
if (e.getSource() == myTrumpsAndMessages.jTable1.getSelectionModel()
&& myTrumpsAndMessages.jTable1.getRowSelectionAllowed()
// typo/misunderstanding or feature? doing stuff only when adjusting
&& e.getValueIsAdjusting())
(reacting only if the selection is adjusting) leads to not seeing keyboard triggered changes.
Is there a way to invoke the listener not only when the mouse is choosing, but also when the choice is being made from the keyboard?
The listener will be triggered, independent of the source of the selection change. So yes, this is perfectly possible and even the default behavior. So nothing special must be done to get this working.
Looking at the code of your listener, I would suggest to rewrite it to
class SelectionListener implements ListSelectionListener {
public SelectionListener(){}
#Override
public void valueChanged(ListSelectionEvent e){
if ( e.getValueIsAdjusting() ){
return;
}
if (e.getSource() == myTrumpsAndMessages.jTable1.getSelectionModel() &&
myTrumpsAndMessages.jTable1.getRowSelectionAllowed() ) {
int selected = myTrumpsAndMessages.jTable1.getSelectedRow();
clearjTable(jTable4);
showSubscribers(selected);
}
}
}
Note the quick break from the method when getValueIsAdjusting() returns true as this is the behavior you want in most cases.
I've just tried a ListSelectionListener and the valueChanged() event is actually being triggered on keyboard selection change as well. See my example below:
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
System.out.println(list.getSelectedValue());
}
});

How to remove an inner class ActionListener?

I am making a text based game, but I am having a rather large problem. This problem is that when I assign a new ActionListener to a button that already has an ActionListener assigned to it, it does both of the actions. Here's my code:
while(shouldLoop) {
if(Player.loc == 1) {
left.setText("Do nothing");
left.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
nar1.setText("You are still in a dungeon."); //Here's my first assignment
}
});
right.setText("Pick the lock");
right.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Player.loc = 2;
}
});
} if(Player.loc == 2) {
nar1.setText("You are now outside");
nar2.setText("the door. What now?");
forward.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
nar1.setText("You hear a guard.");
nar2.setText("What do you do now?");
Player.loc = 3;
}
});
left.addActionListener(new ActionListener() { //Here's another
#Override //assignment to
public void actionPerformed(ActionEvent e) {//the same button
nar1.setText("You hear a guard."); //so when I press
nar2.setText("What do you do now?"); //it here, it
Player.loc = 3; //performs the
} //original assignment
});
right.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
nar1.setText("You hear a guard.");
nar2.setText("What do you do now?");
Player.loc = 3;
}
});
right.setText(rgt);
forward.setText(fwd);
back.setText(bck);
left.setText(lft);
forward.setVisible(true);
} if(Player.loc == 3) {
forward.setVisible(false);
right.setText("Sneak around him!");
left.setText("Fight him!");
}
Thanks for helping,
billofbong
Why don't you just hoist the addActionListener code out of the while loop?
Anonymous inner class listeners look nice in quick and dirty example code, but, in practice, they are a horrible idea. You are learning one of the reasons. Other reasons are that they can't readily be subclassed or modified via Dependency Injection types of things, can't readily be shared (say a button, a toolbar icon, and a menu do the same thing), can't readily be enabled / disabled, (say, "Save As" should be disabled cause nothing is open) etc...
For this use case, break them out into some series of actual Listeners, perhaps organized in a class, array, or some Enums, so they can get swapped in and out.
You are mixing data with code in a bad way by hard-coding the logic of your program in the program itself, and this is the main source of your current and future problems. This won't work, at least not without a lot of kludges.
I suggest you try to separate your data out of your code and make your program more MVC-ish, which stands Model-View-Controller (or one of its many variants). For instance the logic of the program is held by the Model and this includes the non-visualized map of the land being explored, the position of the player in this map, his companions, and your inventory of items that you've collected. So you will likely have several non-GUI classes such as Player, Map, Item (this may be an interface), Room, etc... The Map itself, the possible items, there locations, will be specified in a data file, perhaps a text file for a simple program or a database for a more complex one, and you will need classes to read and parse this file(s).
The View of your program is your GUI, and it should be as dumb as possible. Here is where you'll have your buttons, your text display, and if fancy, a graphical display of your game. No program logic goes in here. None.
The Control will be the Action Listeners that you add to your JButtons and any other code that has to deal with handling user input. But this code could be relatively simple, and it's main task is to pass user interactions to the model. So for instance if the left button is pressed, it may be simply something like
model.leftButtonPress();
Then the model will decide what to do with a left button press, if anything. The model will know where you are in the game for instance, if there's a door to your left or if it's a wall, and based on the state of the model your game will perform the requisite action.

MouseListener fired without checking JCheckBox

This one is pretty crazy:
I've got an AppSight recording (for those not familiar, it's a recording of what they did including keyboard/mouse input + network traffic, etc) of a customer reproducing a bug. Basically, we've got a series of items listed on the screen with JCheckBox-es down the left side. We've got a MouseListener set for the JPanel that looks something like this:
private MouseAdapter createMouseListener() {
return new MouseAdapter(){
public void mousePressed( MouseEvent e ) {
if( e.getComponent() instanceof JCheckBox ) {
// Do stuff
}
}
};
}
Based on the recording, it appears very strongly that they click just above one of the checkboxes. After that, it's my belief that this listener fired and the "Do stuff" block happened. However, it did NOT check the box. The user then saw that the box was unchecked, so they clicked on it. This caused the "Do stuff" block to fire again, thus undoing what it had done the first time. This time, the box was checked. Therefore, the user thinks that the box is checked, and it looks like it is, but our client thinks that the box is unchecked as it was clicked twice.
Is this possible at all? For the life of me, I can't reproduce it or see how it could be possible, but based on the recording and the data the client sent to the server, I can't see any other logical explanation.
Any help, thoughts, and or ideas would be much appreciated.
Maybe the user clicked on the checkbox, but before release the mouse button, he moved the mouse away from the component. I'm pretty sure the chechbox won't get checked in this case, although not so sure about the Event Thread behaviour under this scenario.
I don't think you can assume that because the mouse was pressed on the checkbox that it will be also released on the checkbox. Maybe just do something like this:
private MouseAdapter createMouseListener() {
return new MouseAdapter(){
public void mouseReleased( MouseEvent e ) {
if( e.getComponent() instanceof JCheckBox ) {
// And just to be sure....
JCheckBox jcb = (JCheckBox) e.getComponent();
if(jcb.isSelected())
{
// Do stuff
}
}
}
};
}

Categories