I'm using Java in Netbeans to draw a weighted binary tree. on mouseClick event, a node is drawn, and pressing and releasing the mouse in different points on the pane creates an edge, immediately after which i need an input dialog to pop up and ask the user to input the weight of the edge. This is my code. Don't bother going through the entire code, though.
public class panel1 extends JPanel implements MouseListener,ActionListener
{
nodeClass[] nodes = new nodeClass[20];
int nodeRadius = 10;
int ctr = 0;
int[] x=new int[100];
int[] y=new int[100];
int[] oldx=new int[100];
int[] oldy=new int[100];
int[] newx=new int[100];
int[] newy=new int[100];
int edges=0;
int[] edgeWeight = new int[100];
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GREEN);
if(x[ctr]!=0&&y[ctr]!=0)
{
for(int i=0;i<=ctr;i++){
g.setColor(Color.GREEN);
g.fillOval(x[i]-25, y[i]-45, 40, 40);
g.setColor(Color.BLACK);
g.drawString(String.valueOf(i+1),x[i]-10,y[i]-25);
}
for(int i=0;i<edges;i++){
g.setColor(Color.GREEN);
g.drawLine(oldx[i]-10,oldy[i]-30,newx[i]-10,newy[i]-30);
g.drawString(String.valueOf(edgeWeight[i]), (oldx[i]+newx[i])/2, (oldy[i]+newy[i])/2);
}
ctr++;
}
if(oldx[edges]!=0&&oldy[edges]!=0&&newx[edges]!=0&&newy[edges]!=0&&(newx[edges]!=oldx[edges] && newy[edges]!=oldy[edges]))
{
edgeWeight[edges] = Integer.ParseInt(JOptionPane.showInputDialog("Enter Weight Of Edge : "));
for(int i=0;i<ctr;i++){
g.setColor(Color.GREEN);
g.fillOval(x[i]-25, y[i]-45, 40, 40);
g.setColor(Color.BLACK);
g.drawString(String.valueOf(i+1),x[i]-10,y[i]-25);
}
for(int i=0;i<=edges;i++){
g.setColor(Color.GREEN);
g.drawLine(oldx[i]-10,oldy[i]-30,newx[i]-10,newy[i]-30);
g.drawString(String.valueOf(edgeWeight[i]), (oldx[i]+newx[i])/2, (oldy[i]+newy[i])/2);
}
edges++;
}
}
@Override
public void mouseClicked(MouseEvent me) {
x[ctr]=me.getX();
y[ctr]=me.getY();
repaint();
}
@Override
public void mousePressed(MouseEvent me) {
oldx[edges]=me.getX();
oldy[edges]=me.getY();
}
@Override
public void mouseReleased(MouseEvent me) {
newx[edges]=me.getX();
newy[edges]=me.getY();
repaint();
}
@Override
public void mouseEntered(MouseEvent me) {;
}
@Override
public void mouseExited(MouseEvent me) {;
}
@Override
public void actionPerformed(ActionEvent ae) {
String str=ae.getActionCommand();
System.out.println(str);
}
}
The problem arises that, every time i click or drag, an infinite number of JOptionPane input dialogues pop open, and i have no option but to terminate the program.
I haven't put that bit of code under any loop. But maybe it's because it's in the paintComponent method. Any way to eradicate this problem? Or can i use some other way to get the user to input the weight of the edges?
The problem arises that, every time i click or drag, an infinite number of JOptionPane input dialogues pop open
Don't display the JOptionPane dialog in the paintComponent() method. Painting methods are for painting only.
The option pane should be displayed on your mousePressed or mouseReleased code depending on your exact requirement.