Display multiple images [duplicate] - java

This question already has answers here:
How to add an image to a JPanel?
(14 answers)
Closed 6 years ago.
I need to show images and images numbers are unknown - maybe 5 maybe 10 maybe more and I have no idea how I should display them.
This is my code and display only one image.
public class NewClass extends JFrame {
public static void main(String []args) throws IOException{
BufferedImage img=ImageIO.read(new File("D:\\A-programmer-Life.jpg"));
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(500,500);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Display the images in a JList. The list will allow you to control the number of rows of images and then you add the list to a JScrollPane so scrollbars can appear if necessary.
You can add an Icon to the list and it will render as an image.
Check out the section from the Swing tutorial on How to Use Lists for more information.
The other option is to use a panel with a GridLayout. The labels will wrap to a new row when the first row is full. Again you would add this panel to a JScrollPane. The tutorial also has an example of using a GridLayout.

Related

How can i add JButtons to a panel in vertical way? [duplicate]

This question already has answers here:
Java: vertical alignment within JPanel
(3 answers)
Closed 4 years ago.
the task i am trying to do is simple. I want to add JButtons to a panel in a vertical way, but using a loop to adding it, i tried to do it using .setBounds() and .setLocation() mehtod, but i dont have any results.
In a simple way, i want to do this but adding the buttons vertically and keeping the JScroll bar...:
public class NewMain {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setLayout(null);
for (int i = 0; i < 10; i++) {
JButton asd=new JButton("HOLA "+i);
asd.setLocation(i+20, i+20);
panel.add(asd);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(50, 30, 300, 50);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(500, 400));
contentPane.add(scrollPane);
frame.setContentPane(contentPane);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
Give the JPanel that holds the JButtons an appropriate layout manager that adds components in a vertical manner. A GridLayout(0, 1) would work, the parameters referring to 0 rows -- meaning variable number of rows, and 1 column. This will add the JButtons into a vertical grid, column of one
Other possible solutions include BoxLayout and GridBagLayout, both of which are a little more complex than the GridLayout.
Also avoid using null layouts as you're doing as this leads to inflexible GUI's painful debugging and changes.

Where should the image "logo0.gif" be in order for this code to be able to use it? [duplicate]

This question already has an answer here:
Where have I to put an image to use it to create a new Swing ImageIcon object?
(1 answer)
Closed 5 years ago.
public static void main(String [] args)
{
ImageIcon aLogo = new ImageIcon("Logo0.gif");
JLabel aLabel = new JLabel(aLogo);
JPanel aPanel = new JPanel();
JFrame aFrame = new JFrame();
aFrame.setSize(740, 320);
aFrame.add(aPanel);
aFrame.setVisible(true);
aPanel.add(aLabel);
aLabel.setIcon(aLogo);
}
Here i am trying to create a JFrame to display an image. However when i run this code there is no image on the JFrame, i believe that this is because of the file location.
aFrame.setVisible(true);
aPanel.add(aLabel);
aLabel.setIcon(aLogo);
Components should be added to the frame BEFORE the frame is made visible, otherwise the layout manager is not invoked so the size of the component is (0, 0) which means there is nothing to paint.
Also, read the tutorial and download the demo code for a better way to structure your class so that the components are created properly on the Event Dispatch Thread (EDT). Read the section from the above tutorial on Concurrency in Swing for more information about the EDT and why it is important.

Unable to change image in jScrollPane upon clicking next [duplicate]

This question already has an answer here:
Not able to add image in jscrollpane inside a jframe
(1 answer)
Closed 5 years ago.
I'm making a jframe where i'm adding an image that will change as i click next.I have written a code but it doesn't work.Here is the code:
i++;
ImageIcon icon = new ImageIcon(mean.get(0));
Image image = icon.getImage(); // transform it
Image newimg = image.getScaledInstance(180, 140, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
icon = new ImageIcon(newimg);
JLabel label = new JLabel( icon );
jScrollPane1= new JScrollPane( label );
Please help
The problem lies with:
jScrollPane1= new JScrollPane( label );
The field jScrollPane whose object was placed in the JFrame is set to another JScrollPane, whose object is not added in the GUI.
Store the original label in a field, say jLabel1 and set that label.
A repaint might be needed.
You need to call:
revalidate()
repaint()
See an explanation of why here Java Swing revalidate() vs repaint()
Add this to end of your code jScrollPane1. If haven't JFrame created already create one like below then add that jScrollPane1 to JFrame.
JFrame frame = new JFrame();
frame.add(jScrollPane1, BorderLayout.CENTER);
There is also set the layout as BorderLayout but you can try other layouts or without adding layout also.

using JScrollPane with JFrame, JPanel and GridLayout [duplicate]

This question already has answers here:
Java: Can't apply Gridlayout to a Jscrollpane. Get Get java.lang.ClassCastException
(1 answer)
ScrollPane adding to grid layout
(3 answers)
Closed 5 years ago.
I have a GridLayout (of JTextFields : 57*57) in a JPanel in a JFrame (resolution 800*800) and the result is really small and that's why I would like to have a JScrollPane (that would have a 800*800 resolution and the GridLayout resolution would be 3000*3000 for example).
I've tried many things but it was not working. Can someone please help me ?
Here is the concerned part of the code :
Thanks.
GridLayout grid = new GridLayout(thumbnail.getHeight(), thumbnail.getWidth(),0,0);
final_frame = new JFrame();
final_frame.setPreferredSize(new Dimension(800, 800));
JPanel myPanel = new JPanel();
myPanel.setLayout(grid);
//fill grid with colors and denomination
for (int yy=0;yy<thumbnail.getHeight();yy++){
for (int xx=0;xx<thumbnail.getWidth();xx++){
for (int kk=0;kk<colors.length;kk++){
if (thumbnail.getRGB(xx,yy)==colors[kk].getRGB()){
//System.out.println("Color : "+colors[kk]+" libel : H"+libel[kk]);
JTextField tf = new JTextField("H"+libel[kk]);
tf.setBackground(colors[kk]);
tf.setSize(10, 10); //do nothing ?
myPanel.add(tf);
}
}
}
}
final_frame.add(myPanel);
final_frame.pack();
final_frame.setVisible(true);
final_frame.setLocationRelativeTo(null);
final_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

displaying images from an array in a gui window using swing in java

I am developing a program that will populate an array with 52 images of cards from a file. I would like to display these images in a gui window. This is for a program that will select five random card images and display them in a gui window. So, right now, i am trying to develop the part of the code which will display images from an array in a window and i am at a loss as to how to display png images in a jframe. This is the code i have so far. I used a system.out.println statement so i know that the array of 52 card images is populating correctly, however, i do not know how to display them properly in a window.
String[] cardsArray = new String[52];
for (int i = 0; i < 52; i++)
{
cardsArray[i] = "C:\\Users\\mike\\Documents\\NetBeansProjects\\card shuffler\\cards\\\"+String.valueOf(i+1)+".png";
}
System.out.println(Arrays.toString(cardsArray));
additional note. I have to use a jframe to display the results in a side by side layout. I thought to use flowLayout to accomplish this, but, i a not sure how to pass in an array of images. I have no problem doing it with a single image from a file. I am using the code below as a guide.
JFrame myJFrame = new JFrame();
// create and assign a FlowLayout for myFrame
myJFrame.setLayout(new FlowLayout());
// Create a label with an image icon
JLabel jlCSCI = new JLabel(new ImageIcon("CSCI.jpg"));
// add the Label to the frame
myJFrame.add(jlCSCI); // Add thelabel to MyGridLayout
// set the title, size, location and exit behavior for the frame
myJFrame.setTitle("ImageIcon Demo");
myJFrame.setSize(240, 200);
myJFrame.setLocation(200, 100);
myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make the frame visible (activate the GUI frame)
myJFrame.setVisible(true);
I am not sure how to develop the statement that would utilize an array that i created within the program.
Maybe like this?
for (String imgName : cardsArray)
{
myJFrame.add(new JLabel(new ImageIcon(imgName)));
}
Hope this helps.
EDIT:
I simply added a JLabel with an Icon to the frame. The ImageIcon class is just an implementation of the Icon interface and it creates an icon by reading an image from file. Creating a JLabel with an Icon will display the icon instead of the text. You can also combine the text and the icon. For more info, check the documentation.

Categories