Jump to content

Only 1 of 2 components is displayed, JFrame

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Lefty

Lefty

    Newbie

  • Members
  • Pip
  • 3 posts
I'm very new to GUI programming. Anyway:

I'm trying to place a textbox on top of a .png in a panel. I attempt to accomplish this by using JLayeredPane(). However, instead of displaying the .png file with a textbox superimposed on it, java merely displays solely the textbox. :c-blink:


import java.awt.*;

import java.awt.event.*;

import java.awt.image.*;

import java.io.*;

import javax.imageio.*;

import javax.swing.*;


public class LoadImageApp extends JFrame

{

    public static void main(String[] args)

    {

        JFrame f = new JFrame("Frame Demo");

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextField testField = new JTextField(12);

        testField.setLocation(50, 50);

        BufferedImage img = null;

        try

        {

            img = ImageIO.read(new File(

                "/Users/joshuamedlock/Java/asdf_MotionToContinue.png"));

        } catch (Exception e){}

        JLabel picLabel = new JLabel(new ImageIcon(img));


        JPanel contentPane = new JPanel(new BorderLayout());

        f.setContentPane(new JScrollPane(contentPane));


        contentPane.add(new JLayeredPane());

        contentPane.add(picLabel, null, new Integer(0));

        contentPane.add(testField, null, new Integer(1));

        f.pack();

        f.setVisible(true);

    }

}



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
This goes wrong:

contentPane.add(new JLayeredPane());

contentPane.add(picLabel, null, new Integer(0));

contentPane.add(testField, null, new Integer(1));

What happens here is:
  • You add the jlayeredPane to the JFrame's BorderLayout(default) in the BorderLayout.CENTER(also default)
  • You add the picLabel to the JFrame's BorderLayout(default) in the BorderLayout.CENTER(also default), with index 0.
  • You add the testField to the JFrame's BorderLayout(default) in the BorderLayout.CENTER(also default), with index 1.
What's wrong is that you add the picLabel and testField to the JFrame, and not to the JLayeredPane.
What you should try to do is:

JLayeredPane layerPane = new JLayeredPane();

layerPane.add(picLabel, 0);

layerPane.add(testField, 1);

contentPane.add(layerPane);



#3
Lefty

Lefty

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks for the reply. These adjustments seem appropriate to me. However, now the program displays an empty, small application window (roughly the size of my textbox) with neither component in it. When I avoid using the JLayeredPane object entirely, I'm able display both components (but of course not in an overlapping format as intended). Here is my updated code:


import java.awt.*;

import java.awt.event.*;

import java.awt.image.*;

import java.io.*;

import javax.imageio.*;

import javax.swing.*;


public class LoadImageApp extends JPanel

{

    public static void main(String[] args)

    {

        JFrame f = new JFrame("Frame Demo");

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextField testField = new JTextField("one", 12);

        testField.setBounds(50, 50, 50, 50);

        JTextField testField2 = new JTextField("two", 12);

        testField2.setBounds(200, 200, 50, 50);

        BufferedImage img = null;

        try

        {

            img = ImageIO.read(new File(

                "/Users/joshuamedlock/Java/asdf_MotionToContinue.png"));

        } catch (IOException e){}

        JLabel picLabel = new JLabel(new ImageIcon(img));


        JPanel contentPane = new JPanel();

        f.setContentPane(new JScrollPane(contentPane));


        JLayeredPane layerPane = new JLayeredPane();

        layerPane.add(picLabel, 0);

        layerPane.add(testField, 1);

        contentPane.add(layerPane);

        f.pack();

        f.setVisible(true);

    }

}



#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Override the paintComponent of your JPanel and draw your png image.
Add the JTextArea/JTextField to this JPanel and call the setOpaque( false ) to your JTextArea/JTextField.

I believe this is what you're wanting.Attached File  customJTextArea.png   118.48K   72 downloads

#5
Lefty

Lefty

    Newbie

  • Members
  • Pip
  • 3 posts

lethalwire said:

Override the paintComponent of your JPanel and draw your png image.
Add the JTextArea/JTextField to this JPanel and call the setOpaque( false ) to your JTextArea/JTextField.

I believe this is what you're wanting.[ATTACH=CONFIG]3689[/ATTACH]

That is what I'm wanting. Let me adjust my code and report back.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users