0

So I've been working on this for 2 days now, and I've gotten the basics of java.swing down (I hope, at least, as I started learning it to 2 days ago.) Anyhow, I successfully loaded a background image, but I can't seem to get the foreground one to work. I'm not sure what code you'll need, so I'll post all of it. And while you take a look at it, did I set up my JPanels correctly? (specifically the allContent and fourRows ones.)

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.util.*;
import junit.framework.Test;


public class MainFrame {


   //Variables
   private static final String IMAGE_PATH = "imageFolder/warlordsOrganizerBackground.png";
   private static final String IMAGE_PATH2 = "imageFolder/warlordsLogo.png";
   public static JFrame programFrame;
   public static JLabel warlordsBackground;
   public static JLabel warlordsLogo;
   public static JPanel allContent;
   public static JPanel fourRows;
   public static JScrollPane scrollPane;



   //Making the parts for the GUI
   public static void createGUI(){

  //programFrame Title and Layout
  programFrame = new JFrame("Warlords Organizer");
  programFrame.setLayout(new BorderLayout());

  Icon backgroundIcon = new ImageIcon(IMAGE_PATH);
  warlordsBackground = new JLabel(backgroundIcon);

  File imageFile = new File(IMAGE_PATH);
  File imageFile2 = new File(IMAGE_PATH2);

  //Warlords Logo JLabel
  Icon logoIcon = new ImageIcon(IMAGE_PATH2);
  warlordsLogo = new JLabel(logoIcon);

  //New JPanel for GridLayout 
  fourRows = new JPanel(new GridLayout(0,4));
  fourRows.setLayout(new GridLayout());

  //Makes the Initial BorderLayout (Using allContent JPanel)
  allContent = new JPanel();
  allContent.setLayout(new BorderLayout());
  allContent.add(warlordsLogo, BorderLayout.NORTH);
  allContent.setVisible(true);
  allContent.add(fourRows, BorderLayout.CENTER);

  //Add ScrollPane / MAKE SURE TO ADD TO new JScrollPane WHERE IT NEEDS TO BE / TEXT
  scrollPane = new JScrollPane(allContent);
  scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  scrollPane.setOpaque(false);
  scrollPane.getViewport().setOpaque(false);

  //JFrame programFrame Constructors
  programFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  programFrame.setContentPane(warlordsBackground);
  programFrame.pack();
  programFrame.setVisible(true);
  programFrame.setResizable(false);


  } // public static void createGUI() Closing

  public static void main(String[] args) {
  javax.swing.SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        createGUI();
     } //public void run() Closing
  });
 }

 }

And finally, my JScrollPane. I need to do something with that, so don't mind that. I set up my project files like this, if it helps any -

enter image description here

After I get my image working, I'll need to figure out how to get custom fonts (the BEBAS__.ttf) so if you also have some resources for that, I'd appreciate it.

Charles
  • 50,943
  • 13
  • 104
  • 142
Hathor
  • 187
  • 1
  • 2
  • 10
  • 1
    How is this question different then the one you asked 2 days ago? You got working code then, so why didn't you start with the working code? This wastes the time of people answering this question who by the way are making the same suggestion you got in the last posting. – camickr May 02 '13 at 04:53
  • If you noticed, a lot of my code is based off of the answer from my question 2 days ago. This is a different problem I had, as this one is mainly me not being able to get the second image working... – Hathor May 02 '13 at 04:58
  • That is the problem it is based on the solution, but you didn't follow the solution completely and now you are asking us to spend time debugging the code for you. For example you didn't set the layout of your background image. You didn't set component non-opaque as was done in the other example. – camickr May 02 '13 at 05:06
  • I looked at the other example again, yeah. I apologize for the time wasted. :/ – Hathor May 02 '13 at 05:08

1 Answers1

1

The way I see it (assuming the images are loading properly)...

You create the background image...

warlordsBackground = new JLabel(backgroundIcon);

And a little later, you set it as the content pane of the frame...

programFrame.setContentPane(warlordsBackground);

But you add nothing to it or the frame...

Now, the minor problem you may have, is JLabel doesn't actually have a layout manager, so even if you did add anything to it, nothing would show up...

Try doing something more like...

programFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
programFrame.setContentPane(warlordsBackground);
programFrame.setLayout(new BorderLayout());
programFrame.add(scrollPane);
programFrame.pack();
programFrame.setVisible(true);
programFrame.setResizable(false);

Additional

Now, based on your code, you seem to want to make the scroll pane transparent, but everything your adding to the scroll pane isn't. fourRows and allContent are both opaque...

enter image description hereenter image description here

The two images above are without scroll pane and with scroll pane. As you can see, the second image appears beneath the scroll pane (or through it).

The problem you have is the both fourRows and allContent are opaque (not transparent), meaning that when you add them to the scroll pane, despite the fact the scroll pane and view port are transparent, fourRows and allContent are going to block it.

You need to set fourRows and allContent to be transparent (setOpaque(false))

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • So now here is where the problem arises. That code allows the scrollPane and the warlordsLogo to work, but then in turn disables the background image. Also, I'm going to try, rather than making my scrollPane transparent, to make a custom look for it. The images are loading properly. – Hathor May 02 '13 at 04:25
  • See the "additional" section...You could also use a different layout manager – MadProgrammer May 02 '13 at 04:27
  • May you rephrase your Additional section? For now, disregard my scrollPane, I'll remove that for now if it's a hassle. The main point is that I'd like to get both images I have working at the same time, and that's not working. The JScrollPane, I can fix afterwards. – Hathor May 02 '13 at 04:30
  • Just did, wow. I'm glad it fixed it, but (so it doesn't happen again) how do I know when to set it Opaque or not? (And finally, do you know where I can finds ways to set custom fonts?) Thanks for the help mate! – Hathor May 02 '13 at 04:50
  • You should use `setOpaque` when you want components to be transparent, but, if you have multiple components sitting on each other, the first opaque component will block any transparent objects underneath it... – MadProgrammer May 02 '13 at 04:57
  • Try having a look at [this](http://stackoverflow.com/questions/12998604/adding-fonts-to-swing-application-and-include-in-package) (for fonts) – MadProgrammer May 02 '13 at 04:58
  • Alright, I'll be sure to keep that in mind when working on this. I'll also take a look at that link for fonts. Thanks again, MadProgrammer. – Hathor May 02 '13 at 05:02
  • @MadProgrammar I didn't want to make another topic, but when I tried adding in fonts like this - http://i.imgur.com/vCMzy1H.png I got the following error - http://i.imgur.com/x9FWpRG.png – Hathor May 02 '13 at 05:53
  • I'd be concerned about `/imageFolder/` been a embedded resources. From the looks of it (not having used Eclipse personally), they aren't built into the final Jar. I had a felling that only the resources folder was, but I could be mistaken... – MadProgrammer May 02 '13 at 06:15