Question

In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged.

/// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler.

*************************ButtonHandler class************************************

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ButtonHandler extends JFrame implements ActionListener {
  
private final JButton acceptbutton1;
private final JButton cancelbutton1;
private final JPanel buttonPanel;
private final JPanel mainPanel;
  
public ButtonHandler() {
  
super();
  
mainPanel = new JPanel();
buttonPanel = new JPanel(new FlowLayout());
  
cancelbutton1 = new JButton("Cancel");
acceptbutton1 = new JButton("Accept");
  
mainPanel.add(buttonPanel);
  
add(mainPanel);
  
cancelbutton1.addActionListener(this);
acceptbutton1.addActionListener(this);
  
  
}
  
public JButton getAcceptbutton1() {
return acceptbutton1;
}
  
public JButton getCancelbutton1() {
return cancelbutton1;
}

  
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == acceptbutton1)
////// AND && dragged the mouse
/////THEN
JOptionPane.showMessageDialog(this,"Nice drawing");
////ELSE
JOPtionPane.showMessageDialog(this,"Please try again");
  
if(e.getSource() == cancelbutton1)
JOptionPane.showConfirmDialog(this, "You pressed Cancel button");
}
}
***********************Painter main class************************************


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.*;


public class Painter {


public static void main(String[] args) {

  
JFrame application = new JFrame();

PaintPanel paintPanel = new PaintPanel();
application.add(paintPanel, BorderLayout.CENTER);
  

application.add(new JLabel("Draw"),
BorderLayout.NORTH);
  
  
ButtonHandler buttonHandler = new ButtonHandler();
  
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(buttonHandler.getAcceptbutton1());
panel.add(buttonHandler.getCancelbutton1());
application.add(panel,BorderLayout.SOUTH);

application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setLocationRelativeTo(null);
application.setSize(600, 400);
application.setVisible(true);
}
}
**********************Point class, for the graphics in PaintPanel************************


public class Point {
  
public int x;
public int y;
  
public Point() {
this.x = 0;
this.y = 0;
  
}
public Point (int x, int y) {
this.x = x;
this.y = y;
  
}
public int getX() {
return x;
}
public int getY() {
return y;
}
  
}
******************PaintPanel*******************************************************

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JPanel;


public class PaintPanel extends JPanel {
  
private final ArrayList<Point> points = new ArrayList<>();
  

  
public PaintPanel() {
  
addMouseMotionListener(
new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent event) {

points.add(event.getPoint());
repaint();
}
});
  
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Point point : points ) {
g.fillRect(point.x, point.y, 4,6);
  

}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the solution:

changed the code of ButtonHandler.java and the PaintPanel.java some of the code to checked the mouse is dragged or not.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ButtonHandler extends JFrame implements ActionListener {

   private final JButton acceptbutton1;
   private final JButton cancelbutton1;
   private final JPanel buttonPanel;
   private final JPanel mainPanel;

   public ButtonHandler() {

       super();

       mainPanel = new JPanel();
       buttonPanel = new JPanel(new FlowLayout());

       cancelbutton1 = new JButton("Cancel");
       acceptbutton1 = new JButton("Accept");

       mainPanel.add(buttonPanel);

       add(mainPanel);

       cancelbutton1.addActionListener(this);
       acceptbutton1.addActionListener(this);

   }

   public JButton getAcceptbutton1() {
       return acceptbutton1;
   }

   public JButton getCancelbutton1() {
       return cancelbutton1;
   }

   @Override
   public void actionPerformed(ActionEvent e) {
       if (e.getSource() == acceptbutton1) {
           if (PaintPanel.p == true) //check for the PaintPanel is draw a paint on the panel or not and display a message
               JOptionPane.showMessageDialog(this, "Nice drawing");
           else
               JOptionPane.showMessageDialog(this, "Please try again...");
       }

       if (e.getSource() == cancelbutton1)
           JOptionPane.showConfirmDialog(this, "You pressed Cancel button");
   }
}

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JPanel;

public class PaintPanel extends JPanel {
   static boolean p=true; //declare static variable to check the paintComponent ork changed the value to true
   private final ArrayList<Point> points = new ArrayList<>();

   public PaintPanel() {

       addMouseMotionListener(new MouseMotionAdapter() {
           @Override
           public void mouseDragged(MouseEvent event) {
               points.add(event.getPoint());
               repaint();
           }
       });

   }

   @Override
   public void paintComponent(Graphics g) {
       super.paintComponent(g);
       for (Point point : points) {
           g.fillRect(point.x, point.y, 4, 6);
           p=false; //changed the value to true
       }
   }
}

sample output:

Add a comment
Answer #2

To implement different

To implement different showDialog messages in your ButtonHandler class based on whether the mouse has been dragged or not, you can make use of a boolean variable to track the dragging state. Here's an updated version of your code with the necessary modifications:

