I am creating a profile page. At the left, I have added a jlabel containing a background image to my jpanel. Now over this panel, I want to add another jlabel for the icon. However, it is not showing up. Please help me.
P.s: This is only part of the code
public CustomerProfile()
{
super("Customer Profile");
setLayout(new BorderLayout());
//PROFILE DETAILS JPANEL
jpProfile = new JPanel();
add(jpProfile, BorderLayout.WEST);
//BACKGROUND IMAGE OF THE PROFILE JPANEL
ImageIcon background_img = new ImageIcon("background.jpg");
Image img = background_img.getImage();
Image tempImg = img.getScaledInstance(350, 600, Image.SCALE_SMOOTH);
background_img = new ImageIcon(tempImg);
background = new JLabel("",background_img,JLabel.CENTER);
jpProfile.add(background);
//PROFILE ICON
ImageIcon profImg = new ImageIcon("female.png");
jlProfileIcon = new JLabel();
jlProfileIcon.setIcon(profImg);
//ADDING PROFILE ICON TO JPANEL
jpProfileIcon = new JPanel();
jpProfileIcon.add(jlProfileIcon);
//jpProfile.setOpaque(false);
//jpProfileIcon.setOpaque(false);
jpProfileIcon.add(jlProfileIcon);
background.add(jpProfileIcon);
}
By default only a JPanel uses a layout manager.
The JLabel does not use a layout manager so the size/location of any component added to the label is not changed. The default size of a component is (0, 0) so there is nothing to paint.
Try:
background = new JLabel("",background_img,JLabel.CENTER);
background.setLayout( new BorderLayout() ); // added
…
background.add(jlProfileIcon);
The following code is not needed:
//jpProfileIcon = new JPanel();
//jpProfileIcon.add(jlProfileIcon);
That is you don't need to create a JPanel, just to add the label to it.
A better option is to paint the background image onto a panel and then you can just add your label normally to the panel. See: Background Panel for a class that implements this functionality.
Related
I am trying to make a UI to view recipes from a cookbook stored on the computer. Part of this tab is a JScrollPanel storing a JTextArea that displays the available recipes. All called functions work as intended (e.g. allRecipes() returns a string of the available recipes properly); however, the scroll pane itself does not appear. It is added to the frame, as I can see by a small grey block where the pane would be, but it is not filled as it should be. The code is as follows:
//First panel, buttons to limit displayed recipes
JPanel pane1 = new JPanel();
JButton all = new JButton("All");
JButton makeable = new JButton("Makeable");
JTextField search = new JTextField("", 10);
JButton searchButton = new JButton("Search Ingredient");
//Second panel, display of recipes
JPanel pane2 = new JPanel();
JTextArea recipes = new JTextArea(allRecipes());
JLabel list = new JLabel("List of Recipes:");
JScrollPane scroll = new JScrollPane(recipes);
//Third panel, options to add recipe and view specific recipe
JPanel pane3 = new JPanel();
JButton add = new JButton("Add Recipe");
JTextField view = new JTextField("", 10);
JButton viewButton = new JButton("View Recipe");
//Central method
public Recipes() {
//basic UI stuff
super("Recipes");
setSize(475,350);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
//add pane 1
pane1.add(all);
pane1.add(makeable);
pane1.add(search);
pane1.add(searchButton);
pane1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(pane1);
//add pane 2
pane2.add(list);
scroll.setPreferredSize(new Dimension(10,15));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pane2.add(scroll, BorderLayout.CENTER);
pane2.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(pane2);
//add pane 3
pane3.add(add);
pane3.add(view);
pane3.add(viewButton);
add(pane3);
//start up the UI
setVisible(true);
}
JTextArea recipes = new JTextArea(allRecipes());
We don't know what allRecipes() does, but I would guess it sets the text of the text area.
Instead you should define your text area with the rows/columns you wish. Something like:
JTextArea recipes = new JTextArea(5, 30);
then in the constructor you would add the text:
recipes.setText( allRecipes() );
You should NOT be trying to set the preferred size of the scroll pane. The preferred size will automatically be determined from the preferred size of the text area which is calculated based on the rows/columns provided in the constructor.
//scroll.setPreferredSize(new Dimension(10,15));
Also, the preferred size of a component is specified in pixels, to the above makes no sense.
pane2.add(scroll, BorderLayout.CENTER);
The default layout manager for a JPanel is the FlowLayout. So you can't just use a BorderLayout constraint when adding the component.
I am creating a game which has a background image with cards displayed overtop. I would like to place the background image and cards such that they're always centered vertically and horizontally, even upon resizing the JFrame.
Currently, I am creating the cards (each a JPanel) and adding them into a container JPanel (no layout manager), then I add that Jpanel to the JFrame. After that I place the background image in a JPanel, then add that JPanel to the JFrame. The result is: The background image is hidden behind the cards and revealed when removing each card as desired. The background image is always centered but the card's JPanel does not move around upon resize. I am having a hard time getting the cards to always be centered, no matter what I try. I also need to add another JPanel to the JFrame in the South border, so that will need to work as well. I appreciate your assistance!
In the class that extends JFrame:
setSize(1060,700);
cardPanel = new JPanel();
cardPanel.setSize(1060,700);
cardPanel.setOpaque(false);
cardPanel.setLayout(null);
...card.setLocation(x, y); //loop through cards
...cardPanel.add(card); //and add each one
add(cardPanel, BorderLayout.CENTER); //add cardPanel to JFrame
//Add background image
bgPanel = new JPanel();
URL url = getClass().getResource("images/dragon_bg.png");
imgIcon = new ImageIcon(url);
JLabel background = new JLabel(imgIcon);
bgPanel.setLayout(new BorderLayout());
bgPanel.add(background);
add(bgPanel, BorderLayout.CENTER);
setVisible(true);
I would like to place the background image and cards such that they're always centered vertically and horizontally, even upon resizing the JFrame.
Then you need to use layout managers on your panels. The layout manager is responsible for redoing the layout.
How to add two JPanels to a JFrame in the center?
You could try using the OverlayLayout for this. I think the basic code would be:
JPanel contentPane = new JPanel( new GrigBagLayo9ut() );
frame.add(contentPane, BorderLayout.CENTER);
JPanel overlay = new JPanel()
overlay.setLayout( new OverlayLayout(overlay) );
contentPane.add(overlay, new GridBagConstraints()); // this should center the overlay panel
overlay.add(yourCardPanel); // you care panel must use a suitable layout
overlay.add(new JLabel() ); // use a JLabel for the background not a custom panel
I also need to add another JPanel to the JFrame in the South border,
The default layout manager for a JFrame's content pane is a BorderLayout. We already added the game panel to the center, so know you just add your other panel to the SOUTH.
If the OverlayLayout doesn't work the way you want then you will need to nest panels. Something like:
JPanel center = new JPanel( new GridBagLayout() );
frame.add(center, BorderLayout.CENTER);
JLabel background = new JLabel(...);
background.setLayoutManager( new GridBagLayout() );
center.add(background, new GridBagConstraints());
background.add(yourCardPanel, new GridBagConstraints());
Edit:
Using nested panels:
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutCenter extends JPanel
{
public GridBagLayoutCenter()
{
setLayout( new BorderLayout() );
JLabel background = new JLabel( new ImageIcon("mong.jpg") );
background.setLayout( new GridBagLayout() );
add(background, BorderLayout.CENTER);
JPanel tiles = new JPanel();
tiles.setPreferredSize( new Dimension(200, 200) );
tiles.setBackground( Color.RED );
background.add(tiles, new GridBagConstraints());
add(new JLabel("SOUTH"), BorderLayout.SOUTH);
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("GridBagLayoutCenter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new GridBagLayoutCenter() );
frame.setSize(400, 400);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
The preferred size of the "tiles" panel should not be hardcoded. The size should be determined by your custom layout manager based on the tiles that you add to the panel. The size should not change as tiles are removed.
I ultimately decided to place the background image in the card panel itself, then put the card panel in a box layout manager so that it's always centered. I renamed cardPanel to gameBoard. Definitely could be cleaner, but I can only work with my requirements.
setSize(new Dimension(1000, 600));
gameBoard = new JPanel();
gameBoard.setLayout(null);
gameBoard.setOpaque(false);
Dimension expectedDimension = new Dimension(920, 500);
gameBoard.setPreferredSize(expectedDimension);
gameBoard.setMaximumSize(expectedDimension);
gameBoard.setMinimumSize(expectedDimension);
//add cards to gameBoard here
JLabel background = new JLabel( new ImageIcon( getClass().getResource("images/graphic.png") ) );
background.setLocation(79,0); //manually center graphic
background.setBounds(new Rectangle(0, 0, 920, 500));
gameBoard.add(background);
Box centerBox = new Box(BoxLayout.Y_AXIS);
centerBox.setOpaque(true);
centerBox.setBackground(Color.WHATEVER);
centerBox.add(Box.createVerticalGlue());
centerBox.add(gameBoard);
centerBox.add(Box.createVerticalGlue());
add(centerBox);
setVisible(true);
Good day
I have a basic toolbar to which I added ImageIcon buttons. The images are however different in size.
How would I go about in resizing the Icons so that they are all the same size.
super("ToolBar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//creating the icons for the toolbar
ImageIcon savePic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/save.png");
ImageIcon openFilePic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/open.png");
ImageIcon printPic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/print.png");
//creating buttons with initial text and icons. I.o.w. the buttons for the toolbar are created
JButton save = new JButton("Save", savePic);
JButton open = new JButton("Open", openFilePic);
JButton print = new JButton("Print", printPic);
JToolBar bar = new JToolBar();
bar.add(save);
bar.add(open);
bar.add(new JToolBar.Separator());
bar.add(print);
JTextArea text = new JTextArea(10, 40);
add(BorderLayout.NORTH, bar);
add(BorderLayout.CENTER, text);
pack();
setVisible(true);
The flamingo component suite supports resizable icons, one of the support classes being ImageWrapperResizableIcon. You might try to have a look at the source to get an idea of how to implement automatically resizing icons without the need to manually do so.
Alternatively, just create a resized version of the image yourself and create the ImageIcon using that resized version.
i have an URL image, and i want to display it in a panel.
How can i do it ?
One way to achieve this would be to use the URL class to grab the image from the web, create your ImageIcon object and then add it onto your JPanel,
Code is untested, but should demonstrate what you need to do.
URL img = new URL("http://www.example.com/whatever.jpg");
ImageIcon image = new ImageIcon(img);
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );
What would be the most appropriate image type to display a jpg image (loaded from a local folder) on a JPanel?
Cheers.
ImageIcon image = new ImageIcon("image/pic1.jpg");
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );
You could use a javax.swing.ImageIcon and add it to a JLabel using setIcon() method, then add the JLabel to the JPanel.
I'd probably use an ImageIcon and set it on a JLabel which I'd add to the JPanel.
Here's Sun's docs on the subject matter.
I would use a Canvas that I add to the JPanel, and draw the image on the Canvas.
But Canvas is a quite heavy object, sine it is from awt.
You could also use
ImageIcon background = new ImageIcon("Background/background.png");
JLabel label = new JLabel();
label.setBounds(0, 0, x, y);
label.setIcon(background);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(label);
if your working with a absolut value as layout.