Jonas
Jonas

Reputation: 128827

How can I place a component in a JLayeredPane right below an existing component?

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

Upvotes: 4

Views: 2953

Answers (3)

mKorbel
mKorbel

Reputation: 109813

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

Upvotes: 2

toto2
toto2

Reputation: 5326

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.

Upvotes: 2

camickr
camickr

Reputation: 324118

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.

Upvotes: 4

Related Questions