Drag drop button and repaint

Hi,

I just created an Applet in which, when you click a JButton (say A), then a new Jbutton (say B) is created on the pane.

The problem is that when I run the applet and click A, the button B does not appear. I think I may be having a problem with the paint, repaint and update method. I've been doing some research but I can't find the solution...

Soes anybody know how where in the code I should call a repaint? Any other suggestions?

Thanks!

[477 byte] By [kandua] at [2007-11-27 8:38:28]
# 1
Validate the parentparent.add(newButton);parent.validate();
crwooda at 2007-7-12 20:36:09 > top of Java-index,Desktop,Core GUI APIs...
# 2

Sorry to ask, but where in the code should I validate the parent? My code is:

package proyectoreal;

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import com.borland.jbcl.layout.*;

import javax.swing.*;

import javax.swing.border.*;

/**

*

Title:

*

Description:

*

Copyright: Copyright (c) 2007

*

Company:

* @author not attributable

* @version 1.0

*/

public class Appletprueba extends Applet {

private boolean isStandalone = false;

XYLayout xYLayout1 = new XYLayout();

TitledBorder titledBorder1;

JButton jButton2 = new JButton();

//Get a parameter value

public String getParameter(String key, String def) {

return isStandalone ? System.getProperty(key, def) :

(getParameter(key) != null ? getParameter(key) : def);

}

//Construct the applet

public Appletprueba() {

}

//Initialize the applet

public void init() {

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

public void update(Graphics g){

}

//Component initialization

private void jbInit() throws Exception {

titledBorder1 = new TitledBorder("");

this.setBackground(new Color(0, 233, 216));

this.setForeground(Color.black);

this.setVisible(true);

this.setLayout(xYLayout1);

jButton2.setText("jButton2");

jButton2.addMouseListener(new Appletprueba_jButton2_mouseAdapter(this));

this.add(jButton2, new XYConstraints(72, 27, 79, 31));

this.repaint();

}

//Get Applet information

public String getAppletInfo() {

return "Applet Information";

}

//Get parameter info

public String[][] getParameterInfo() {

return null;

}

void jButton2_mouseClicked(MouseEvent e) {

botonquesedraga ani = new botonquesedraga();

this.add(ani, new XYConstraints(50,50,79,31));

///// SHOULD I VALIDATE IT HERE?

///// botonquesedraga is a j button which I can drag, so the code is in a separate class

}

}

class Appletprueba_jButton2_mouseAdapter extends java.awt.event.MouseAdapter {

Appletprueba adaptee;

Appletprueba_jButton2_mouseAdapter(Appletprueba adaptee) {

this.adaptee = adaptee;

}

public void mouseClicked(MouseEvent e) {

adaptee.jButton2_mouseClicked(e);

}

}

Thanks in advance

kandua at 2007-7-12 20:36:09 > top of Java-index,Desktop,Core GUI APIs...
# 3
Yes.
crwooda at 2007-7-12 20:36:09 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hi crwood. Thanks for your helo, but I still have a few questions:

1. Where excatly in the code should I validate the parent, and who's parent? (the jButton's?)

2. If have another problem: when I set a bakground and I drag a jButton, it leaves a shadow or a pintig bejind where it was located before. Do you have any idea of how to avoid this rpobel? I already triend repaint, paint and updat, but if you have any otrher aiddeas I would appreciatte ir.

Thanks a lot. Regards!

kandua at 2007-7-12 20:36:09 > top of Java-index,Desktop,Core GUI APIs...
# 5

Where excatly in the code should I validate the parent,

In your event code, usually in the same place where you add or remove a component.

and who's parent?

The parent of the child component you are adding.

