I new here, and I'm a beginner about developing. My problem is that I have couple of JTextField
controls inside a JPanel
which is inside a JFrame
too, and the text field doesn't show the text that I pass by a method, and I don't know why, because the source for that is really simple.
I want to show the coordinates of the mouse of a canvas that I get in a pair of text fields. That canvas is inside a frame like a panel which contain the couple of text fields. I'm going to put here the source of a canvas which in a MouseMotionListener
is refer.
public void addPosicionPuntero(){
addMouseMotionListener(new MouseMotionAdapter(){
@Override
public void mouseMoved(MouseEvent evento){
x1 = evento.getX();
y1 = evento.getY();
updateUI();
panelCoordenadas pC = new panelCoordenadas();
pC.mostrarCoordenadas(x1,y1);
System.out.println(x1 + " --- " + y1);
}
@Override
public void mouseDragged(MouseEvent evento){
mouseMoved(evento);
}
});
}
well, now in other class the panel which includes the text field.
public class panelCoordenadas extends javax.swing.JPanel{
JTextField txfX = new JTextField("X");
JTextField txfY = new JTextField("Y");
public panelCoordenadas() {
this.setSize(100, 100);
//this.setBounds(60,50,100,60);
this.setLocation(50, 50);
this.setBackground(Color.yellow);
JLabel coordX = new JLabel("coordX");
JLabel coordY = new JLabel("coordY");
add(coordX);
add(coordY);
txfX.setEditable(false);
txfY.setEditable(false);
txfX.setSize(40, 20);
txfY.setSize(40, 20);
add(txfX);
add(txfY);
}
public void mostrarCoordenadas(int x, int y){
txfX.setText(String.valueOf(x));
txfY.setText(String.valueOf(y));
System.out.println("mostrarCoordenadas = " + x + " --- " + y);
updateUI();
}
}
I put a System.out to know if its gets the data. The data shows by screen but not for the text field.
If anybody knows what's the problem I'd glad to tell me.
Class names should NOT start with a lower case character. Have you seen a class in the JDK that does? Learn by example.
So "panelCoordenadas" should be PanelCoordenadas
.
updateUI();
That is not needed. That method is used internally by Swing when the LAF changes. There is no need for you to invoke it in application code.
JLabel coordX = new JLabel("coordX");
JLabel coordY = new JLabel("coordY");
add(coordX);
add(coordY);
In the constructor of your class you create components and add the components to the panel. That is good. So now your panel can display the components.
In your MouseListener you have:
panelCoordenadas pC = new panelCoordenadas();
pC.mostrarCoordenadas(x1,y1);
So you create a panel but do nothing with the panel. The panel is just sitting in memory. You don't add it to the frame. This approach is wrong.
Don't keep creating a new instance of your PanelCoordenadas
class.
Instead in your main class where you create your other components, you create an instance of the PanelCoodenadas
class and add it to the frame. If you want to update the text fields of that instance, then you need to pass that variable as a parameter to the class where you define your MouseListener.