4

I have a JTextField, and right below it I want to show a JLabel placed in a JLayeredPane (I will use it for autosuggestions later on).

How can I place my JLabel in JLayeredPane right below the JTextField?

Here is some code I have, and the current result shown in the screenshot below:

public static void main(String[] args) {

    JTextField field = new JTextField();
    JLabel lbl = new JLabel("Hello");
    lbl.setBackground(Color.YELLOW);
    lbl.setOpaque(true);

    JLayeredPane layeredPane = new JLayeredPane();
    layeredPane.setLayout(new GridLayout(0,1));
    layeredPane.add(lbl, (Integer) (JLayeredPane.POPUP_LAYER - 10));
    layeredPane.setPreferredSize(field.getPreferredSize());

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(field, BorderLayout.NORTH);
    panel.add(layeredPane, BorderLayout.SOUTH);

    JFrame frame = new JFrame();
    frame.add(panel);
    frame.setSize(200, 360);
    frame.setVisible(true);
}

enter image description here

Second try:

public static void main(String[] args) {

    JTextField field = new JTextField();
    JLabel lbl = new JLabel("Hello");
    lbl.setBackground(Color.YELLOW);
    lbl.setOpaque(true);
    lbl.setBounds(field.getBounds().x, field.getBounds().y, 
        field.getBounds().width, field.getBounds().height);

    JPanel popPanel = new JPanel(new BorderLayout());
    popPanel.add(lbl, BorderLayout.NORTH);
    popPanel.setLocation(field.getLocation().x+10, field.getLocation().y+20);
    popPanel.setPreferredSize(field.getPreferredSize());

    JFrame frame = new JFrame();

    JLayeredPane layeredPane = frame.getRootPane().getLayeredPane();
    layeredPane.setLayout(new GridLayout(0,1));
    layeredPane.add(popPanel, (Integer) (JLayeredPane.POPUP_LAYER - 10));

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(field, BorderLayout.NORTH);

    frame.add(panel);
    frame.setSize(200, 360);
    frame.setVisible(true);
}

enter image description here

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • please read http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post – mKorbel Aug 19 '11 at 18:20

3 Answers3

4

Add the layeredPane to the "CENTER", not the SOUTH.

However, your understanding a layed pane seems to be a little confused. You use a layered pane when you want multiple components to be displayed on top (stacked?) of one another. You are still using the layered pane in 2 dimensions which is unnecessary. YOu can just use a panel for this.

If you want to popup a list of suggestions then you should just use a JPopupMenu and position it below the text field. Read the section from the Swing tutorial on Bringing up Popup Menus.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I do want the layered content to be displayed on top of others, I'm not trying to use it in 2 dimensions. Popup menu may be a solution, but I want to use custom content on my popup e.g. multi-line text. – Jonas Aug 19 '11 at 16:32
  • @Jonas, you can add any component to a JPopupMenu. – camickr Aug 19 '11 at 16:39
  • @camickr as I know that without/not JComboBox, this Bug is stil here http://stackoverflow.com/questions/6559275/jcombobox-on-a-jpopupmenu – mKorbel Aug 19 '11 at 17:20
  • @mKorbel, thanks for the info. I guess it makes sense that you can't have two popups together unless of course you manage the popup the way the popup menu manage child popups. – camickr Aug 19 '11 at 17:53
  • `manage the popup the way the popup menu manage child popups` that right, btw back to your post, good idea, most complex, maybe could be saved lots of useless coding for built some Model for others JComponent(s), by this reason was up-voted – mKorbel Aug 19 '11 at 18:11
2

First of all, I don't think you should use a JLayeredPane for that, but just a permanent label.

If you do use a layered pane, you'll have to compute where the text field ends (y = field.getY() + field.getHeight()) and set your JPanel at 'panel.setLocation(0, y)' inside the JLayeredPane (provided the JLayeredPane has the same starting position as the underlying JFrame). You could equivalently position the JLayeredPane at (0, y) and put the label at (0, 0) within that layered pane.

You have to make sure this is done every time the components are resized.

toto2
  • 5,306
  • 21
  • 24
  • A permanent JLabel can't be shown above other components, so I think JLayeredPane is better. I tried to use `setLocation()` on the panel in my new example, but I can't get it working. – Jonas Aug 19 '11 at 16:24
2

why not using AutoComplete ComboBox / JTextField and if you don't want to display JComboBox, then there is AutoCompleted JTextField, and for somehow reduced autosuggestions, would be better look for undecorated JDialog/Window with JTable with one TableColum and without TableHeaded in the JScrollPane, just with plain RowSorter, very simle job

mKorbel
  • 109,525
  • 20
  • 134
  • 319