I want to know how to nest JPanels using a GridLayout. This is how it should look like.
I approached this problem by 2 ways so far,
using JPanels and
using JLabels,
and none of them worked (only the first panel created is shown).
Here is the code for the JPanel approach:
int x=20, y=20;
JPanel [] panels = new JPanel[3];
JLabel animal = new JLabel(new ImageIcon(getClass().getResource("Pictures/animal.gif")));
JLabel map = new JLabel(new ImageIcon(getClass().getResource("Pictures/map.gif")));
JLabel mountain = new JLabel(new ImageIcon(getClass().getResource("Pictures/mountain.gif")));
for(int i=0;i<panels.length;i++)
{
if(i>0)
{
x+=x;
y+=y;
}
panels[i] = new JPanel(new GridLayout(2,2));
panels[i].setPreferredSize(new Dimension(x,y));
if(i==0)
panels[i].add(new JPanel());
else
panels[i].add(panels[i-1]);
panels[i].add(mountain);
panels[i].add(map);
panels[i].add(animal);
}
add(panels[2]);
One option is to create a class that will represent a panel divided into the grid with the images. The only issue would be the top left quadrant which would usually contain the nested panel, at some point you want this to contain just a blank panel. So maybe something like this (barring various optimizations):
class GridPanel extends JPanel{
JLabel mountain, map, animal;
public GridPanel(JPanel panel){
super();
setLayout(new GridLayout(2, 2));
animal = new JLabel(new ImageIcon(getClass().getResource("pictures/animal.gif")));
map = new JLabel(new ImageIcon(getClass().getResource("pictures/map.gif")));
mountain = new JLabel(new ImageIcon(getClass().getResource("pictures/mountain.gif")));
add(panel);
add(mountain);
add(map);
add(animal);
}
}
Notice that it accepts the panel that is to be displayed in the top left corner of the grid. This coud then be called with the panel specified. So at the point where you want to create the main panel:
JPanel grid = new GridPanel(new JPanel()); //initial
for(int i = 1; i <= 5; i++){
grid = new GridPanel(grid);
}
add(grid);
The initial grid is created with a blank JPanel. And every subsequent grid will contain the previous one as the top left panel. You have to resize your images and such and maybe even avoid loading the images multiple times etc. But that is another question. This example shows 5 nested panels.
Just to be clear, you should use ImageIO to load the images once and reuse the images. For example You can create a BufferedImage like this in your main class:
BufferedImage mointainImg = ImageIO.read(new File("pictures/mountain.gif"));
And when you want to create the JLabel you can do this:
mountain = new JLabel(new ImageIcon(mountainImg));
And the advantage is that you can manipulate the image a bit if you want.
One issue that you have is that the images are not scaled. To scale images, use Image.getScaledInstance(). Proper scaling would at least fix the problem of the visible images being cut off. It also might cause the other images to be shown as they might just be hiding behind the visible images because they are too big.
Related
I am new to Java, what I am trying to do is generate a GUI with a bunch of images I have managed to generate a bunch of images, however, they have unnecessary space between them horizontally, is there any way that I can get rid of this currently I am generating them like so?
setLayout(new FlowLayout());
int numberOfTabs = 4;// number of times image is shown
for(int i = 0; i<numberOfTabs;i++) {
add(
new JLabel(new ImageIcon(getClass().getClassLoader().getResource("Path to image"))));
//creates a new image anonymous object and adds it to the jframe
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Sheetssss");
pack();
setVisible(true);
//also in this context this = a Jframe object so all these methods can be called without an object reference
is there some other method that I can call so that the horizontal spacing can be set to 0?
As shown in the image below there is space between the images, the 2 vertical black lines between images should be overlapping
Thanks
You just need to change
setLayout(new FlowLayout())
to
setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
I know that absolute positioning is not recommended, but I need to show my labels randomly scattered as well as randomly changing their positions.
I have researched how to use setBounds but it doesn't seem to work. The following code shows the labels in a Flow Layout, and when I use setLayout(null) it shows a blank frame.
public class GUI extends JFrame{
device mobiles[];
device station;
JPanel pane= new JPanel();
public GUI()
{
setTitle("communication is Key");
setSize(1000, 1000);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pane.setBackground(Color.WHITE);
int x=0; int y=0;
mobiles= new device[10];
for (int i = 0; i < 10; i++) {
x=randInt();
y=randInt();
mobiles[i]= new device(1,x,y);
pane.add(mobiles[i]);
}
x=randInt();
y=randInt();
station = new device(0,x,y);
pane.add(station);
this.add(pane);
}
and this is class "devices" that extends JLabel
public class device extends JLabel{
ImageIcon mob = new ImageIcon("mob.png");
ImageIcon tow = new ImageIcon("tower.png");
public device(int num, int x, int y)
{ if(num==1)
this.setIcon(mob);
else this.setIcon(tow);
this.setBounds(x, y, 3, 7);
}
}
any help in finding out what the problem is, would be be appreciated.
The following code shows the labels in a Flow Layout, and when I use setLayout(null) it shows a blank frame.
The layout manager sets the size and location of the component.
If you don't use the layout manager, then you are responsible for set the size and location of each component.
Typically I would set the size to equal to the components preferred size.
Also, did you display the x/y value that are randomly generated? Maybe the values are larger than the size of the panel.
and when I use setLayout(null) it shows a blank frame.
What layout is set to null? The panel of the frame. Your code doesn't use the above method. Post the code that you use to cause the problem. We don't want to guess what you may or may not be doing.
thanks to #CasparNoree ... the answer suggested was to initialize the Japnel from the start:
JPanel pane = new JPanel(null);
When you set the layout to null you can set the bounds manually with coordinates.
JFrame jf = new JFrame();
JPanel p = new JPanel();
JButton jb = new JButton();
// this is where you make it so setting the bounds actually does something
p.setLayout(null);
jb.setBounds(100,100,100,100);
p.add(jb);
jf.add(p);
jf.setVisible(true);
I have the following code:
try {
File file_background = new File(
"C:\\Users\\xxxx\\Desktop\\background.png");
ImageIcon icon_background = new ImageIcon(
ImageIO.read(file_background));
JLabel background = new JLabel(icon_background);
window.setContentPane(background);
File file_car = new File(
"C:\\Users\\xxxxxx\\Desktop\\car.png");
ImageIcon icon_car = new ImageIcon(ImageIO.read(file_car));
JLabel car = new JLabel(icon_car);
car.setVisible(true);
background.add(car);
// TODO Get car showing on top of the background label
} catch (IOException e) {
e.printStackTrace();
}
Where I'm attempting to have the car label show on TOP of the background label. But I'm only getting the background JLabel showing. I'm new to SWING so any suggestions to what steps I'm missing would be great.
..I want to move it a later stage. But right now I want it to show first :)
There are two ways,
1st.
Put JLabel car to JPanel, drawing an Image by using paintComponent, instead of JLabel background (advantage JPanel is container with proper notifications for LayoutManager).
Put JLabel car to JLabel background, but JLabel haven't implemented any LayoutManager, have to set desired.
Advantage all images in JLabel are static, with zero CPU and GPU inpact ad consumption in compare with paintComponent.
Disadvantage JLabel isn't container and with proper notifications for LayoutManager, required a few code lones moreover in compare with JLabel placed in JPanel, for movement (AbsoluteLayout) is quite good solution.
2nd.
Draw both Images by using BufferedImage and Graphics.
Add them both to a JPanel that uses OverlayLayout. It is not ok to add a JLabel to another JLabel.
This code has not gone through a compiler so take it for what it is :)
JPanel panel = new JPanel(new OverlayLayout());
panel.add(background);
panel.add(car);
Works.. Added the following to make it display:
car.setBounds(200, 200, 200, 200);
Apparently it's because by default a null layout manager is used. So setting the bounds of the label will enable it to display since the default size is 0.
I have searched many places to add and display images dynamically on JPanel but couldn't get proper help.
Basically I have JPanel on which I have to display many images vertically but it should be dynamic.
for(int i=0;i<macthedImages.length;i++) {
JLabel jLabel = new JLabel(new ImageIcon(macthedImages[i]));
searchResultPanel.add(jLabel);
}
macthedImages is an array of bufferedImages
searchResultPanel is JPanel
1) you have to set proper LayoutManager,
2) for lots of Images in the JLabel would be GridLayout best options, in case that you want to see all images on one JPanel
3) use CardLayout, if you want to see each Image separatelly
4) maybe there no needed re-create
JLabel jLabel = new JLabel(new ImageIcon(macthedImages[i]));
only to set
jLabel[i].setIcon(macthedImages[i]);
5) maybe put JPanel to the JSCrollPane
6) if you add/remove JCOmponents on Runtime you have to call
revalidate();
repaint()// sometimes required
If you want to show all images at same time then use GridLayout but you have to consider rows and columns of grid layout.
GridLayout gl = new gridLayout(2,macthedImages.length/2);
Or if you want to show one image at a time then use CardLayout. Like this:
CardLayout cl = new CardLayout();
for(int i=0;i<macthedImages.length;i++){
JLabel jLabel = new JLabel(new ImageIcon(macthedImages[i]));
cl.add(jLabel, "jLabel"+i);
}
In second option you can show any image by firing event. It provides many methods
I have task to prepare two windows with swing. One contains grid of squares, with random numbers in them. In second I need to load pieces of tiled image and then show them in the correct order, forming tiled image.
Windows should look like this :
alt text http://img535.imageshack.us/img535/3129/lab8a.jpg
Okay so how to bite this? I've used swing only few times to draw some 2d polylines, so basically I just theoretically now what to do.
Ok, so window number 1:
I start with creating Jframe for the window. Then I do for loop and in it create 16 JLabels with random numbers in them? How to set margins between each tile and the whole window?
Window number 2:
So I start the same, but instead of loading numbers I add images? Now, how can I load image from file and then set it as background?
The following code lays out the JLabels using the GridLayout. The arguments to the GridLayout are the following: rows, cols, horizontal gap, vertical gap. In the example below I have 3 pixels wide gap between labels both vertically and horizontally.
To use images instead of numbers, you could pass an ImageIcon to the constructor of the JLabel instead of the text.
However, it looks like your doing a game where the user should be able to click on the tiles. This suggests that you perhaps should use buttons instead of labels, but it's up to you :-)
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class FrameTest {
public static void main(String[] args) {
final JFrame f = new JFrame("Frame Test");
JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));
for (int i = 0; i < 16; i++) {
JLabel l = new JLabel("" + i, JLabel.CENTER);
//JLabel l = new JLabel(new ImageIcon("image_file.png"), JLabel.CENTER);
l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
l.setFont(l.getFont().deriveFont(20f));
panel.add(l);
}
f.setContentPane(panel);
f.setSize(200, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}