0

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:

enter image description here

After editing Jans code and typing "a" sowly three times:

enter image description here

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);
                }
            });
        }
    }
Andras
  • 45
  • 1
  • 6
  • Where is the window packed? BTW `val panel = new JPanel();` What is a `val`? For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Sep 04 '16 at 13:46
  • Packed: frame.pack-> for the application popup.pack -> for the JWindow popup val: as i wrote the code is written in Scala. – Andras Sep 04 '16 at 14:02
  • It seems the `popup` is not packed until a `KeyEvent` is detected. It **should** be packed before it is made visible! – Andrew Thompson Sep 04 '16 at 14:06
  • The code is in Java now. @Andrew: The location of the pack did not help. – Andras Sep 04 '16 at 14:10
  • Huh.. I'm not familiar with that particular layout, so hopefully an expert will be along soon. – Andrew Thompson Sep 04 '16 at 14:12

1 Answers1

1

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, FormLayout is a good layout; it creates layouts that are portable. Unlike old layouts, like BoxLayout or GridBagLayout, it uses dialog units instead of pixels. This way a portable layout to various screen sizes can be created. Actually, FormLayout was the first Java layout manager that enabled to create truly portable UIs. Other two that can do it are MigLayout and GroupLayout.

If you can, try using MigLayout instead. MigLayout was inspired by FormLayout and it significantly improved it. For instance, in MigLayout you use set the gaps once, whereas in FormLayout, you have to tediously take the gaps into account when doing your layout.

Corrections:

1) Call the pack() method before the setVisible() 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 BoxLayout were so simplistic that we needed them. With FormLayout and MigLayout, 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:

package com.zetcode.formlayoutex;

import com.jgoodies.forms.factories.CC;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JWindow;

public class FormLayoutEx {

    public static void main(String[] args) {

        JTextField tf = new JTextField(10);

        JFrame frame = new JFrame();
        frame.setLayout(new FormLayout("6dlu, p, 5dlu, p:g, 3dlu", 
                "6dlu, p, 4dlu, p, 4dlu, p, 6dlu"));
        frame.add(new JLabel("Before"), CC.xy(2, 2));
        frame.add(new JTextField("before"), CC.xy(4, 2));
        frame.add(tf, CC.xywh(2, 4, 3, 1));
        frame.add(new JLabel("After"), CC.xy(2, 6));
        frame.add(new JTextField("after"), CC.xy(4, 6));


        frame.pack();
        frame.setTitle("FormLayout example");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        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);
            }
        });
    }
}

Screenshot:

enter image description here

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77
  • Hi, thanks for the answer, but the problem as i mentioned if there are more components in the same line. If you modify your code like this the error still comes: frame.add(new JLabel("Middle"), CC.xy(2, 4));//new line frame.add(tf, CC.xywh(4, 4, 1, 1));//new constraint – Andras Sep 04 '16 at 19:35
  • Hi Jan, see the new screenshot in the question after the line: After editing Jans code and typing a sowly three times – Andras Sep 05 '16 at 11:47