import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JavaGUI extends JPanel {
private Control control = new Control();
private Keys keys = new Keys("Original starting value.");
public JavaGUI() {
this.setLayout(new GridLayout(0, 1));
this.add(keys);
this.add(control);
private class Control extends JPanel {
public Control() {
this.add(new JButton(new AbstractAction("Update") {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Command: " + e.getActionCommand());
keys.string = String.valueOf(System.nanoTime());
//keys.label.setText(keys.string); //remove comments cause button move
JButton j = (JButton) e.getSource();
j.setLocation(j.getX() + 10, j.getY() + 10);
}
}));
}
}
private class Keys extends JPanel {
private String string;
private JLabel label = new JLabel();
public Keys(String s) {
this.string = s;
label.setText(s);
this.add(label);
}
}
private void display() {
JFrame f = new JFrame("JavaGUI");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new JavaGUI().display();
}
});
}
}

- 109,525
- 20
- 134
- 319

- 86
- 1
- 8
-
1Nice, what should we do with this bad formatted code without any specific request with exception of a cryptic question title? Print it and stick it on a wall? – Jack Jan 06 '13 at 03:45
-
But how do i format this code dear. i cant understand. Isn't it good. – user1952211 Jan 06 '13 at 03:49
2 Answers
You are using a GridLayout so you can't just move components around randomly. I suggest you read the Swing tutorial on Laying Out Components Within a Container for an understanding of how layout management works.

- 321,443
- 19
- 166
- 288
-
I accidentally downvoted [your answer](http://stackoverflow.com/a/7375655/679982) will you please edit it so that I can remove the downvote, and then you remove your edit and I can upvote it (that's what I wanted to do in the first place >( ). I tried to dummy edit it myself but it got rejected – linski Jan 08 '13 at 18:42
I'll give all. The error was, that you set the String string
, not the JLabel label
.
Such behaviour, of something not having a GUI effect, could also have been caused by a missing invokeLater
in actionPerformed
.
You made classes for the used objects to set specific properties. That is a burocratic waste, hence I have rewritten it a bit.
If you resize the window, you'll see that the GridLayout manager again layouts, so that the JButton appears at its original position. You can use absolute positionwing with setLayout(null)
using individual setLocation
s.
public class JavaGUI extends JPanel {
private JPanel control;
private JLabel keys;
public JavaGUI() {
setLayout(new GridLayout(0, 1));
keys = new JLabel("Original starting value.");
keys.setHorizontalAlignment(SwingConstants.CENTER);
add(keys);
control = new JPanel();
control.setPreferredSize(new Dimension(200, 100));
control.setMinimumSize(new Dimension(200, 100));
control.setLayout(null);
JButton button = new JButton(new AbstractAction("Update") {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Command: " + e.getActionCommand());
keys.setText(String.valueOf(System.nanoTime()));
//keys.label.setText(keys.string); //remove comments cause button move
JButton j = (JButton) e.getSource();
j.setLocation(j.getX() + 10, j.getY() + 10);
}
});
button.setBounds(10, 10, 90, 24);
control.add(button);
add(control);
}
public static void main(String[] args) {
final JFrame f = new JFrame("JavaGUI");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JavaGUI());
f.pack();
f.setLocationRelativeTo(null);
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
f.setVisible(true);
}
});
}
}
P.S. I have seen considerably worse code. This is actually neat code with only the superfluous classes.

- 107,315
- 7
- 83
- 138
-
Your suggestion didn't work also. Now time ischanging bt button is not moving dude. – user1952211 Jan 09 '13 at 04:37
-
Also with invokeLater inside actionPerformed? Under Linux the code works as given. LookAndFeel dependent maybe. – Joop Eggen Jan 09 '13 at 06:57
-
But in my lapy it doesn't work. so what should I do. its imp 4 me to do this – user1952211 Jan 09 '13 at 16:23
-