Image as a background in JScrollPane - java

How to add an image as a background in JScrollPane?
I tried this but the image doesn't display:
BufferedImage img = null;
try {
img = ImageIO.read(new File("C:\\Users\\Suraj\\Documents\\NetBeansProjects\\JavaApplication2\\src\\javaapplication2\\images\\2.jpg"));
}
catch (IOException e) {
e.printStackTrace();
}
Image imag = img.getScaledInstance(d.width, d.height, Image.SCALE_SMOOTH);
ImageIcon imageBack = new ImageIcon(imag);
FlowLayout fl = new FlowLayout();
frame.getContentPane().setLayout(fl);
fl.addLayoutComponent(null, new JLabel(imageBack));
EDIT : I would like to add jlabels and jbuttons on the JScrollPane with the background

If your goal is to simply show an image in a JScrollPane without showing other components (such as a JTable) in the JScrollPane, then you should:
Make an ImageIcon out of your image via new ImageIcon(myImage)
Add that Icon to a JLabel
Place the JLabel into the JScrollPane's viewport, something that can be done by passing the JLabel into the JScrollPane's constructor.
And your done.
If you need to do something else, then describe your problem in greater detail.
For example,
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
#SuppressWarnings("serial")
public class ImageInScrollPane extends JPanel {
public static final String IMAGE_PATH = "http://image.desk7.net/"
+ "Space%20Wallpapers/1422_1280x800.jpg";
private static final int PREF_W = 500;
private static final int PREF_H = 400;
public ImageInScrollPane() throws IOException {
URL imageUrl = new URL(IMAGE_PATH);
BufferedImage img = ImageIO.read(imageUrl);
Icon icon = new ImageIcon(img);
JLabel label = new JLabel(icon);
JScrollPane scrollPane = new JScrollPane(label);
setLayout(new BorderLayout());
add(scrollPane);
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
ImageInScrollPane mainPanel = null;
try {
mainPanel = new ImageInScrollPane();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
JFrame frame = new JFrame("ImageInScrollPane");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
You ask in comment:
what if I have to add other objects like buttons?....How do I do it?
In this situation, I'd create a class that extends JPanel and display the image in this JPanel by drawing it within the paintComponent method override (if you search on this, you'll find many examples, some by me), then add the buttons/components to this image-drawing JPanel, and then adding this JPanel into the JScrollPane's viewport as I do with the JLabel above.

Related

When button clicked, replace entire window content with an image

SOLVED
So I have an issue where when I click the button, I want an image to replace the entire content of the window. But it's only replacing, what I believe to be, a part of a panel. Should I not use panels in this instance? I found some code online which didn't use panels which worked, but maybe there is a scenario where I can remove the panel and just cover the entire frame with my image when the button is clicked?
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class SatNav extends JFrame {
private JFrame frame;
private JPanel panel;
private JLabel satelliteLabel;
private JLabel aboutLabel;
private JButton satellite;
private JButton about;
public SatNav() {
frame = new JFrame("Work Package 5");
frame.setVisible(true);
frame.setSize(300, 380);
about = new JButton("About");
add(about);
event abo = new event();
about.addActionListener(abo);
panel = new JPanel();
frame.add(panel);
panel.add(about);
setLocationRelativeTo(null); //This is for centering the frame to your screen.
setDefaultCloseOperation(EXIT_ON_CLOSE); //This for closing your application after you closing the window.
}
public class event implements ActionListener {
public void actionPerformed(ActionEvent abo) {
ImagePanel imagePanel = new ImagePanel();
//JFrames methods
panel.add(imagePanel, BorderLayout.CENTER);
revalidate();
repaint();
about.setVisible(false);
//satellite.setVisible(false);
}
}
public class ImagePanel extends JPanel {
private BufferedImage image;
public ImagePanel() {
try {
image = ImageIO.read(new File("about.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
setBorder(BorderFactory.createLineBorder(Color.black, 2));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}
}
public static void main(String[] args) {
new SatNav();
}
}
Your problem is that you're treating the panel JPanel as if it has a BorderLayout when it doesn't. Rather it has JPanel's default FlowLayout which will size components to their preferred sizes (here [0, 0]) rather than have contained components fill the container. The simple solution: give your panel a BorderLayout:
panel = new JPanel(new BorderLayout());
Now when adding components, the BorderLayout constants will be respected by the container's layout.
Another and possibly better and more durable solution is to use a CardLayout to help you swap components.
For example:
import java.awt.CardLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class SatNav2 extends JPanel {
private static final long serialVersionUID = 1L;
// a publicly available example image for demonstration purposes:
public static final String SAT_PATH = "https://upload.wikimedia.org"
+ "/wikipedia/commons/1/18/AEHF_1.jpg";
private static final String INTRO_PANEL = "intro panel";
private static final String IMAGE_LABEL = "image label";
// our layout
private CardLayout cardLayout = new CardLayout();
// JLabel to display the image
private JLabel imgLabel = new JLabel();
public SatNav2(Image img) {
// put image into JLabel
imgLabel.setIcon(new ImageIcon(img));
// JPanel to hold JButton
JPanel introPanel = new JPanel();
// add button that does the swapping
introPanel.add(new JButton(new ShowImageAction("Show Image")));
// set the CardLayout and add the components. Order of adding
// is important since the first one is displayed
setLayout(cardLayout);
// add components w/ String constants
add(introPanel, INTRO_PANEL);
add(imgLabel, IMAGE_LABEL);
}
private class ShowImageAction extends AbstractAction {
public ShowImageAction(String text) {
super(text);
}
#Override
public void actionPerformed(ActionEvent e) {
// tell card layout to show next component
cardLayout.next(SatNav2.this);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
createAndShowGui();
} catch (IOException e) {
e.printStackTrace();
}
});
}
private static void createAndShowGui() throws IOException {
// get the image
URL imgUrl = new URL(SAT_PATH);
Image img = ImageIO.read(imgUrl);
// pass image into our new JPanel
SatNav2 mainPanel = new SatNav2(img);
JFrame frame = new JFrame("Satellite");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

Wrong size with JScrollPane containing updated image

I am coding a GUI containing a JScrollPane that displays an image that gets updated (with potential modification of its dimensions). The image is in an ImageIcon in a JLabel. The image size is retrieved using ImageIcon.getIconWith() and getIconHeight(). And the JLabel preferred size is updated with those dimensions.
When the application is started for the first time, the JScrollPane and its scrollbars have the right dimensions to view the whole image (potentially using scrolling). But when the image gets updated the JScrollPane and the scrollbars assume the image has the dimensions of the previous image. How do I get the JScrollPane to update correctly ?
Here is a curated version of my GUI. Visualizer.java uses the GUI VisualizerGUI.java. When the "Run" button is pushed, a new image is randomly generated using ImageDrawer.drawImage() (simulates the behavior of the real application) and the content of the JScrollPane is updated using the function VisualizerGUI.setTransitionsImage(String imgPath).
Visualizer.java:
import java.util.*;
import java.io.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Visualizer implements ActionListener {
private VisualizerGUI gui = null;
public Visualizer() {
gui = VisualizerGUI.createAndStart(this);
}
public static void main(String[] args) {
Visualizer viz = new Visualizer();
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Run command")) {
run();
}
}
public void run() {
updateGUIwithSolution();
}
public void updateGUIwithSolution() {
gui.initGUIupdate();
try {
ImageDrawer.drawImage();
gui.setTransitionsImage("image.png");
} catch (Exception e) {
System.out.println("Error while generating image");
e.printStackTrace();
}
gui.finalizeGUIupdate();
}
}
VisualizerGUI.java:
import java.util.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.lang.reflect.InvocationTargetException;
public final class VisualizerGUI {
private JFrame frame;
private JButton runButton;
private JButton nextButton;
private JScrollPane transitionsDisplay;
private JTabbedPane executionsDisplay;
private JTabbedPane tracesDisplay;
private JTextArea textInfoArea;
public VisualizerGUI() {}
private void initGUI(ActionListener actionsHandler) {
//Create and set up the window.
frame = new JFrame("Visualizer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel controlPanel = new JPanel(new FlowLayout());
runButton = new JButton("Run");
runButton.addActionListener(actionsHandler);
runButton.setActionCommand("Run command");
controlPanel.add(runButton);
nextButton = new JButton("Next");
nextButton.addActionListener(actionsHandler);
nextButton.setActionCommand("Find next solution");
controlPanel.add(nextButton);
transitionsDisplay = new JScrollPane();
executionsDisplay = new JTabbedPane();
tracesDisplay = new JTabbedPane();
JSplitPane ETspliter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, executionsDisplay, tracesDisplay);
JSplitPane graphsSpliter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, transitionsDisplay, ETspliter);
textInfoArea = new JTextArea();
textInfoArea.setLineWrap(true);
textInfoArea.setWrapStyleWord(true);
textInfoArea.setEditable(false);
JScrollPane textInfoAreaSP = new JScrollPane(textInfoArea);
JSplitPane topSpliter = new JSplitPane(JSplitPane.VERTICAL_SPLIT, graphsSpliter, textInfoAreaSP);
transitionsDisplay.setPreferredSize(new Dimension(200,200));
executionsDisplay.setPreferredSize(new Dimension(200,200));
tracesDisplay.setPreferredSize(new Dimension(200,200));
textInfoAreaSP.setPreferredSize(new Dimension(200,100));
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(controlPanel, BorderLayout.NORTH);
frame.getContentPane().add(topSpliter, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static VisualizerGUI createAndStart(ActionListener actionsHandler) {
VisualizerGUI gui = new VisualizerGUI();
final Runnable guiRunner =
new Runnable() {
public void run() {
gui.initGUI(actionsHandler);
// gui.pack();
}
};
try {
javax.swing.SwingUtilities.invokeAndWait(guiRunner);
} catch (InterruptedException e) {
System.out.println(">>> WARNING <<< InterruptedException while creating the GUI");
} catch (InvocationTargetException e) {
System.out.println(">>> WARNING <<< InvocationTargetException while creating the GUI");
}
return gui;
}
public void clear() {
initGUIupdate();
finalizeGUIupdate();
}
public void initGUIupdate() {
// frame.setVisible(false);
transitionsDisplay.setViewportView(null);
executionsDisplay.removeAll();
tracesDisplay.removeAll();
textInfoArea.setText(null);
}
public void pack() {
frame.pack();
}
public void finalizeGUIupdate() {
// frame.validate();
// frame.repaint();
// frame.setVisible(true);
}
public void setTransitionsImage(String imgPath) {
ImageIcon icon = new ImageIcon(imgPath);
icon.getImage().flush();
int width = icon.getIconWidth();
int height = icon.getIconHeight();
JLabel label = new JLabel();
label.setVerticalAlignment(SwingConstants.CENTER);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setIcon(icon);
label.setPreferredSize(new Dimension(width,height));
//label.setPreferredSize(null);
transitionsDisplay.setViewportView(label);
label.revalidate();
label.repaint();
transitionsDisplay.getViewport().revalidate();
transitionsDisplay.getViewport().repaint();
transitionsDisplay.revalidate();
// transitionsDisplay.validate();
transitionsDisplay.repaint();
frame.revalidate();
// frame.validate();
frame.repaint();
}
public void setTransitionsImageInED(String imgPath) {
final Runnable guiRunner =
new Runnable() {
public void run() { setTransitionsImage(imgPath); }
};
// javax.swing.SwingUtilities.invokeLater(guiRunner);
try {
javax.swing.SwingUtilities.invokeAndWait(guiRunner);
} catch (InterruptedException e) {
System.out.println(">>> WARNING <<< InterruptedException while creating the GUI");
} catch (InvocationTargetException e) {
System.out.println(">>> WARNING <<< InvocationTargetException while creating the GUI");
}
}
}
ImageDrawer.java:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageDrawer {
public static void drawImage() throws Exception {
try {
int width = 20 + (int)(Math.random() * 1000);
int height = 20 + (int)(Math.random() * 1000);
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();
ig2.setPaint(Color.blue);
ig2.fillRect(0, 0, width, height);
ig2.setPaint(Color.red);
ig2.fillRect(5, 5, width - 10, height - 10);
ig2.setPaint(Color.blue);
ig2.drawLine(0, 0, width, height);
ig2.drawLine(0, height, width, 0);
ImageIO.write(bi, "PNG", new File("image.png"));
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
Can someone explain why I have this problem? Thanks!
transitionsDisplay.setPreferredSize(new Dimension(200,200));
executionsDisplay.setPreferredSize(new Dimension(200,200));
tracesDisplay.setPreferredSize(new Dimension(200,200));
textInfoAreaSP.setPreferredSize(new Dimension(200,100));
Don't use setPreferredSize(...). Each Swing component is responsible for determining its own size.
The image size is retrieved using ImageIcon.getIconWith() and getIconHeight(). And the JLabel preferred size is updated with those dimensions.
Not necessary. Again the JLabel will determine its own size based on the size of the Icon. This is done dynamically as the image/icon changes.
The scrollbars of the scrollpane will appear when the preferred size of the label is greater than the size of the scrollpane. Just let the layout managers do their job.

Unable to add other components to window after creating background using JLabel in Java

I want to create a java Application like a widget. Here is my code below
package newpackage;
public class MainFrame extends JFrame {
JLabel imageLabel = new JLabel();
public MainFrame() {
try {
this.setUndecorated(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(new Dimension(360, 360));
ImageIcon ii = new ImageIcon(this.getClass().getResource("imageexcel.gif"));
imageLabel.setIcon(ii);
add(imageLabel, java.awt.BorderLayout.CENTER);
this.setVisible(true);
Shape shape=new Ellipse2D.Float(0,0,360,360);
AWTUtilities.setWindowShape(this, shape);
AWTUtilities.setWindowOpaque(this, false);
imageLabel.add(new JButton("START"));
} catch (Exception exception) {
exception.printStackTrace();
}
}
public static void main(String[] args) {
new MainFrame();
}
}
In the above code, I have done the following:
Created a Frame
Removed the Title Bar
Added the Background using JLabel
Changed the shape of window as circle according to the shape of image
Now I would like to add some components in to it and perform some action with them but no component is visible after adding.
I have tried adding to Frame as well as JLabel and no use from either.
This is the image i used for background
Please help me to proceed further....
Thanking you
JLabels use null layouts by default, and so your button will default to size 0,0. Try giving it a decent layout manager, even FlowLayout would likely work. Another solution is to keep the null layout and set the sizes and positions of added components, but this route is a dangerous route and one I don't recommend.
Actually a GridBagLayout works nice to center the components. Also add all components before calling setVisible(true):
imageLabel.setLayout(new GridBagLayout());
this.setUndecorated(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(new Dimension(360, 360));
ImageIcon ii = new ImageIcon(this.getClass().getResource("imageexcel.gif"));
imageLabel.setIcon(ii);
add(imageLabel, java.awt.BorderLayout.CENTER);
imageLabel.add(new JButton("START"));
this.setVisible(true);
or better?
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Shape;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.Ellipse2D;
import java.net.URL;
import javax.swing.*;
import com.sun.awt.AWTUtilities;
#SuppressWarnings("serial")
public class MainPanelOvalFrame extends JPanel {
private static final String RESOURCE_PATH = "imageexcel.gif";
private Window window;
private Image img;
public MainPanelOvalFrame(Window window, Image image) {
this.window = window;
this.img = image;
setLayout(new GridBagLayout());
add(new JButton(new StartAction("Start", KeyEvent.VK_S)));
int w = image.getWidth(this);
int h = image.getHeight(this);
Shape shape = new Ellipse2D.Float(0, 0, w, h);
AWTUtilities.setWindowShape(window, shape);
AWTUtilities.setWindowOpaque(window, false);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet() || img == null) {
return super.getPreferredSize();
}
int w = img.getWidth(this);
int h = img.getHeight(this);
return new Dimension(w, h);
}
private class StartAction extends AbstractAction {
public StartAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
window.dispose();
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame();
frame.setUndecorated(true);
URL imgUrl = MainPanelOvalFrame.class.getResource(RESOURCE_PATH);
Image image = new ImageIcon(imgUrl).getImage();
MainPanelOvalFrame mainPanel = new MainPanelOvalFrame(frame, image);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
You just need to set a Layout manager for imageLabel ,or use null as the Layout manager, then set the size and location of the JButton manually.
to use Layout Manager
imageLabel.setIcon(ii);
imageLabel.setLayout(new FlowLayout());
imageLabel.add(new JButton("START"));
//need to setLayout and add JButton before setVisible(true)
add(imageLabel, java.awt.BorderLayout.CENTER);
this.setVisible(true);
to use null layout
JButton j=new JButton("START");
j.setSize(100,50);
j.setLocation(imageLabel.getWidth()/2-j.getWidth()/2, imageLabel.getHeight()/2-j.getHeight()/2);
//then add Button into imageLabel
imageLabel.add(j);
Layout manager is usually recommended because it can fit different environment.

Placing button on top of image

I am back again. I was wondering how I would go about placing a button on top of an image in a GUI. Here is my current code:
private static JPanel titlePanel = new JPanel();
private static JLabel titleScreen = new JLabel();
private static JLabel titleScreenBackground = new JLabel();
private static JButton startGameButton = new JButton("START GAME");
private static ImageIcon titleScreenPic = new ImageIcon("http://icdn6.digitaltrends.com/image/battleship-650x0.jpg");
private static JFrame frame=new JFrame(); //creates frame
public static void main(String[] args) throws MalformedURLException{
titleScreen();
}
public static void titleScreen() throws IOException{
titleScreen.setLayout(new GridBagLayout());
titlePanel.setLayout(new GridBagLayout());
GridBagConstraints c1 = new GridBagConstraints();
c1.gridx = 0;
c1.gridy = 0;
c1.anchor = GridBagConstraints.PAGE_END;
titleScreenBackground.setIcon(titleScreenPic);
titlePanel.add(startGameButton);
titlePanel.setAlignmentY(SwingConstants.BOTTOM);
frame.add(titleScreenBackground);
frame.add(titlePanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(630, 300); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
}
I tried to make the panel a gridbaglayout so I could place the components in the same cell, but it still places the image first and then the button directly next to it.
EDIT: I have redone the code, making it do somewhat what I wanted. As you can see, the line where I try to set the location of the button does not do anything to the button.
how I would go about placing a button on top of an image in a GUI.
If you want to place a Swing button on top of an image then you need to follow 2 steps.
set a layout manager for the label containing the image.
add the button to the label (not the panel).
See Background Panel for more information and examples.
Edit:
To center a component the easiest approach is:
label.setLayout( new GridBagLayout() );
label.add(button, new GridBagConstraints());
If you want button on the image you can just use image in paint method of JPanel.
Example (with resource im
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestFrame extends JFrame {
BackgroundPane bgPane;
private JButton startButton;
public TestFrame() {
super();
initComponents();
}
private void initComponents() {
try {
URL url = getClass().getResource("battleship-650x0.jpg");
BufferedImage image = ImageIO.read(url);
bgPane = new BackgroundPane(image);
bgPane.setLayout(new GridBagLayout());
startButton = new JButton("Start");
bgPane.add(startButton);
setContentPane(bgPane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* #param args
*/
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setVisible(true);
}
class BackgroundPane extends JPanel {
Image image;
public BackgroundPane(Image backGroundImage) {
super();
image = backGroundImage;
setPreferredSize(new Dimension(image.getWidth(this), image.getHeight(this)));
}
#Override
public void paint(Graphics g) {
super.paint(g);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
}

Error with JScrollPane and Content

I couldn't explain the question in the title any better, so here goes -
I created a tiled background image. I then set the created background image to my JFrame. However, I added my JScrollPane to said background. Depending on the order I place my code in, 1 of two things will happen. When I have my JScrollPane like so -
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
public class TestingApp {
public static JFrame programFrame;
public static JLabel projectBackground;
public static JLabel projectLogo;
public static JPanel allContent;
public static JPanel fourRows;
public static JPanel centerPanel;
public static JScrollPane scrollPane;
// Tiled Background
public static void tiledBackground() {
#SuppressWarnings("serial")
class ImagePanel extends JPanel {
public Image image;
public boolean tile;
ImagePanel(Image image) {
this.image = image;
this.tile = true;
};
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int iw = image.getWidth(this);
int ih = image.getHeight(this);
if (iw > 0 && ih > 0) {
for (int x = 0; x < getWidth(); x += iw) {
for (int y = 0; y < getHeight(); y += ih) {
g.drawImage(image, x, y, iw, ih, this);
}
}
}
}
}
}
// Making the parts for the GUI
public static void createGUI() {
java.net.URL img1 = null;
try {
img1 = new URL("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSE6uHn-B_qKtLZOKjQNVeIxhOaxbmfio45VMUq-mVgGKvgmeghKw");
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
Image image = null;
try {
image = ImageIO.read(img1);
} catch (IOException e) {
e.printStackTrace();
}
// programFrame Title and Layout
programFrame = new JFrame("Organizer");
programFrame.setLayout(new BorderLayout());
Icon backgroundIcon = new ImageIcon(img1);
projectBackground = new JLabel(backgroundIcon);
// Logo JLabel
java.net.URL img2 = null;
try {
img2 = new URL("https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTvXxBQRsJ5NgSb8VOSNU_Qfom6HRV_crcazhD6bSZUh_ux3VHbgQ");
} catch (MalformedURLException e) {
e.printStackTrace();
}
Icon logoIcon = new ImageIcon(img2);
projectLogo = new JLabel(logoIcon);
projectLogo.setBorder(new EmptyBorder(10, 10, 10, 10));
// New JPanel for GridLayouts to hold each JPanel with GridLayouts
fourRows = new JPanel(new GridLayout(1, 4));
fourRows.setLayout(new GridLayout());
fourRows.setOpaque(false);
fourRows.add(new JButton("Button"));
fourRows.add(new JButton("Button"));
fourRows.add(new JButton("Button"));
fourRows.add(new JButton("Button"));
// Makes the Initial BorderLayout (Using allContent JPanel)
allContent = new JPanel();
allContent.setLayout(new BorderLayout());
allContent.add(projectLogo, BorderLayout.NORTH);
allContent.setVisible(true);
allContent.setOpaque(false);
allContent.add(fourRows, BorderLayout.CENTER);
// Add ScrollPane
scrollPane = new JScrollPane(allContent);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.getVerticalScrollBar().setUnitIncrement(10);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
// JFrame programFrame Constructors
programFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
programFrame.setLayout(new BorderLayout());
programFrame.setContentPane(new ImagePanel(image));
programFrame.add(scrollPane);
programFrame.pack();
programFrame.setVisible(true);
programFrame.setResizable(true);
programFrame.setSize(1280, 720);
programFrame.setLocationRelativeTo(null);
} // 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
});
}
}
It doesn't scroll on the entire JFrame, just the contentStuff, and even then, it stretches beyond to the bottom of the content to where the scroll bar isn't there, and you can't scroll.
However, when I rearrange that code, and I put my
programFrame.setContentPane(new ImagePanel(image));
BEFORE
programFrame.add(scrollPane);
I just get the repeated background image, and no content.
EDIT - Added an SSCCE
EDIT2 - Here's a solution I was trying. I tried creating an empty panel to add in the content that should update based on JFrame size. Needless to say, it didn't work. Small snippet of the edited code, nothing else was changed -
// Makes the Initial BorderLayout (Using allContent JPanel)
allContent = new JPanel();
allContent.setLayout(new BorderLayout());
allContent.add(warlordsLogo, BorderLayout.NORTH);
allContent.setVisible(true);
allContent.setOpaque(false);
allContent.add(fourRows, BorderLayout.CENTER);
int widthForCenterPanel = programFrame.getWidth();
int heightForCenterPanel = programFrame.getHeight();
// Makes a Panel to add the ScrollPane to to center is properly
centerPanel = new JPanel();
centerPanel.setOpaque(false);
centerPanel.setBounds(0, 0, widthForCenterPanel, heightForCenterPanel);
centerPanel.add(allContent);
// Add ScrollPane
scrollPane = new JScrollPane(centerPanel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.getVerticalScrollBar().setUnitIncrement(10);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
EDIT 3 - Fixed my SSCCE. If you notice when you try it, it's dependent on how large the JPanel is, not the JFrame.

Categories