I'm using JLayeredPanel to put two panels on top of each other. The first panel would act like a background and the second panel which uses JFileChooser to get the user's image to print on the panel then that panel will lay on top of the first panel with a button click.
I've sucessfully get the image to print onto the second panel fron JFileChooser but unable to get the second panel to lay on top of the first panel.
Here's the outline.
http://s8.postimg.org/ofblrk179/outline.png
Here's my code.
...
public class DesignerUI extends JFrame {
public DesignerUI() {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
DesignerUI frame = new DesignerUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
card2 = new JPanel();
cards.add(card2, "Design");
card2.setLayout(null);
JButton btnGraphic = new JButton("Add Graphic");
btnGraphic.setBounds(0, 0, 400, 53);
panel.add(btnGraphic);
btnGraphic.addActionListener(new chooseGraphic());
JLayeredPane lp = new JLayeredPane();
lp.setPreferredSize(new Dimension(300,400));
lp.setBounds(400,0,382,406);
card2.add(lp);
panel_1 = new JPanel();
panel_1.setBounds(0, 0, 383, 406);
picPanel = new JPanel();
picPanel.setBounds(105, 115, 213, 200);
lp.add(picPanel, new Integer(2));
lp.add(panel_1, new Integer(3));
private class chooseGraphic implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFrame graphicInputWindow = new JFrame();
new graphicInputWindow();
}
}
public class graphicInputWindow extends JFrame {
JButton button;
JButton select;
JLabel label;
JPanel picPanel;
Image image;
public graphicInputWindow() {
super("Select Your Image");
setResizable(false);
button = new JButton("Browse");
button.setBounds(300, 300, 100, 40);
select = new JButton("Select");
select.setBounds(400, 300, 100, 40);
picPanel = new JPanel();
label = new JLabel();
label.setBounds(10, 10, 670, 250);
add(button);
add(select);
add(picPanel);
add(label);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser file = new JFileChooser();
file.setCurrentDirectory(new File(System
.getProperty("user.home")));
// filter the files
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"*.Images", "jpg", "gif", "png");
file.addChoosableFileFilter(filter);
int result = file.showSaveDialog(null);
// if the user click on save
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = file.getSelectedFile();
String path = selectedFile.getAbsolutePath();
label.setIcon(ResizeImage(path));
}
// if the user click on cancel
else if (result == JFileChooser.CANCEL_OPTION) {
System.out.println("No File Select");
}
}
});
select.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// if user click select
JPanel picPanel = new JPanel(new BorderLayout());
picPanel.add(label);
lp.add(picPanel);
lp.setVisible(true);
//close window returns to card2
setVisible(false);
}
});
setSize(700, 400);
setVisible(true);
revalidate();
contentPane.revalidate();
contentPane.repaint();
}
public ImageIcon ResizeImage(String ImagePath) {
ImageIcon MyImage = new ImageIcon(ImagePath);
Image img = MyImage.getImage();
Image newImg = img.getScaledInstance(panel_1.getWidth(),
panel_1.getHeight(), Image.SCALE_SMOOTH);
ImageIcon image = new ImageIcon(newImg);
return image;
}
/*protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}*/
}
}
Related
I want to create a window where there is a background image and components on top of that. I have managed to get the components and background 'stacked', but my problem is positioning the components.
I have tried using AbsoluteLayout but it doesn't seem to work.
This is my (beginner) code thus far:
public class RegionPrompt extends JPanel {
public RegionPrompt () throws IOException {
JFrame frame = new JFrame("Map");
GridBagConstraints gbc = new GridBagConstraints();
JPanel pane = new JPanel() {
URL image2 = getClass().getResource("map.jpg");
BufferedImage image = ImageIO.read(image2);
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), this);
}
};
frame.setContentPane(pane);
JPanel pane2 = new JPanel();
pane2.setOpaque(false);
pane2.setSize(DefaultGUI.defaultSize);
pane2.setLayout(new GridBagLayout());
JButton c = new JButton("Map Location");
//gbc.gridx = 0;
//gbc.gridy = 0;
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Location information", JOptionPane.INFORMATION_MESSAGE);
}
});
pane2.add(c,gbc);
frame.add(pane2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(653, 448);
frame.setVisible(true);
}
}
Any help (as well as forgiveness for this code chunk) is greatly appreciated!
I need to make a GUI that asks the users for details and then save them in a linked list. I wanted to use the CardLayout to switch from one frame to another, which is something I'm doing for the first time. I have done probably less half of what I need to do and here I am quite lost in this part. The code below compiles and executes but when I click the buttons, the desired change does not happen . What could be wrong?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyDatabaseWindow extends JPanel{
public static final String FRONT_PAGE = "Front Page";
public static final String BROWSE_MEMORIES = "Browse Memories";
public static final String ADD_EDIT = "Add Edit";
public static final String VIEW_MEMORY = "View Memory";
public static void createAndShowGUI() {
final MyDatabaseWindow mdbw = new MyDatabaseWindow();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
final JButton addButton = new JButton("Add");
final JButton editButton = new JButton("Edit");
final JButton deleteButton = new JButton("Delete");
final JButton browseButton = new JButton("Browse");
final JButton searchButton = new JButton("Search");
addButton.setBounds(100, 400, 100, 100);
editButton.setBounds(200, 400, 100, 100);
deleteButton.setBounds(300, 400, 100, 100);
browseButton.setBounds(400, 400, 100, 100);
searchButton.setBounds(500, 400, 100, 100);
buttonPanel.add(addButton);
buttonPanel.add(editButton);
buttonPanel.add(deleteButton);
buttonPanel.add(browseButton);
buttonPanel.add(searchButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToAddPage();
}
});
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToBrowse();
}
});
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
mdbw.goToSearch();
}
});
JFrame frame = new JFrame("Memory Files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 540);
frame.setLocation(250, 100);
frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private CardLayout cardLayout = new CardLayout();
private JPanel cardShowingPanel = new JPanel(cardLayout);
public MyDatabaseWindow() {
Window1 win1 = new Window1();
cardShowingPanel.add(win1, FRONT_PAGE);
Window2 win2 = new Window2();
cardShowingPanel.add(win2, BROWSE_MEMORIES);
Window3 win3 = new Window3();
cardShowingPanel.add(win3, ADD_EDIT);
Window4 win4 = new Window4();
cardShowingPanel.add(win4, VIEW_MEMORY);
setLayout(new BorderLayout());
add(cardShowingPanel, BorderLayout.NORTH);
}
public void goToAddPage() {
cardLayout.first(cardShowingPanel);
}
public void goToBrowse() {
cardLayout.first(cardShowingPanel);
cardLayout.next(cardShowingPanel);
}
public void goToSearch() {
cardLayout.last(cardShowingPanel);
}
public void showCard(String key) {
cardLayout.show(cardShowingPanel, key);
}
}
class Window1 extends JPanel {
public Window1() {
init();
}
private void init() { //dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window2 extends JPanel {
public Window2() {
init();
}
private void init() { //dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window3 extends JPanel {
public Window3() {
init();
}
private void init() {//dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
class Window4 extends JPanel {
public Window4() {
init();
}
private void init() {//dummy details
JLabel title = new JLabel("Memory Files");
title.setBounds(0, 0, 500, 500);
add(title);
}
}
The main problem is the use of null-layouts and these lines of code:
frame.getContentPane().add(mdbw);
frame.getContentPane().add(buttonPanel);
First you add the panel using the CardLayout to BorderLayout.CENTER, then you "overlay" it with your buttonPanel, which is using null-layout.
I would go with a simple FlowLayout (the default layout-manager for a JPanel) for the buttonPanel and add it to the BorderLayout.SOUTH of the contentPane. I would also strongly recommend reading this tutorial.
So remove the following lines of code:
buttonPanel.setLayout(null);
...
addButton.setBounds(100, 400, 100, 100);
editButton.setBounds(200, 400, 100, 100);
deleteButton.setBounds(300, 400, 100, 100);
browseButton.setBounds(400, 400, 100, 100);
searchButton.setBounds(500, 400, 100, 100);
and change frame.getContentPane().add(buttonPanel); to frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);.
Also forget about the null-layout / setBounds() in your Window-classes.
(Note that you still won't see the text change if you press a button because you always add a JLabel with the same text ("Memory Files") to your Windows.)
I'm working on this project in Java and need an image to display, along with a bio, and button that plays a song/sound. I finished the button and bio but I can figure out how to get the image to display in the NORTH part of the layout along with the button in the center part, any help would be great.
This is my error: The method add(String, Component) in the type Container is not applicable for the arguments (Image, String)
public void init() {
// image
myPicture = getImage(getCodeBase(), "sample.jpg");
// THIS IS WHERE MY PROBLEM IS vvvvvvv
add(myPicture, BorderLayout.NORTH);
add(bio);
paneSouth.add(play);
getContentPane().add(paneSouth, BorderLayout.SOUTH);
mySound = getAudioClip(getDocumentBase(), "sample.wav");
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mySound.play();
}});
}
public void paint(Graphics g){
g.drawImage(myPicture, 10, 10, this);
}
private JPanel paneSouth = new JPanel();
private TextArea bio = new TextArea("bio thing");
private JButton play = new JButton("Play");
private AudioClip mySound;
private Image myPicture;
}
EDIT:
public void init() {
// image
ImageIcon icon = new ImageIcon(myPicture);
JLabel myLabelImage = new Image(icon);
add(myLabelImage, BorderLayout.NORTH);
// bio
add(bio);
add(bio, BorderLayout.CENTER);
// sound
paneSouth.add(play);
getContentPane().add(paneSouth, BorderLayout.SOUTH);
mySound = getAudioClip(getDocumentBase(), "sample.wav");
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mySound.play();
}});
}
private JPanel paneSouth = new JPanel();
private TextArea bio = new TextArea("bio.");
private JButton play = new JButton("Play");
private AudioClip mySound;
private Image myPicture;
}
I guess you are using JApplet because you are using Swing components. Try something like this:
public class ImageApplet extends JApplet {
private JPanel paneSouth = new JPanel();
private JTextArea bio = new JTextArea("bio thing");
private JButton play = new JButton("Play");
private Image myPicture;
private ImageIcon icon;
private JLabel label;
public void init() {
try {
URL pic = new URL(getDocumentBase(), "sample.jpg");
myPicture = ImageIO.read(pic);
icon = new ImageIcon(myPicture);
label = new JLabel(icon);
} catch (Exception e) {
e.printStackTrace();
}
// add image
add(label, BorderLayout.NORTH);
// bio
add(bio, BorderLayout.CENTER);
// sound
paneSouth.add(play);
add(paneSouth, BorderLayout.SOUTH);
// here add your sound declaration and button event...
}
#Override
public void paint(Graphics g) {
super.paint(g);
}
}
And try change your TextArea for a JTextArea since you are only using swing components.
so this is the current code and it has been so far working except for the background color, anything that has "//" before it is what causes errors between other things
package com.Luminate.luminatemedia;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Window extends JFrame implements ActionListener{
static JFrame mediaform = new JFrame();
public static void main(String[] args) {
}
public static void load() {
ImageIcon fileIcon = new ImageIcon ("assets/icon.png");
ImageIcon exitIcon = new ImageIcon ("assets/exit.png");
JButton exitButton = new JButton();
JLabel text = new JLabel("Luminate Media");
JPanel panel = new JPanel();
mediaform.add(panel);
panel.add(exitButton);
panel.add(text);
mediaform.setTitle("Luminate");
mediaform.setIconImage(fileIcon.getImage());
mediaform.setBounds(0, 688, 1366, 40);
//mediaform.setLayout(null);
mediaform.setUndecorated(true);
mediaform.setResizable(false);
mediaform.setVisible(true);
mediaform.setAlwaysOnTop(true);
//mediaform.getContentPane().setBackground(Color.DARK_GRAY);
mediaform.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text.setFont(new Font("dandelion in the spring", Font.PLAIN, 32));
text.setBounds(0, 0, 0, 0);
text.setHorizontalAlignment(SwingConstants.CENTER);
text.setSize(0, 0);
text.setForeground(Color.black);
text.setHorizontalAlignment(0);
exitButton.setBorder(null);
exitButton.setBounds(1326, 0, 40, 40);
exitButton.addActionListener(null);
exitButton.setIcon(exitIcon);
//BELOW IS MY ISSUE
exitButton.addActionListener(this);
panel.setLayout(null);
panel.setBackground(DARK_GRAY);
}
#Override
public void actionPerformed(ActionEvent e) {
//code to close the whole program
}
}
so there's an error with the emphasized text where it says "this"
also what would the code be to close the main class at the click of the button and am I doing this correctly ?
Try this
public class Window extends JFrame implements ActionListener{
public static void main(String[] args) {
new Window();
}
public Window() {
ImageIcon fileIcon = new ImageIcon ("assets/icon.png");
ImageIcon exitIcon = new ImageIcon ("assets/exit.png");
JButton exitButton = new JButton("Label");
JLabel text = new JLabel("Luminate Media");
JPanel panel = new JPanel();
setTitle("Luminate");
setIconImage(fileIcon.getImage());
setBounds(0, 688, 1366, 40);
setLayout(null);
setUndecorated(true);
setResizable(false);
setVisible(true);
setAlwaysOnTop(true);
getContentPane().setBackground(Color.DARK_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text.setFont(new Font("dandelion in the spring", Font.PLAIN, 32));
text.setBounds(0, 0, 0, 0);
text.setHorizontalAlignment(SwingConstants.CENTER);
text.setSize(0, 0);
text.setForeground(Color.black);
text.setHorizontalAlignment(0);
exitButton.setBorder(null);
exitButton.setBounds(1326, 0, 40, 40);
exitButton.setIcon(exitIcon);
exitButton.addActionListener(this);
panel.setLayout(null);
panel.setBackground(DARK_GRAY);
panel.add(exitButton);
panel.add(text);
add(panel);
}
#Override
public void actionPerformed(ActionEvent e) {
//code to close the whole program
}
}
I want to create a "preview window" for my "main window", so if I take mouse pointer to a particular button or place then it show main window preview in a small window, and when I click on that "preview window" then it should return to the "main window". For example Windows 7 task bar creates a preview for each opened application. How can I can offer this feature to my users?
E.G.
An extremely simplistic implementation.
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
class ShowPreviews {
class ToolTipListener extends MouseAdapter {
JWindow toolTip;
JLabel label;
Component preview;
ToolTipListener(Component preview) {
this.preview = preview;
}
#Override
public void mouseEntered(MouseEvent me) {
if (toolTip==null) {
toolTip = new JWindow();
label = new JLabel();
toolTip.add(label);
}
label.setIcon( new ImageIcon(
getScaledImageOfComponent(preview, .6) ) );
toolTip.pack();
Component c = (Component)me.getSource();
int x = c.getLocationOnScreen().x+(c.getWidth()/2);
int y = c.getLocationOnScreen().y+c.getHeight();
toolTip.setLocation(x,y);
toolTip.setVisible(true);
}
#Override
public void mouseExited(MouseEvent me) {
toolTip.setVisible(false);
toolTip.dispose();
}
public Image getScaledImageOfComponent(
Component component, double scale) {
BufferedImage bi = new BufferedImage(
(int)(component.getWidth()*scale),
(int)(component.getHeight()*scale),
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.scale(scale, scale);
component.paint(g);
g.dispose();
return bi;
}
}
ShowPreviews() {
JPanel gui = new JPanel(new BorderLayout(2,2));
final CardLayout cards = new CardLayout();
final JPanel cardPanel = new JPanel(cards);
JPanel treePanel = new JPanel();
JTree tree = new JTree();
tree.setVisibleRowCount(5);
tree.expandRow(2);
treePanel.add(new JScrollPane(tree));
cardPanel.add(treePanel, "tree");
JPanel labelPanel = new JPanel(new GridLayout(0,1,2,2));
for (int ii=1; ii<7; ii++) {
labelPanel.add(new JLabel("Label " + ii));
}
cardPanel.add(new JScrollPane(labelPanel), "label");
JToolBar uiSelectors = new JToolBar();
// we should use a ButtonGroup for the cards,
// but plain buttons look better on hover.
JButton treeButton = new JButton("Tree");
treeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
cards.show(cardPanel, "tree");
}
});
uiSelectors.add(treeButton);
treeButton.addMouseListener( new ToolTipListener(treePanel));
JButton labelButton = new JButton("Label");
labelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
cards.show(cardPanel, "label");
}
});
uiSelectors.add(labelButton);
labelButton.addMouseListener( new ToolTipListener(labelPanel));
gui.add(uiSelectors, BorderLayout.NORTH);
gui.add(cardPanel, BorderLayout.CENTER);
JOptionPane.showMessageDialog(null, gui);
}
public static void main(String[] args) {
// start the GUI on the EDT
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ShowPreviews();
}
});
}
}