开发者

Want to display JList over the JTextArea

I want to disply JList over the JTex开发者_Python百科tArea but its showing it behind the JTextArea. I have attached two images to describe my problem through images.At runtime how can we set JList over the JTextArea?

JList behind JTextArea:

Want to display JList over the JTextArea

JList over JTextArea:

Want to display JList over the JTextArea


bsm: you should not use JLists for this situation but JComboBoxes which will have drop down lists that display correctly over the JTextArea. For e.g.,

import java.awt.BorderLayout;
import javax.swing.*;

public class JComboAndJTextArea extends JPanel {

   private static final String[] ITEMS1 = {"one", "two", "three", "four", "five"};
   private static final String[] ITEMS2 = {"Monday", "Tuesday", 
      "Wednesday", "Thursday", "Friday"};

   public JComboAndJTextArea() {
      JPanel northPanel = new JPanel();
      northPanel.add(new JCheckBox("Reminder"));
      northPanel.add(new JComboBox(ITEMS1));
      northPanel.add(new JComboBox(ITEMS2));

      setLayout(new BorderLayout());
      add(northPanel, BorderLayout.NORTH);
      add(new JScrollPane(new JTextArea(8, 30)), BorderLayout.CENTER);

   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("JComboAndJTextArea");
      frame.getContentPane().add(new JComboAndJTextArea());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

I also re-recommend that you put your NetBeans code generation to the side for the time being as I honestly believe that for many it hinders their ability to learn how to code in Swing.


I would think that default behaviour is to show combo content over other components, thus you have it this way. The only suggestion that I would think of at the moment is to use layered pane.

You could check to which layer the part of combo box with options is added. Then add the list to one above.

Tutorial about LayeredPane http://download.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

From this description of RootPane I think the options of combobox must be shown on popup layer http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html

Good luck, Boro.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