By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,939 Members | 1,602 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,939 IT Pros & Developers. It's quick & easy.

Placing components on a background image.

P: 67
Alright I'm trying to place the textarea on top of the image but right now it displays the textarea below the image, how do i go about doing this.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.Color;
  2. import java.awt.FlowLayout;
  3. import javax.swing.ImageIcon;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JPanel;
  7. import javax.swing.JTextArea;
  8.  
  9. /**
  10.  *
  11.  * @author Hanshawc
  12.  */
  13. public class MyDesk extends JFrame
  14. {
  15.    private JPanel panel;
  16.    private JTextArea questionBox;
  17.  
  18.    public static void main(String[] args)
  19.    {
  20.        MyDesk myDesk = new MyDesk();
  21.    }
  22.  
  23.    public MyDesk()
  24.    {
  25.        this.setTitle("MyDesk Alpha Version: 0.1");
  26.        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  27.        this.setSize(660,525);
  28.        this.setResizable(false);
  29.        this.add(getMainPanel());
  30.        this.setVisible(true);
  31.    }
  32.  
  33.    private JPanel getMainPanel()
  34.    {
  35.        panel = new JPanel();
  36.        panel.setSize(640,480);
  37.        panel.setBackground(Color.WHITE);
  38.        panel.setLayout(new FlowLayout());
  39.        panel.add(getBackgroundImage());
  40.        panel.add(getQuestionBox());
  41.        return panel;
  42.    }
  43.  
  44.    private JLabel getBackgroundImage()
  45.    {
  46.        ImageIcon titleIcon = new
  47.        ImageIcon(getClass().getResource("background.jpg"));
  48.        JLabel titleLabel = new JLabel();
  49.        titleLabel.setIcon(titleIcon);
  50.        return titleLabel;
  51.    }
  52.  
  53.    private JTextArea getQuestionBox()
  54.    {
  55.         questionBox = new JTextArea(getQuestion());
  56.         questionBox.setColumns(20);
  57.         return questionBox;
  58.    }
  59.  
  60.    private String getQuestion()
  61.    {
  62.        String question = "Test question";
  63.        return question;
  64.    }
  65. }
  66.  
Mar 9 '09 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
See the API specs of the FlowLayout that you are using to find out why that is happening. You could use a GridLayout(2, 1) and add the TextArea first.
Mar 10 '09 #2

Expert 10K+
P: 11,448
@r035198x
My guess is that the OP means "on top" in the third dimension, i.e. in front of the image. A JDesktopPane can do it or else the GlassPane can be of help: i.e. it is in front of everything else.

kind regards,

Jos
Mar 10 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.