hi all i am trying to add multiple components to my jframe. but i cant seem to get it to work.
private void initGUI() {
setAlwaysOnTop(true);
setUndecorated(true);
AWTUtilities.setWindowOpaque(this, false);
AWTUtilities.setWindowOpacity(this, 0.5f);
setLocation(ini.getButtonsX(), ini.getButtonsY());
setSize(ini.getButtonsW(), ini.getButtonsH());
setLayout(null);
JPanel panel = new JPanel();
panel.setLayout(null);
ImageView baron = new ImageView("image/nashor.png", 50, 50);
baron.setBounds(50, 50, 50, 50);
ImageView test = new ImageView("image/dragon.png", 50, 50);
test.setBounds(50, 150, 50, 50);
panel.add(baron);
panel.add(test);
panel.setBounds(0, 0, ini.getButtonsW(), ini.getButtonsH());
add(panel);
}
my ImageView is a class that extends a JPanel which paints a image.
at this point only nashor is painted
any help is greatly appreciated.
I recommend that you have your JPanel panel use a GridLayout, not a null layout, and that you not set your ImageView sizes but rather make sure that the class has a getPreferredSize() method override that makes sense, that returns a Dimension of the appropriate size. Then if you call pack() on your JFrame after adding components, the layout managers will take care of sizing things for you.
Consider this program:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Init extends JFrame{
JPanel view = new JPanel();
JMenuBar mBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem mItemOpen = new JMenuItem("Open");
JMenuItem mItemExit = new JMenuItem("Exit");
JFileChooser fc = new JFileChooser();
JTextField txtPath = new JTextField();
BufferedImage myPicture;
File filePath;
String path;
public Init(){
mBar.add(menu);
menu.add(mItemOpen);
menu.addSeparator();
menu.add(mItemExit);
setJMenuBar(mBar);
txtPath.setEditable(false);
mItemOpen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fc.showOpenDialog(null);
filePath = fc.getSelectedFile();
path = filePath.getPath();
txtPath.setText(path);
try {
//view.removeAll();
myPicture = ImageIO.read(new File(path));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
view.add(picLabel);
revalidate();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(view),BorderLayout.CENTER);
getContentPane().add(txtPath,BorderLayout.SOUTH);
setTitle(".:My Picture Viewer:.");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//pack();
setSize(1024,768);
setVisible(true);
}
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Init();
}
});
}
}
Related
I have made a student information page using GUI concept. I want to know that how can i add an image at a specific location using JLabel or any other method? What i want is a background image surrounding the whole Jframe and another image at specific location like at the top right. How can i achieve this?
i also found a code to add image using Jlabel but it doesn't work with my code as my setting the layout to null.
the code i found
String path = "Image1.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
f.setLocation(200,200);
f.setVisible(true);
Below is my code:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class LoginPage
{
JFrame jf;
JLabel gender,hobbies,name_label,rollno_label,marks_label,city_label,address_label;
JTextField name_field,rollno_field,marks_field;
JRadioButton male,female;
ButtonGroup bg;
JCheckBox photography,music,sketching,coding;
JComboBox city_combo;
JTextArea adress_textarea;
JButton save, exit;
JMenuBar mbar;
JMenu file,edit,help;
JMenuItem open,save_item,edit_item,close,cut,copy,paste,find,replace,help_content,about,updates;
public LoginPage() //constructor
{
jf = new JFrame("Student Information");
name_label = new JLabel("Student's Name");
name_field = new JTextField();
rollno_label = new JLabel("Student's Roll Number");
rollno_field = new JTextField();
marks_label = new JLabel("Student's Total Marks Achieved");
marks_field = new JTextField();
gender = new JLabel("Gender");
male = new JRadioButton("Male");
female = new JRadioButton("Female");
bg = new ButtonGroup();
hobbies = new JLabel("Hobbies");
photography = new JCheckBox("Photography");
music = new JCheckBox("Music");
coding = new JCheckBox("Coding");
sketching = new JCheckBox("Sketching");
city_label = new JLabel("City");
city_combo = new JComboBox();
address_label = new JLabel("Residential Address");
adress_textarea = new JTextArea();
save = new JButton("Save");
exit = new JButton("Exit");
mbar = new JMenuBar();
file = new JMenu("File");
edit = new JMenu("Edit");
help = new JMenu("Help");
open = new JMenuItem("open");
save_item = new JMenuItem("Save");
edit_item = new JMenuItem("Edit");
close = new JMenuItem("Close");
cut = new JMenuItem("Cut");
copy = new JMenuItem("Copy");
paste = new JMenuItem("Paste");
find = new JMenuItem("Find");
replace = new JMenuItem("Replace");
about = new JMenuItem("About");
updates = new JMenuItem("Check for Updates");
help_content = new JMenuItem("Help Content");
}
void Display()
{
jf.setSize(1000, 700);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(null);
jf.getContentPane().setBackground( Color.LIGHT_GRAY );
name_label.setBounds(50, 50, 150, 20);
name_field.setBounds(300, 50, 200, 20);
rollno_label.setBounds(50, 100, 150, 20);
rollno_field.setBounds(300, 100, 200, 20);
marks_label.setBounds(50, 150, 200, 20);
marks_field.setBounds(300, 150, 200, 20);
gender.setBounds(50, 200, 100, 20);
male.setBounds(300, 200, 80, 20);
female.setBounds(400, 200, 80, 20);
hobbies.setBounds(50, 250, 80, 20);
photography.setBounds(300, 250, 100, 20);
music.setBounds(420, 250, 80, 20);
sketching.setBounds(500, 250, 100, 20);
coding.setBounds(600, 250, 80, 20);
city_label.setBounds(50, 300, 100, 20);
city_combo.setBounds(300, 300, 100, 20);
address_label.setBounds(50, 350, 200, 20);
adress_textarea.setBounds(300, 350, 300, 100);
save.setBounds(300, 500, 100, 50);
exit.setBounds(600, 500, 100, 50);
bg.add(male);
bg.add(female);
city_combo.addItem("Select City");
city_combo.addItem("Chandigarh");
city_combo.addItem("Kurali");
city_combo.addItem("Mohali");
city_combo.addItem("Panchkula");
file.add(open);
file.add(save_item);
file.add(edit_item);
file.add(close);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(find);
edit.add(replace);
help.add(about);
help.add(help_content);
help.add(updates);
mbar.add(file);
mbar.add(edit);
mbar.add(help);
jf.add(name_label);
jf.add(name_field);
jf.add(rollno_label);
jf.add(rollno_field);
jf.add(marks_label);
jf.add(marks_field);
jf.add(gender);
jf.add(male);
jf.add(female);
jf.add(hobbies);
jf.add(music);
jf.add(photography);
jf.add(sketching);
jf.add(coding);
jf.add(city_label);
jf.add(city_combo);
jf.add(address_label);
jf.add(adress_textarea);
jf.add(save);
jf.add(exit);
jf.setJMenuBar(mbar);
jf.setVisible(true);
}
public static void main(String[] args) {
new LoginPage().Display();
}
}
What i want is a background image surrounding the whole Jframe
Suggestions:
Create a class that extends JPanel,
override its paintComponent method
be sure to call the super's paintComponent method in your override
Draw your background image within this method using g.drawImage(...)
Either make this JPanel your JFrame's contentPane or add it to the contentPane BorderLayout.CENTER, and then add your GUI components to this JPanel
Make sure that some of the components are not-opaque, e.g., call setOpaque(false) on your JRadioButtons, and perhaps others, so that the background image shows up.
and another image at specific location like at the top right. How can i achieve this?
Use the same JPanel above, and draw the smaller image using an overload of drawImage(...) that precisely places the image where you want
Notes:
When I create background images for the GUI, I prefer to draw within a JPanel rather than a JLabel since the JLabel is not wired out of the box to act as a contentPane or a decent container.
I strongly advise you not to use null layouts and setBounds as this will lead to gui's that might look good on one platform, but not on another, that have JLabels whose text is not fully seen, that are very difficult to upgrade and maintain. Learn and use the layout managers.
Looks like you're using multiple JFrames. If so, please read: The Use of Multiple JFrames: Good or Bad Practice?
For example, here's one way to display an image as a background image as well as a smaller image in the right upper portion of the GUI, all within a JPanel:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
#SuppressWarnings("serial")
public class LoginPage3 extends JPanel {
public static final String BG_IMG_PATH = "https://upload.wikimedia.org/wikipedia/"
+ "commons/e/e9/Maesil_%28prunus_mume%29_washed_and_stemmed.jpg";
public static final String RU_IMG_PATH = "https://upload.wikimedia.org/wikipedia/"
+ "commons/thumb/5/5b/Escudo_de_San_Pedro_de_Atacama.svg/200px-Escudo_de_San_Pedro_de_Atacama.svg.png";
private BufferedImage backgroundImg;
private BufferedImage rightUpperImg;
public LoginPage3(BufferedImage bgImg, BufferedImage ruImg) {
this.backgroundImg = bgImg;
this.rightUpperImg = ruImg;
}
#Override
public Dimension getPreferredSize() {
if (backgroundImg == null || isPreferredSizeSet()) {
return super.getPreferredSize();
} else {
int w = backgroundImg.getWidth();
int h = backgroundImg.getHeight();
return new Dimension(w, h);
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImg != null) {
g.drawImage(backgroundImg, 0, 0, this);
}
if (rightUpperImg != null) {
int x = getWidth() - rightUpperImg.getWidth();
g.drawImage(rightUpperImg, x, 0, this);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
BufferedImage bg = null;
BufferedImage ru = null;
try {
bg = ImageIO.read(new URL(BG_IMG_PATH));
ru = ImageIO.read(new URL(RU_IMG_PATH));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
LoginPage3 mainPanel = new LoginPage3(bg, ru);
JFrame frame = new JFrame("LoginPage3");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
For background image:
Load it as an icon, and create a JLabel with it. (See your first snippet)
Set the JLabel as the JFrame's content pane.
All you have to take care of, that Icon does not stretch, so you have to have an image that is exactly the same size as your JFrame (or larger).
As for the image at a given position, the JLabel creating and Icon loading process is the same, but after adding it to the JFrame you have to set position and size, just like for your other components, EG call setBounds()...
I am creating a Flight Reservation system for a project, having trouble with the GUI of the app. I am using a CardLayout to manage the multiple cards for this program.
In the login card I am trying to add a background image, but the input fields are shown below the image.
The code for the program is
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
import java.net.*;
public class CardPanel {
public static void main(String[] args) {
try {
CardLayout cardLayout = null;
JFrame frame = new JFrame("Welcome");
JPanel contentPane = new JPanel(cardLayout);
URL url = new URL("https://i.stack.imgur.com/P59NF.png");
BufferedImage img = ImageIO.read(url);
ImageIcon imageIcon = new ImageIcon(img);
JLabel logo = new JLabel(imageIcon);
JPanel buttonsPanel = new JPanel();
JButton login = new JButton("Login");
buttonsPanel.add(login);
contentPane.setLayout(new BorderLayout(10, 15));
contentPane.add(logo, BorderLayout.NORTH);
contentPane.add(buttonsPanel, BorderLayout.SOUTH);
frame.add(contentPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
A screenshot of the app is also attached (http://i.imgur.com/PkjblPu.png).
I would like the button to be above the background image.
A test suggests a card layout cannot be used to show a BG image. It seems that internally it removes one card and adds another when swapping components. Use a custom painted JPanel to draw the BG image.
Here is the evidence.
That red color is the panel with the card layout, the button panel is set to be transparent.
import java.awt.*;
import javax.swing.*;
import java.net.URL;
public class CardPanel {
public static void main(String[] args) throws Exception {
CardLayout cardLayout = new CardLayout();
JFrame frame = new JFrame("Welcome");
JPanel contentPane = new JPanel(cardLayout);
contentPane.setBackground(Color.RED);
ImageIcon imageIcon = new ImageIcon(new URL("https://i.stack.imgur.com/OVOg3.jpg"));
JLabel logo = new JLabel(imageIcon);
JPanel buttonsPanel = new JPanel();
JButton login = new JButton("Login");
buttonsPanel.add(login);
buttonsPanel.setOpaque(false);
contentPane.add(logo, "logo");
contentPane.add(buttonsPanel, "button");
cardLayout.show(contentPane, "button");
frame.add(contentPane, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Please look below for Edits.
So I've looking over numerous "solutions" to fix my problem, but I just can't seem to get it working.
This is what my application looks like with the code below:
Basically, I want to set the location of a button, but I can't manage to do so. Here is my code:
package me.cervinakuy.application;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ControlPanel3 extends JFrame {
JPanel panel = new JPanel();
JButton startRobo = new JButton();
JButton stopRobo = new JButton();
JButton restartRobo = new JButton();
public ControlPanel3() {
// setLayout(null);
setSize(1000, 700);
setResizable(false);
setLocation(450, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(new Color(45, 48, 55));
setTitle("Espin Software | Control Panel");
setVisible(true);
startRobo.setIcon(new ImageIcon(getClass().getResource("/resources/startRobo.png")));
stopRobo.setIcon(new ImageIcon(getClass().getResource("/resources/stopRobo.png")));
restartRobo.setIcon(new ImageIcon(getClass().getResource("/resources/restartRobo.png")));
startRobo.setBorder(null);
stopRobo.setBorder(null);
restartRobo.setBorder(null);
startRobo.setLocation(100, 100);
panel.add(startRobo);
panel.add(stopRobo);
panel.add(restartRobo);
panel.setOpaque(false);
add(panel);
validate();
}
}
EDIT:
I have now managed to create a GUI of what I was initially looking for, however, I have a new problem. Buttons are now pressable from different parts of the GUI, rather than only on the image. For those interested, here is what I have been able to accomplish:
New GUI look.
Updated Code:
package me.cervinakuy.application;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ControlPanel3 extends JFrame {
JPanel panel = new JPanel();
JButton startRobo = new JButton();
JButton stopRobo = new JButton();
JButton restartRobo = new JButton();
public ControlPanel3() {
// setLayout(null);
setSize(1000, 700);
setResizable(false);
setLocation(450, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(new Color(45, 48, 55));
setTitle("Espin Software | Control Panel");
setVisible(true);
startRobo.setIcon(new ImageIcon(getClass().getResource("/resources/startRobo.png")));
stopRobo.setIcon(new ImageIcon(getClass().getResource("/resources/stopRobo.png")));
restartRobo.setIcon(new ImageIcon(getClass().getResource("/resources/restartRobo.png")));
startRobo.setBorder(null);
stopRobo.setBorder(null);
restartRobo.setBorder(null);
panel.setLayout(null);
startRobo.setLocation(200, 200);
startRobo.setBounds(5, -95, 300, 300);
stopRobo.setBounds(5, 0, 300, 300);
restartRobo.setBounds(5, 95, 300, 300);
panel.add(startRobo);
panel.add(stopRobo);
panel.add(restartRobo);
panel.setOpaque(false);
add(panel);
validate();
}
}
There are typically a number of ways to layout components that end with the same effect. In this example, we use a panel to contain the buttons in a column (buttonContainer using a GridLayout) then a panel to restrict that container to the top (buttonConstrainPanel using a BorderLayout) then a container to put that panel on the left (ui with BorderLayout).
It could also be achieved using a single GridBagLayout or a GroupLayout, though the logic of achieving it might not be as simple.
The focus border seen on the blue button indicates the limits of where a mouse click would activate the button.
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ThreeButtonAlignedLeft {
private JComponent ui = null;
private String prefix = "http://i.stack.imgur.com/";
private String[] suffix = {"gJmeJ.png","T5uTa.png","wCF8S.png"};
ThreeButtonAlignedLeft() {
try {
initUI();
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
public void initUI() throws MalformedURLException {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
JPanel buttonContainer = new JPanel(new GridLayout(0, 1, 5, 5));
for (int ii=0; ii<suffix.length; ii++) {
JButton b = new JButton(new ImageIcon(new URL(prefix + suffix[ii])));
b.setBorderPainted(false);
b.setMargin(new Insets(0,0,0,0));
b.setContentAreaFilled(false);
buttonContainer.add(b);
}
JPanel buttonConstrainPanel = new JPanel(new BorderLayout(0, 0));
buttonConstrainPanel.add(buttonContainer, BorderLayout.PAGE_START);
ui.add(buttonConstrainPanel, BorderLayout.LINE_START);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ThreeButtonAlignedLeft o = new ThreeButtonAlignedLeft();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
I am doing a layout, and I have a pair of buttons that I would like to be flush together. I can think of a couple ways to accomplish this, but I am wondering if there is a good way.
Here is some template code:
package helloworld;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.EventQueue;
/**
* Created by matt on 22/07/16.
*/
public class FlushButtons {
private void showGui(){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton plus = new JButton("+");
JButton minus = new JButton("-");
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
panel.add(plus);
panel.add(minus);
frame.setContentPane(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
EventQueue.invokeLater(()->{
new FlushButtons().showGui();
});
}
}
That produces the image on the left, but I would like it to look more like the image on the right.
I tried using JButton#setMargin, but that did not have any effect. I have used JButton#setPreferredSize which I can get the desired result, but I need to know the size of the button beforehand.
Here's my solution which looks ..pretty neat!
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class FlushButtons {
private JComponent ui = null;
FlushButtons() {
initUI();
}
private JButton getFlushButton(String text) {
JButton b = new JButton();
b.setBorderPainted(false);
b.setContentAreaFilled(false);
b.setMargin(new Insets(0,0,0,0));
b.setBorder(null);
b.setIcon(new ImageIcon(getImageOfText(text, Color.GREEN.darker())));
b.setRolloverIcon(new ImageIcon(getImageOfText(text, Color.ORANGE)));
b.setPressedIcon(new ImageIcon(getImageOfText(text, Color.RED)));
return b;
}
private BufferedImage getImageOfText(String text, Color color) {
int s = 24;
BufferedImage bi = new BufferedImage(s, s, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setColor(color);
g.fillRect(0, 0, s, s);
g.setColor(Color.BLACK);
g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
// use better ways to position text..
g.drawString(text, 8, 16);
g.dispose();
return bi;
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
ui.setBorder(new EmptyBorder(4,4,4,4));
ui.add(getFlushButton("+"));
ui.add(getFlushButton("-"));
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
FlushButtons o = new FlushButtons();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
Here is my solution, which looks a bit ridiculous.
JPanel panel = new JPanel();
JButton plus = new JButton("+");
JButton minus = new JButton("-");
plus.setPreferredSize(new Dimension(29, 29));
plus.setMaximumSize(new Dimension(33, 33));
minus.setPreferredSize(new Dimension(29, 29));
minus.setMaximumSize(new Dimension(33, 33));
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
panel.add(plus);
panel.add(minus);
panel.add(Box.createVerticalStrut(33));
panel.add(Box.createHorizontalGlue());
Without the vertical strut the buttons get packed without borders.
Something of a hack, hope to hear something better.
Hi i want to convert panel which contains components like label and buttons to image file.
I have done the following code. The image was saved. but the content of the panel not visible or saved. Can anyone tell me how to save the panel with its components.
Code:
package PanelToImage;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class sample extends JPanel {
public JPanel firstpanel;
public JPanel secondpanel;
JLabel label1, label2;
JButton button1, button2;
public sample() {
firstpanel = new JPanel();
firstpanel.setSize(400,300);
firstpanel.setBackground(Color.RED);
secondpanel = new JPanel();
secondpanel.setBackground(Color.GREEN);
secondpanel.setSize(400,300);
label1 = new JLabel("label1");
label2 = new JLabel("label2");
button1 = new JButton("button1");
button2 = new JButton("button2");
firstpanel.add(label1);
firstpanel.add(button1);
secondpanel.add(label2);
secondpanel.add(button2);
saveImage(firstpanel);
add(firstpanel);
// add(secondpanel);
}
public static void main(String args[]) {
JFrame frame = new JFrame();
sample sam = new sample();
frame.setContentPane(sam);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
}
private void saveImage(JPanel panel) {
BufferedImage img = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
panel.paint(img.getGraphics());
try {
ImageIO.write(img, "png", new File("E://Screen.png"));
System.out.println("panel saved as image");
} catch (Exception e) {
System.out.println("panel not saved" + e.getMessage());
}
}
}
Tthis code works for me (in the JFrame):
Container c = getContentPane();
BufferedImage im = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
c.paint(im.getGraphics());
ImageIO.write(im, "PNG", new File("shot.png"));
Maybe you have used custom panels. If true, try to add super.paint(g) at the beginning of the paint methods of your panels.
EDIT: You have to call saveImage after display the frame:
public static void main(String args[]) {
...
frame.setSize(400, 300);
sam.saveImage(sam.firstpanel);
}
EDIT 2: This is the saved image (is little because the layout, but is the proof that it should work):
I called the saveImage as last call in the main, and used a file in the user dir (new File("Screen.png")) as nIcE cOw said.
Here try this example program, instead of using getGraphics() seems like you have to use createGraphics() for the BufferedImage you are about to make.
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class SnapshotExample
{
private JPanel contentPane;
private void displayGUI()
{
JFrame frame = new JFrame("Snapshot Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.WHITE);
JLabel label = new JLabel("This JLabel will display"
+ " itself on the SNAPSHOT", JLabel.CENTER);
contentPane.add(label);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
makePanelImage(contentPane);
}
private void makePanelImage(Component panel)
{
Dimension size = panel.getSize();
BufferedImage image = new BufferedImage(
size.width, size.height
, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
panel.paint(g2);
try
{
ImageIO.write(image, "png", new File("snapshot.png"));
System.out.println("Panel saved as Image.");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new SnapshotExample().displayGUI();
}
});
}
}