(the jButton's?)

Yes. Like this:

void jButton2_mouseClicked(MouseEvent e) {

botonquesedraga ani = new botonquesedraga();

this.add(ani, new XYConstraints(50,50,79,31));

this.validate();

///// SHOULD I VALIDATE IT HERE?Yes!

The validate method is a Container method and therefore can be used on AWT and

Swing containers. The revalidate method is a JComponent method and can be used on

Swing (up through j2se 1.5 anyway ?may apply to AWT someday). Validate whenever you make

changes to a containers children. It asks the container to do a new layout.

when I set a bakground and I drag a jButton, it leaves a shadow or a pintig bejind

where it was located before. Do you have any idea of how to avoid this rpobel

I'm not sure I understand exactly what you are doing. So I'll make up an example using

JLabels instead of JButtons. JButtons consume MouseEvents which is awkward for drag_n_drop

operations. Press the jButton2 to add a label to the centerPanel and then you can click

and drag the label. I've used a Swing/light-weight components for this. I'm not sure if

you would get the affect you mentioned if you used AWT/heavy-weight components. Otherwise,

I haven't a clue.

// <applet code="AppletpruebaRx" width="400" height="400"></applet>

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import javax.swing.*;

import javax.swing.event.MouseInputAdapter;

public class AppletpruebaRx extends Applet implements ActionListener {

private boolean isStandalone = false;

CenterPanel centerPanel;

public void init() {

try {

jbInit();

} catch(Exception e) {

e.printStackTrace();

}

}

private void jbInit() throws Exception {

JButton jButton2 = new JButton("jButton2");

jButton2.addActionListener(this);

JPanel south = new JPanel();

south.add(jButton2);

centerPanel = new CenterPanel();

setLayout(new BorderLayout());

add(centerPanel);

this.add(south, "Last");

}

public void actionPerformed(ActionEvent e) {

int count = centerPanel.getComponentCount();

JLabel label = new JLabel("Label " + (count+1), JLabel.CENTER);

label.setBorder(BorderFactory.createEtchedBorder());

centerPanel.add(label);

int x = count*50;

int y = count*50;

label.setBounds(x, y, 75, 30);

// Not needed for null layout but you might need

// this here for your Borland XYLayout.

//centerPanel.revalidate();

}

public String getParameter(String key, String def) {

return isStandalone ? System.getProperty(key, def) :

(getParameter(key) != null ? getParameter(key) : def);

}

public String getAppletInfo() { return "Applet Information"; }

public String[][] getParameterInfo() { return null; }

}

class CenterPanel extends JPanel {

public CenterPanel() {

super(null);

this.setBackground(new Color(0, 233, 216));

this.setForeground(Color.black);

PanelMouseAdapter adapter = new PanelMouseAdapter(this);

addMouseListener(adapter);

addMouseMotionListener(adapter);

}

public void setComponent(int index, int x, int y) {

Component c = getComponent(index);

c.setLocation(x, y);

repaint();

}

}

class PanelMouseAdapter extends MouseInputAdapter {

CenterPanel centerPanel;

Point offset = new Point();

int selectedIndex;

boolean dragging = false;

PanelMouseAdapter(CenterPanel cp) {

this.centerPanel = cp;

}

public void mousePressed(MouseEvent e) {

Point p = e.getPoint();

Component[] c = centerPanel.getComponents();

for(int j = 0; j < c.length; j++) {

Rectangle r = c[j].getBounds();

if(r.contains(p)) {

selectedIndex = j;

offset.x = p.x - r.x;

offset.y = p.y - r.y;

dragging = true;

break;

}

}

}

public void mouseReleased(MouseEvent e) {

dragging = false;

}

public void mouseDragged(MouseEvent e) {

if(dragging) {

int x = e.getX() - offset.x;

int y = e.getY() - offset.y;

centerPanel.setComponent(selectedIndex, x, y);

}

}

}

Message was edited by:

crwood

crwooda at 2007-7-12 20:36:09 > top of Java-index,Desktop,Core GUI APIs...
# 6

Hi crwood, thanks for all your tips, I will just try them after I finish writing you

>I'm not sure I understand exactly what you are doing

I'm working on my thesis in order to graduate as an engineer, and what I'm trying to do is sometrhing very similar to what you will find in page:

http://jdsp.asu.edu/jdsp.html (you get to that page, and click start jdsp and follow instructions. It's very easy)

It's a signals and systems simulator. But mine will have some things different from the one on that page. But anyway, it will work prettymuch the same. I need to create boxes, which the user can drag and drop, and connect, and each of this bosex will represent either a signal (sine wave, etc) or an operator (adder, delayer, etc).

Now Im't trying to work on the graphic interface, and then I will program the functions for each box. I need to show this by two weeks, so I'm really in a hurry to finish this part...

Thanks for your help, I hope you undestood, and if you have any suggestions, I will be very happy and grateful to hear it..

kandua at 2007-7-12 20:36:09 > top of Java-index,Desktop,Core GUI APIs...