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

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.

Related

Using a function to create multiple components and change line automatically [Java]

So i lost some hours already with this and i can't seem to find a solution.
Basically i have a Jframe and inside, i have a Scrollpane and a panel
I have 1 Jlabel, 1 JTextField and 1 JButton inside that panel in a single line.
The JButton can add a new JLabel, a new JTextField and a new JButton, but i can't get them to be positioned in the next line.
I have been messing around with the layouts, but none of them fits my needs, and unfortunaly i never understand or learned how the GUI of java Works.
How's the best way to just keep adding those componentes (Jlabel, Jtextfields and Jbuttons) on a next line for every click i made?
This is my code:
private void BtnaddvariableActionPerformed(java.awt.event.ActionEvent evt) {
JLabel Lblvariablextra = new JLabel("Testing");
PanelVariable.add(Lblvariablextra);
ScrollPaneVariable.setViewportView(PanelVariable);
}
The code only contains an exemple of the label tough.
Create a main panel that is added to the scroll pane when the GUI is created:
Box main = Box.createVerticalBox();
scrollPane.setViewportView( main );
Then in the ActionListener you create a child panel contain the 3 components every time the button is pressed:
JPanel child = new JPanel();
child.add( new JLabel("I'm a label") );
child.add( new JTextField(10) );
child.add( new JButton("Click Me") );
main.add(child);
Read the section from the Swing tutorial on Layout Manager to understand how layout management works.

JScrollPane is not adding to my JList.

Im trying to add a Scroll bar to my JList. I want only 4 headings to be available on my JList at a time. When I add a JList and run my program, the JList disappears from the screen. Can someone help me fix this problem? I am using Java Eclipse.
This is my code:
songList1 = new JList (ListData);
songList1.setVisibleRowCount(4);
songList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
songList1.setBounds(300,100,100,200);
panel.add(new JScrollPane(songList1);
You can try one of the following:
set panel's layout or
set scroll panes bounding area by scrollPane.setBounds()
Just an oversight:
songList1 = new JList(ListData);
songList1.setVisibleRowCount(4);
songList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane songList1ScrollPane = new JScrollPane(songList1)
//songList1ScrollPane.setBounds(300, 100, 100, 200);
songList1ScrollPane.setBounds(10, 10, 100, 200);
panel.setLayout(null); // Absolute positioning
panel.add(songList1ScrollPane);
...
add(panel); // Or so
Of course the panel should have an appropiate layout.

Displaying a JLabel on top of another JLabel

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.

Java - delete an ImageIcon?

Is there any way to easily delete an ImageIcon completely from the screen? I can't find one anywhere.
ImageIcon image = new ImageIcon("candle.gif");
image.paintIcon(this, g, 150, 80)
For example, if I wanted to get rid of "image" later on when a button is pressed, what would be the appropriate code? (Not for the button, I know how to do that).
Don't reinvent the wheel. The easiest way to paint an Icon is to use a JLabel. The code would be something like:
ImageIcon icon = new ImageIcon(...);
JLabel label = new JLabel( icon );
panel.add( label );
If you don't want to display the icon anymore then in the ActionListener of your button you simple do:
label.setIcon( null );

How to print content of a label in java?

I have created a label and added an image to it using the seticon property.
Now I want to print that image but I am unable to do so.
I tried with .print(Graphics g) method but I dont know what is that graphics??
If someone can help me in printing the label, I would be really grateful to you.
Thanks
You don't need to print anything explicitly just try following
ImageIcon ii=new ImageIcon("PATH_TO_IMAGE_FILE");
//Create a label with your image file
JLabel label=new JLabel(ii);
//Create a window using JFrame with title ( Put image on JLabel )
JFrame frame=new JFrame("Put image on JLabel");
//Add created label into JFrame
frame.add(label);
Update as per your clarification :
Follow this , and pass label.getGraphics() to printAll() method
You should implenent Printable interface
see for example here

Categories