I have a problem with swing that I cannot resolve since a whole day.
I want to show a popup in JWindow if someone types into a JTextField.
But if the layout uses a JGoodies FormLayout with more components in one row then the display is going to be corrupt.
Do you have any ideas?
Screenshot after typing some letters into the second text field:
After editing Jans code and typing "a" sowly three times:
Code in Java:
package eu.eyan;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JWindow;
import com.jgoodies.forms.factories.CC;
import com.jgoodies.forms.layout.FormLayout;
public class Snippet {
public static void main(String[] args) {
JTextField tf = new JTextField(10);
JPanel panel = new JPanel();
panel.setLayout(new FormLayout("p", "p,p,p"));
panel.add(new JTextField("before"), CC.xy(1, 1));
panel.add(tf, CC.xy(1, 2));
panel.add(new JTextField("after"), CC.xy(1, 3));
JFrame frame = new JFrame();
frame.setLayout(new FormLayout("p,p,p", "p"));
frame.add(new JLabel("bef"), CC.xy(1, 1));
frame.add(panel, CC.xy(2, 1));
frame.add(new JLabel("aft"), CC.xy(3, 1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
JWindow popup = new JWindow();
popup.setLayout(new FormLayout("p", "p"));
JLabel l = new JLabel("popup");
popup.add(l, CC.xy(1, 1));
popup.pack();
tf.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
popup.setVisible(true);
}
});
}
}


The best way how to get help with a layout is providing a sketch of your UI, either as picture or as ASCII. Here I am only guessing what you had in mind.
First,
FormLayoutis a good layout; it creates layouts that are portable. Unlike old layouts, likeBoxLayoutorGridBagLayout, it uses dialog units instead of pixels. This way a portable layout to various screen sizes can be created. Actually,FormLayoutwas the first Java layout manager that enabled to create truly portable UIs. Other two that can do it areMigLayoutandGroupLayout.If you can, try using
MigLayoutinstead.MigLayoutwas inspired byFormLayoutand it significantly improved it. For instance, inMigLayoutyou use set the gaps once, whereas inFormLayout, you have to tediously take the gaps into account when doing your layout.Corrections:
1) Call the
pack()method before thesetVisible()method.2) Don't use unnecessary panels to create the layout. You probably saw some examples where panels were used to create the layout. This was because managers like
BoxLayoutwere so simplistic that we needed them. WithFormLayoutandMigLayout, this is not necessary.3) You also need to add gaps to your layout in dialogs units.
Here is an example of what I thought you might want to achieve:
Screenshot: