Reputation: 191
Ok, I dunno why is this happening but when I press close button, the joptionpane won't close. It keep popping up back and I need to click multiple times to close it.
Here the code snaphot
Point p;
p = onScreenLocation(0.134,0.019, eastlake);
btn.setBounds(p.x,p.y,128,96);
btn.setContentAreaFilled(false);
btn.setBorderPainted(false);
add(btn);
btn.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
Object[] options = {"View Info","View Place","Close"};
Object[] choice ={"Close"};
int response = JOptionPane.showOptionDialog(null,"Apartment Area","Message",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,ResidentImage,options,"Close");
if(response == 0 ){
JOptionPane.showOptionDialog(null, "Apartment Eastlake \n" +
"provides students with conducive room and reasonable prices ", "Message", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,ResidentImage,choice,"Close");
}
else if(response == 1){
JFrame ImageBox = new JFrame();
ImageBox.setSize(300,400);
ImageBox.add(new JLabel(ResidentImageView,SwingConstants.CENTER));
ImageBox.setVisible(true);
}
else{
}
}
});
The full code
package environment;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class Map extends JPanel{
String time = null;
serializedObject[] humans2 = null;
serializedDatas input = new serializedDatas();
Image eastlake,otherImage, buddyImage, clientImage, EventImage;
String[] buddyList;
String clientName;
JFrame frame;
ClassLoader cl = this.getClass().getClassLoader();
ImageIcon TransparentImage = new ImageIcon(cl.getResource("image1/bnt1.png"));
ImageIcon TescoImageView = new ImageIcon(cl.getResource("image1/tesco.jpg"));
ImageIcon TescoImage = new ImageIcon(cl.getResource("image1/Tesco.png"));
JButton btn = new JButton(TransparentImage);
public Map(Image map, Image agent, Image buddy, Image other, String clientName){
eastlake = map;
clientImage = agent;
otherImage = other;
buddyImage = buddy;
this.clientName = clientName;
}
protected void paintComponent(Graphics g)
{
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(eastlake,0,0,null);
this.setPreferredSize(new Dimension(2624,1696));
Font font = new Font("Helvetica", Font.BOLD, 12);
g2d.setFont(font);
g2d.setColor(Color.red);
if(time != null)
g2d.drawString(time, 10, 10);
Point p;
p = onScreenLocation(0.134,0.019, eastlake);
btn.setBounds(p.x,p.y,128,96);
btn.setContentAreaFilled(false);
btn.setBorderPainted(false);
add(btn);
btn.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me){
Object[] options = {"View Info","View Place","Close"};
Object[] choice ={"Close"};
int response = JOptionPane.showOptionDialog(null,"Apartment Area","Message",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,ResidentImage,options,"Close");
if(response == 0 ){
JOptionPane.showOptionDialog(null, "Apartment Eastlake \n" +
"provides students with conducive room and reasonable prices ", "Message", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,ResidentImage,choice,"Close");
}
else if(response == 1){
JFrame ImageBox = new JFrame();
ImageBox.setSize(300,400);
ImageBox.add(new JLabel(ResidentImageView,SwingConstants.CENTER));
ImageBox.setVisible(true);
}
else{
}
}
});
}
public void setTime(String time2) {
// TODO Auto-generated method stub
this.time = time2;
}
public void setBuddyList(String[] buddyList2) {
// TODO Auto-generated method stub
this.buddyList = buddyList2;
}
protected Point onScreenLocation(double x, double y, Image img)
{
return new Point((int)(img.getWidth(null)*x),(int)(img.getHeight(null)*y));
}
}
Edited: Posted wrong code earlier.
Upvotes: 1
Views: 3064
Reputation: 30206
First of all it's always a great idea to post a minimum code sample - especially since in lots of cases you'd find your mistake yourself that way.
But this time it seems fairly obvious:
You should add the mouseListener in your constructor and NOT every time paintComponent
is called. Otherwise you add a new listener everytime your paintComponent method is called, which can be quite a lot for a swing component (also since the JOptionPane probably hides parts of the panel, clicking ok will generate a new paintComponent call which means you've got an endless loop there).
Upvotes: 7