0

I have a JPanel cPanel, and a TextField resultField imported from JDK 8's javafx.scene.control.TextField. Now I want to cPanel.add(resultField,BorderLayout.NORTH), Eclipse give me an error:

The method add(String, Component) in the type Container is not applicable for the arguments (TextField, String)

How do I add resultField to cPanel properly?

Dylan Czenski
  • 1,305
  • 4
  • 29
  • 49
  • JPanel is a swing component. It would make more sense to add a swing JTextField than the JavaFX TextField. – WillShackleford Nov 14 '15 at 02:03
  • @willshackford thanks. That does make sense. I wanted to add prompt text to resultField, but JTextField doesn't support prompt text – Dylan Czenski Nov 14 '15 at 02:08
  • Then , Its better to switch to javafx instead of using java swing. – Madushan Perera Nov 14 '15 at 02:09
  • SwingLabs SwingX library has `Prompt` (and `Buddy`) support as demonstrated [here](http://stackoverflow.com/questions/22396282/how-to-set-text-like-placeholder-in-jtextfield-in-swing/22396303#22396303) – MadProgrammer Nov 14 '15 at 02:23

2 Answers2

2

but JTextField doesn't support prompt text

You can use the Text Prompt class.

I allows you to specify a prompt that is displayed when the text field is empty. As soon as you type text the prompt is removed.

The prompt is actually a JLabel so you can customize the font, forground etc..:

JTextField tf7 = new JTextField(10);
TextPrompt tp7 = new TextPrompt("First Name", tf7);
tp7.setForeground( Color.RED );
camickr
  • 321,443
  • 19
  • 166
  • 288
2

You can Download swingx-core-1.6.2.jar and use it to make swing Text Fields Prompt text.

    PromptSupport.setPrompt("User ID", userNameField);
    PromptSupport.setFontStyle(Font.BOLD, userNameField);
    PromptSupport.setForeground(Color.BLACK, userNameField);
    PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT,
            userNameField);

Download it from here :http://www.java2s.com/Code/JarDownload/swingx/swingx-core-1.6.2.jar.zip

Madushan Perera
  • 2,568
  • 2
  • 17
  • 36