Trying to write a Swing GUI. When clicking a button, I want to test if the other button has already been clicked. If it has, then execute the statements in the "if" statements. But it looks like the "if" statements never execute?
private void radSingleBurgerActionPerformed(java.awt.event.ActionEvent evt) {
if(radDoubleBurger.isSelected()){
newItemPrice = Double.parseDouble(lblItemPrice.getText());
newItemPrice -= doublePrice;
lblTest.setText(String.valueOf(newItemPrice));//test to see if working
}
lblItemPrice.setText(String.valueOf(newItemPrice += singlePrice));
}
private void radDoubleBurgerActionPerformed(java.awt.event.ActionEvent evt) {
if(radSingleBurger.isSelected()){
newItemPrice = Double.parseDouble(lblItemPrice.getText());
newItemPrice -= singlePrice;
lblTest.setText(String.valueOf(newItemPrice));//test to see if working
}
lblItemPrice.setText(String.valueOf(newItemPrice += doublePrice));
}
Clicking a JButton does not make it (permanently) selected. Clicking is only a temporary action.
Maybe you want to use a JToggleButton. This allows you to click a button to make it selected and then click it again to make it unselected.
Read the section from the Swing tutorial on How to Use Buttons for more information.
Or if you just want to know if the user has clicked on a regular JButton, then you will need to maintain a Boolean variable yourself that you update in the ActionListener of the button.
Related
Im trying to create a gui, it has some checkbox and another components, under the start test buton there are some tests. when i click this button, firstly,disable all checkbox, after that start all checked tests. And then enable all failed test's checkbox.
problem is that; i clicked start button, all checkbox turn disable but in code side (i show it with breakpoint)!! did not disable in gui. All tests finished, and all fail tests turn enable other success tests turn disable successfully.
here is my codes template
btnStartTests.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
disable_all_checkbox();
startSelectedTests();
bla_bla_bla();
}
}
how can i disable these checkboxes properly (in click start button time)?
i use SwingWorker class, it is clearly runnig as i want thanks to #JBNizet
SwingWorker<Void,Void> worker2 = new SwingWorker<Void, Void>(){
#Override
protected Void doInBackground() throws Exception {
disable_all_checkbox();
startSelectedTests();
bla_bla_bla();
return null;
}
};
In the below code, I have a label named card with a mouse click event. I only want the click event to implement once. Meaning it will implement the first time I click the label, but not the following times. How do I do this? I imagine I must disable its Listener.
private void cardMouseClicked(java.awt.event.MouseEvent evt) {
// displays backside of each flashcards when label (flashcard) is clicked
i++;
card.setText(cardB[i]);
}
I think we all would do the same.
It's really simple. Just declare a boolean then change its status when you click the first time.
boolean labelClicked = false;
private void cardMouseClicked(java.awt.event.MouseEvent evt) {
// displays backside of each flashcards when label (flashcard) is clicked
if(!labelClicked){
i++;
card.setText(cardB[i]);
labelClicked=true;
}
else{
//doNothing
}
}
I am pretty new to programming and am trying to make a Minesweeper GUI. The game worked perfectly right clicking a JToggleButton displayed a "B" for bomb on the button, but when I replaced the setText() with setIcon() in the mouselistener it shows the icon when both left and right clicking occurs. I didn't have this problem when setText().
public void mousePressed(MouseEvent e){
if(e.isMetaDown())
if(btnPresses == 0)
{
startTime = System.currentTimeMillis();
btnPresses++;
}
//if(btn[y][x].getText().equals("B"))
if(btn[y][x].getIcon()==flag)
{
//btn[y][x].setText("");
btn[y][x].setIcon(null);
if(bombs[y][x]!=BOMB)
markers++;
}
else
{
//btn[y][x].setText("B");
btn[y][x].setIcon(flag);
if(bombs[y][x]==BOMB)
markers++;
else
markers--;
}
I added a btn[y][x].setIcon(null) to the actionlistener, which causes the flag icon to appear only briefly when left clicking but I'd rather it not appear at all.
You need to distinguish between a left mouse button click, MouseEvent.BUTTON3, and a right mouse button click, MouseEvent.BUTTON3, and then act accordingly. For example, when I did something like this, I set the "flag" boolean in my model (using MVC) via:
#Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
model.upDateButtonFlag();
}
}
The MouseListener should be used only to set or unset the flag. Otherwise you should have your JButton respond via its ActionListener for left button clicks.
Add a System.err.println("" + System.currentTimeMillis() + " " + e); to the beginning of your handler. I strongly suspect your code is being called more times than you think - as a single click can generate multiple events. Once you know what is going on, it should be easy to fix.
This is a question on how to detect which button was clicked in the MessageBox/Dialog.
GXT 2.1 or 2.2 only. Please do not answer using GXT 3.
Ideally, this is how I could do a confirm dialog.
final MessageBox box = MessageBox.confirm(
"Confirm kill avatar",
"Please remove " + getAvatar().getName(),
new Listener<MessageBoxEvent>()
{
#Override
public void handleEvent(MessageBoxEvent be)
{
Button clicked = be.getButtonClicked();
if (clicked == box.getDialog().getButtonById("yes"))
deleteAvatar();
else
Info.display("Action cancelled");
}
});
However. since box has not been defined, box.getDialog() would be NPE,
and compiler preempts that by croaking "box not initialised",
and cannot initialise because box has to be final,
box has to be final because it is used in the anon Listener class.
Instead, I have to compare buttons using the button text. Which is not i18n friendly. Very bad practice.
#Override
public void handleEvent(MessageBoxEvent be)
{
Button clicked = be.getButtonClicked();
if (clicked.getText().equals("Yes")))
deleteAvatar();
else
Info.display("Action cancelled");
}
In GXT 2.2, is this the recommended way? Or is there a better way to detect button being pressed, i18n-friendly?
I SHOULD compare buttons NOT the text of the buttons.
You can use:
if (Dialog.CANCEL.equals(be.getButtonClicked().getItemId())) {
//do action
}
Never mind.
I should simply construct my own confirm/alert/etc from Dialog and provide my own submit/cancel buttons with the appropriate listeners.
Messagebox is but a sandbox/example on how to do simple gxt dialogs.
I seem to be having a problem with JTextPane. I have extended JTextPane to render a floating image because the JTextPane icon functionality does not suit my purpose. I want the user to be able to click on the image, and have certain events performed. However, when I click on the image, even when I use evt.consume(), the caret and selection are updated in the JTextPane. I would like clicks and mouse events in general that interact with the image to not affect the caret position or selection at all. Relevant code:
public class JTextPaneImg extends JTextPane {
public JTextPaneImg(){
super();
addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
formMousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
formMouseReleased(evt);
}
});
}
private void formMousePressed(java.awt.event.MouseEvent evt) {
if (imgBound.contains(evt.getPoint())) {
evt.consume();
//Do some stuff in here to interact with the image
// but the event still undesirably interacts with selection/caret
}
}
private void formMouseReleased(java.awt.event.MouseEvent evt) {
if (imgBound.contains(evt.getPoint())) {
evt.consume();
//Do some stuff in here to interact with the image
// but the event still undesirably interacts with selection/caret
}
}
}
I have even called getMouseListeners and verified that my own mouse listener is the last in the array, I read that listeners are called from highest to lowest index, meaning if my listener calls consume, it should be the last to act on the event. Why is my mouse click event still updating the caret then? Is this a problem with the Look and Feel?
I read that listeners are called from highest to lowest index, meaning if my listener calls consume, it should be the last to act on the event
This order is not guaranteed. All that is guaranteed is that all listeners will be notified. I believe it is up to each listener to check if the event is consumed. At least that is my understanding
You might be able to use a Global Event Dispatcher to prevent the event from being dispatched to the component. Just remember that all events go through the dispatcher so the code should be efficient so you don't slow down the queue.
Or you can use the technique presented on Mouse Wheel Controller which removes all the listeners from the components and replaces it with your custom listener. You can then decide when to forward the events to the other listener.
Or maybe your image should be painted on the viewport, not the text pane.
Resolved by creating my own caret. The issue was the DefaultCaret implementation does NOT check e.isConsumed on mouse events, so you must override and add the isconsumed check. My textpane behaves as intended now that I have the following code in the constructor:
this.setCaret(new DefaultCaret() {
#Override
protected void positionCaret(MouseEvent e) {
if (!e.isConsumed()) super.positionCaret(e);
}
#Override
protected void moveCaret(MouseEvent e) {
if (!e.isConsumed()) super.moveCaret(e);
}
});
This was my fault for not fully understanding the role of the Caret in the event model. But, if you ask me this is somewhat silly. From the documentation I have read, the convention is to have all listeners only act tangibly if !e.isConsumed().