javaCopy codeimport java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;public class ButtonHandler extends JFrame implements ActionListener {  
  private final JButton acceptbutton1;  private final JButton cancelbutton1;  private final JPanel buttonPanel;  private final JPanel mainPanel;  private boolean mouseDragged; // Track dragging state

  public ButtonHandler() {    super();
    mainPanel = new JPanel();
    buttonPanel = new JPanel(new FlowLayout());
    cancelbutton1 = new JButton("Cancel");
    acceptbutton1 = new JButton("Accept");
    mainPanel.add(buttonPanel);
    add(mainPanel);

    cancelbutton1.addActionListener(this);
    acceptbutton1.addActionListener(this);    // Initialize dragging state to false
    mouseDragged = false;
  }  public JButton getAcceptbutton1() {    return acceptbutton1;
  }  public JButton getCancelbutton1() {    return cancelbutton1;
  }  @Override
  public void actionPerformed(ActionEvent e) {    if (e.getSource() == acceptbutton1) {      if (mouseDragged) { // Check dragging state
        JOptionPane.showMessageDialog(this, "Nice drawing");
      } else {
        JOptionPane.showMessageDialog(this, "Please try again");
      }
    }    if (e.getSource() == cancelbutton1) {
      JOptionPane.showConfirmDialog(this, "You pressed Cancel button");
    }
  }  // Setter method to update dragging state
  public void setMouseDragged(boolean mouseDragged) {    this.mouseDragged = mouseDragged;
  }
}

In the PaintPanel class, you need to add a method to update the dragging state in the ButtonHandler class. Here's the modified PaintPanel class:

javaCopy codeimport java.awt.Graphics;import java.awt.Point;import java.awt.event.MouseMotionAdapter;import java.awt.event.MouseEvent;import java.util.ArrayList;import javax.swing.JPanel;public class PaintPanel extends JPanel {  private final ArrayList<Point> points = new ArrayList<>();  private final ButtonHandler buttonHandler; // Reference to ButtonHandler

  public PaintPanel(ButtonHandler buttonHandler) {    this.buttonHandler = buttonHandler;
    addMouseMotionListener(new MouseMotionAdapter() {      @Override
      public void mouseDragged(MouseEvent event) {
        points.add(event.getPoint());
        buttonHandler.setMouseDragged(true); // Update dragging state
        repaint();
      }
    });
  }  @Override
  public void paintComponent(Graphics g) {    super.paintComponent(g);    for (Point point : points) {
      g.fillRect(point.x, point.y, 4, 6);
    }
  }
}

And finally, in your Painter class, you need to pass the ButtonHandler instance to the PaintPanel constructor. Here's the updated Painter class:

javaCopy codeimport java.awt.BorderLayout;import java.awt.FlowLayout;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class Painter {  public static void main(String[] args) {    JFrame application = new JFrame();    PaintPanel paintPanel = new PaintPanel(new ButtonHandler());
    application.add(paintPanel, BorderLayout.CENTER);
    application.add(new JLabel("Draw"), BorderLayout.NORTH);    ButtonHandler buttonHandler = (ButtonHandler) paintPanel.buttonHandler;
    JPanel


answered by: Hydra Master
Add a comment
Know the answer?
Add Answer to:
In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Swing File Adder: Build a GUI that contains an input file, text box and Infile button....

    Swing File Adder: Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile if not already written that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file.  Program...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • Java question so right now this code is work perfectly the problem is I also want...

    Java question so right now this code is work perfectly the problem is I also want to know if possible to get the X and Y value outside the JFrame? import java.awt.FlowLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JLabel; public class MouseEventDemo extends JFrame implements MouseListener { //It is a class which generate a JFrame and implements MouseListener interface //These all are text that show x , y coordinates on mouse click event JLabel lclick; JLabel lpressed; JLabel lreleased;...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • Need help writing Java code for this question. CircleFrame class is provided below. what happen after...

    Need help writing Java code for this question. CircleFrame class is provided below. what happen after u add the code and follow the requirement? It would be nice to be able to directly tell the model to go into wait mode in the same way that the model's notify method is called. (i.e. notify is called directly on the model, whereas wait is called indirectly through the suspended attribute.) Try altering the the actionPerformed method of the SliderListener innner class...

  • Can you please help me to run this Java program? I have no idea why it...

    Can you please help me to run this Java program? I have no idea why it is not running. import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @SuppressWarnings({"unchecked", "rawtypes"}) public class SmartPhonePackages extends JFrame { private static final long serialVersionID= 6548829860028144965L; private static final int windowWidth = 400; private static final int windowHeight = 200; private static final double SalesTax = 1.06; private static JPanel panel;...

  • please help me debug this Create a GUI for an application that lets the user calculate...

    please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....

  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

  • How can i make the java class seperate into an MVC pattern? public class ChatView implements Acti...

    How can i make the java class seperate into an MVC pattern? public class ChatView implements ActionListener {    public static void main(String[] args)    {        JFrame chatFrame = new JFrame();        JFrame chatFrame1 = new JFrame();        JFrame chatFrame2 = new JFrame();               JPanel chatPanel = new JPanel();        JPanel chatPanel1 = new JPanel();        JPanel chatPanel2 = new JPanel();        chatFrame.setContentPane(chatPanel);        chatFrame1.setContentPane(chatPanel1);        chatFrame2.setContentPane(chatPanel2);        chatFrame.setLayout(new FlowLayout());        chatFrame1.setLayout(new FlowLayout());        chatFrame2.setLayout(new FlowLayout());        JTextField chatWrite = new JTextField();        JTextField chatWrite1 = new JTextField();        JTextField chatWrite2 = new JTextField();        JTextArea chatDisplay...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT