How to change jdesktoppane default background image? - java

How to change jdesktoppane background image in MDI (Multiple Documents interface) using java netbeans? Means I added the jdesktoppane to java MDI so now I want to change default background image of that jdesktoppane which I'm using in java MDI. Any easy way?
Check attached snapshot link may be you will better understand my question what I want.
http://i50.tinypic.com/iml1e9.jpg

+1 to MadProgrammers comment.
Simply override JDesktopPane paintComponent(..) and call drawImage(Image img,int x,int y,ImageObserver io) to draw an image.
Dont forget to honor the paint chain and call super.paintComponent(g) as first call in overridden paintComponent(..) method
Here is an example:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class JInternalFrameDemo {
private JDesktopPane jdpDesktop;
private static int openFrameCount = 0;
private BufferedImage img;
public JInternalFrameDemo() {
JFrame frame = new JFrame("JInternalFrame Usage Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
img = ImageIO.read(new URL("http://images1.wikia.nocookie.net/__cb20120817224359/villains/images/6/6a/Nine-Tailed_Fox_(Naruto).jpg"));
} catch (Exception ex) {
ex.printStackTrace();
}
// A specialized layered pane to be used with JInternalFrames
jdpDesktop = new JDesktopPane() {
#Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
grphcs.drawImage(img, 0, 0, null);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(img.getWidth(), img.getHeight());
}
};
createFrame(); // Create first window
frame.setContentPane(jdpDesktop);
frame.setJMenuBar(createMenuBar());
// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
frame.pack();
frame.setVisible(true);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Frame");
menu.setMnemonic(KeyEvent.VK_N);
JMenuItem menuItem = new JMenuItem("New IFrame");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createFrame();
}
});
menu.add(menuItem);
menuBar.add(menu);
return menuBar;
}
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new JInternalFrameDemo();
}
});
}
class MyInternalFrame extends JInternalFrame {
static final int xPosition = 30, yPosition = 30;
public MyInternalFrame() {
super("IFrame #" + (++openFrameCount), true, // resizable
true, // closable
true, // maximizable
true);// iconifiable
setSize(300, 300);
// Set the window's location.
setLocation(xPosition * openFrameCount, yPosition
* openFrameCount);
}
}
}

I resolve it in a separate function to create a desktop object.
Code as below
private JDesktopPane intializeDesktop(JDesktopPane mydesktop,String imagePath,int scalx,int scaly) {
// A specialized layered pane to be used with JInternalFrames
mydesktop = new JDesktopPane() {
ImageIcon icon = new ImageIcon(imagePath);
Image image = icon.getImage();
Image newimage = image.getScaledInstance(scalx, scaly, Image.SCALE_SMOOTH);
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(newimage, 0, 0, this);
}
};
return mydesktop;
}

Related

My Save Method is only saving the Background and not the Draw Image in Java GUI

I'm making a Java GUI that has a panel where you can scribble on it. I want to save the scribbled image after drawing it. However, my save method only saves the background and not the drawing.
Here is part of the constructor for my GUI. The important part is the scribblePane2 object which is the section of the GUI where the image is drawn.
public GUI(){
// Create the main scribble pane component, give it a border, and
// a background color, and add it to the content pane
scribblePane = new ScribblePane2();
scribblePane.setBorder(new BevelBorder(BevelBorder.LOWERED));
scribblePane.setBackground(Color.black);
contentPane.add(scribblePane, BorderLayout.CENTER);
}
Here is the scribblePane2 class where I take the mouse input to draw the image in the panel. The method "lineto" is where I draw the lines in response to mouse input. I tried drawing to the buffered image, bufferImage, but it did not work for me.
class ScribblePane2 extends JPanel {
BufferedImage bufferImage;
public ScribblePane2() {
// Give the component a preferred size
setPreferredSize(new Dimension(200, 200));
bufferImage = new BufferedImage(200, 200,
BufferedImage.TYPE_INT_ARGB);
Graphics2D bufferGraphics = bufferImage.createGraphics();
}
/** Draw from the last point to this point, then remember new point */
public void lineto(int x, int y) {
Graphics g = getGraphics();
g.setColor(color);
((Graphics2D) g).setStroke(new BasicStroke(10));// Tell it what color to use
g.drawLine(last_x, last_y, x, y);
moveto(x, y);
}
public BufferedImage getImage (){
return bufferImage;
}
}
Here is my method to save the image. I tried by creating a Buffered Image of the scribblePane and a doing the screen capture which is the line commented out. Both have not worked.
public static void saveDrawing(int i) {
BufferedImage imagebuf = null;
//imagebuf = new Robot().createScreenCapture(scribblePane.getBounds());
imagebuf = new BufferedImage(scribblePane.WIDTH, scribblePane.HEIGHT, BufferedImage.TYPE_INT_RGB);
//imagebuf = scribblePane.getImage();
Graphics2D graphics2D = imagebuf.createGraphics();
scribblePane.paint(graphics2D);
try {
ImageIO.write(imagebuf, "png", new File("save" + i +".png"));
System.out.println("image saved");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("error");
}
}
For reference, here is all of my code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import javax.imageio.ImageIO;
public class GUI {
private static int i;
private static JButton button;
private static JButton button2;
private static JLabel label;
private static ScribblePane2 scribblePane;
private static JPanel contentPane;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;
//private Bitmap mBitmap;
private Canvas mCanvas;
BufferedImage bufferImage;
public static void main(String[] args){
new GUI();
setUpButtonListeners();
// Scribble scribble = new Scribble();
//scribble.setSize(500, 300);
//scribble.setVisible(true);
}
public GUI(){
JFrame frame = new JFrame();
frame.setSize(300,300);
GridBagLayout layout = new GridBagLayout();
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
label = new JLabel("number");
JLabel label2 = new JLabel("Hi I'm contentPane");
contentPane = new JPanel();
contentPane.add(label2);
button = new JButton("Classify Image");
button2 = new JButton("Erase Image");
// Specify a layout manager for the content pane
contentPane.setLayout(new BorderLayout());
// Create the main scribble pane component, give it a border, and
// a background color, and add it to the content pane
scribblePane = new ScribblePane2();
scribblePane.setBorder(new BevelBorder(BevelBorder.LOWERED));
scribblePane.setBackground(Color.black);
contentPane.add(scribblePane, BorderLayout.CENTER);
// Create a menubar and add it to this window. Note that JFrame
// handles menus specially and has a special method for adding them
// outside of the content pane.
JMenuBar menubar = new JMenuBar(); // Create a menubar
//frame.setJMenuBar(menubar); // Display it in the JFrame
// Create menus and add to the menubar
JMenu filemenu = new JMenu("File");
JMenu colormenu = new JMenu("Color");
menubar.add(filemenu);
// menubar.add(colormenu);
// Create some Action objects for use in the menus and toolbars.
// An Action combines a menu title and/or icon with an ActionListener.
// These Action classes are defined as inner classes below.
Action black = new ColorAction(Color.black);
Action red = new ColorAction(Color.red);
Action blue = new ColorAction(Color.blue);
// Populate the menus using Action objects
// colormenu.add(black);
// Now create a toolbar, add actions to it, and add it to the
// top of the frame (where it appears underneath the menubar)
JToolBar toolbar = new JToolBar();
// contentPane.add(toolbar, BorderLayout.NORTH);
// Create another toolbar for use as a color palette and add to
// the left side of the window.
JToolBar palette = new JToolBar();
palette.add(black);
palette.setOrientation(SwingConstants.VERTICAL);
// contentPane.add(palette, BorderLayout.WEST);
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(layout);
panel2.setLayout(new BorderLayout());
panel2.add(label, BorderLayout.WEST);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(contentPane, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(panel2,gbc);
gbc.gridx = 0;
gbc.gridy = 2;
//gbc.fill = GridBagConstraints.HORIZONTAL;
//gbc.gridwidth = 2;
panel.add(button, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
panel.add(button2, gbc);
//panel.setLayout(new BorderLayout());
//panel.add(button, BorderLayout.WEST);
//panel.add(button2, BorderLayout.EAST);
//panel.add(label, BorderLayout.NORTH);
//panel.add(contentPane, BorderLayout.SOUTH);
frame.add(panel);
frame.setTitle("GUI");
frame.pack();
frame.setVisible(true);
}
public static void setUpButtonListeners(){
ActionListener buttonListener = new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
System.out.println("save");
int output = 0;
saveDrawing(i);
i++;
label.setText("" + output);
}
};
ActionListener buttonListener2 = new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
System.out.println("erase");
scribblePane.clear();
label.setText("");
}
};
button.addActionListener(buttonListener);
button2.addActionListener(buttonListener2);
}
public static void saveDrawing(int i) {
//throws AWTException {
BufferedImage imagebuf = null;
//imagebuf = new Robot().createScreenCapture(scribblePane.getBounds());
imagebuf = new BufferedImage(scribblePane.WIDTH, scribblePane.HEIGHT, BufferedImage.TYPE_INT_RGB);
//imagebuf = scribblePane.getImage();
Graphics2D graphics2D = imagebuf.createGraphics();
scribblePane.paint(graphics2D);
try {
ImageIO.write(imagebuf, "png", new File("save" + i +".png"));
System.out.println("image saved");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("error");
}
}
private void loadImageFromStorage(String path)
{
System.out.println("load");
//use a fileInputStream to read the file in a try / catch block
// try {
// File f=new File(path, "drawn_image.jpg");
// Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
// ImageView img=(ImageView)findViewById(R.id.outputView);
// img.setImageBitmap(b);
// }
// catch (FileNotFoundException e)
// {
/// e.printStackTrace();
// }
}
/** This inner class defines the "clear" action that clears the scribble */
/** This inner class defines the "quit" action to quit the program */
/**
* This inner class defines an Action that sets the current drawing color of
* the ScribblePane2 component. Note that actions of this type have icons
* rather than labels
*/
class ColorAction extends AbstractAction {
Color color;
public ColorAction(Color color) {
this.color = color;
putValue(Action.SMALL_ICON, new ColorIcon(color)); // specify icon
}
public void actionPerformed(ActionEvent e) {
scribblePane.setColor(color); // Set current drawing color
}
}
/**
* This inner class implements Icon to draw a solid 16x16 block of the
* specified color. Most icons are instances of ImageIcon, but since we're
* only using solid colors here, it is easier to implement this custom Icon
* type
*/
static class ColorIcon implements Icon {
Color color;
public ColorIcon(Color color) {
this.color = color;
}
// These two methods specify the size of the icon
public int getIconHeight() {
return 16;
}
public int getIconWidth() {
return 16;
}
// This method draws the icon
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(color);
g.fillRect(x, y, 16, 16);
}
}
}
class ScribblePane2 extends JPanel {
BufferedImage bufferImage;
public ScribblePane2() {
// Give the component a preferred size
setPreferredSize(new Dimension(200, 200));
bufferImage = new BufferedImage(200, 200,
BufferedImage.TYPE_INT_ARGB);
Graphics2D bufferGraphics = bufferImage.createGraphics();
// Register a mouse event handler defined as an inner class
// Note the call to requestFocus(). This is required in order for
// the component to receive key events.
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
moveto(e.getX(), e.getY()); // Move to click position
requestFocus(); // Take keyboard focus
}
});
// Register a mouse motion event handler defined as an inner class
// By subclassing MouseMotionAdapter rather than implementing
// MouseMotionListener, we only override the method we're interested
// in and inherit default (empty) implementations of the other methods.
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
lineto(e.getX(), e.getY()); // Draw to mouse position
}
});
// Add a keyboard event handler to clear the screen on key 'C'
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_C)
clear();
}
});
}
/** These are the coordinates of the the previous mouse position */
protected int last_x, last_y;
/** Remember the specified point */
public void moveto(int x, int y) {
last_x = x;
last_y = y;
}
/** Draw from the last point to this point, then remember new point */
public void lineto(int x, int y) {
Graphics g = getGraphics();
/// BufferedImage newBufferedImage = new BufferedImage(200,200,
//BufferedImage.TYPE_INT_ARGB);
/// Graphics g = newBufferedImage.getGraphics();
// Get the object to draw with
g.setColor(color);
((Graphics2D) g).setStroke(new BasicStroke(10));// Tell it what color to use
g.drawLine(last_x, last_y, x, y); // Tell it what to draw
//g.drawImage(bufferImage, x,y,null);
//bufferImage = newBufferedImage;// Save the current point
moveto(x, y);
}
public BufferedImage getImage (){
return bufferImage;
}
/**
* Clear the drawing area, using the component background color. This method
* works by requesting that the component be redrawn. Since this component
* does not have a paintComponent() method, nothing will be drawn. However,
* other parts of the component, such as borders or sub-components will be
* drawn correctly.
*/
public void clear() {
repaint();
}
/** This field holds the current drawing color property */
Color color = Color.white;
/** This is the property "setter" method for the color property */
public void setColor(Color color) {
this.color = color;
}
/** This is the property "getter" method for the color property */
public Color getColor() {
return color;
}
}

How would I add an action Listener to the button to refresh the image? [duplicate]

I have been trying to figure this out why not the next picture showing on the same panel after click the button. I want to separate those classes not into one class and used repaint() to re-invoke paintComponent() with the new pic.
Please help me. I am almost dying :(
when I run this, the first picture appears well. when the button is clicked to change the first picture to the second one, the Panel just keep on showing the first picture.
Thank you.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
class drawImage extends JPanel {
BufferedImage[] b = new BufferedImage[2];
public drawImage() {
try {
b[0] = ImageIO.read(new File("img/gameOn.png"));
b[1] = ImageIO.read(new File("img/gameOff.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(b[0], 0, 0, null);
}
public void setNextImage(BufferedImage image) {
b[0] = image;
repaint();
}
public BufferedImage getB0() {
return b[0];
}
public BufferedImage getB1() {
return b[1];
}
}// end drawImage
class clickedListener implements ActionListener {
BufferedImage pre = new drawImage().getB0();
BufferedImage next = new drawImage().getB1();
#Override
public void actionPerformed(ActionEvent e) {
new drawImage().setNextImage(next);
}
}
public class buttonFrame {
public static void main(String[] args) throws IOException {
JFrame jf = new JFrame("Button & Frame");
JButton btn = new JButton("Click");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.setLayout(new GridLayout(2, 0));
jf.add(new drawImage());
jf.add(btn);
jf.setSize(200, 250);
btn.addActionListener(new clickedListener());
}
}
Why not change your approach and make use of a JLabel instead? Set your image as an icon on the label and add it to your JPanel:
BufferedImage image = ImageIO.read(new File("image-path"));
JLabel label = new JLabel(new ImageIcon(image));
panel.add(label);
You can then make subsequent calls to JLabel#setIcon(...) each time you want the image to change.
You can also use ImageIcon like this
image = new ImageIcon(imageList[1]);
and when each time button is clicked you can change image like this
label.setIcon(image);

Gif not playing in JFrame

Hello, I have a gif in a JFrame. It all works fine except the gif is frozen on the first time as though it is an jpeg or png.
Also, the stackoverflow is telling me I need to add more details even though I have added all the details required in order to state my problem, so feel free to ignore this.
Here is the code:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class LoadImageApp extends Component {
BufferedImage img;
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
public LoadImageApp() {
try {
img = ImageIO.read(new File("spooky.gif"));
} catch (IOException e) {
}
}
public Dimension getPreferredSize() {
if (img == null) {
return new Dimension(100,100);
} else {
return new Dimension(img.getWidth(null), img.getHeight(null));
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.add(new LoadImageApp());
f.pack();
f.setVisible(true);
}
}
I'm pretty sure ImageIO has a few problems with animated gifs.
Try using a JLabel and and Icon, like so:
Icon icon = new ImageIcon(filename);
JLabel gif = new JLabel(icon);
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.getContentPane().add(gif);
f.pack();
f.setVisible(true);
As mentioned, ImageIO will not correctly load all the frames of an animated GIF. But one of the Toolkit methods will. E.G.
import java.awt.*;
import javax.swing.*;
import java.net.URL;
// Ach AWT!
//public class LoadImageApp extends Component {
public class LoadImageApp extends JPanel {
Image img;
// should be paintComponent for a JComponent
//public void paint(Graphics g) {
public void paintComponent(Graphics g) {
super.paintComponent(g); // call super method first
g.drawImage(img, 0, 0, this);
}
public LoadImageApp() {
setBackground(Color.BLUE.darker().darker());
try {
URL url = new URL("http://i.stack.imgur.com/OtTIY.gif");
// as mentioned, ImageIO will not load animated GIFs correctly.
//img = ImageIO.read(url));
// but the toolkit method will, OTOH..
img = Toolkit.getDefaultToolkit().createImage(url);
// ..we need a MediaTracker
MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 0);
mt.waitForAll();
} catch (Exception e) {
e.printStackTrace();
}
}
public Dimension getPreferredSize() {
if (img == null) {
return new Dimension(100, 100);
} else {
return new Dimension(img.getWidth(this), img.getHeight(this));
}
}
public static void main(String[] args) {
// should be donw on the EDT - BNI
JFrame f = new JFrame("Load Image Sample");
f.add(new LoadImageApp());
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
For further discussion, see Show an animated BG in Swing.

How do I build a JMenu dynamically?

I am trying to build a JMenu dynamically right when clicking in it (I'm only getting an empty menu), below is my code.
final JMenu JMWindows = new JMenu("Opened Windows");
JMWindows.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for(JInternalFrame ji : desktop.getAllFrames())
{
JMWindows.add(ji.getTitle());
}
}
});
I've realized that the actionperformed is never called, the JMenu is inside a JMenuBar. What could be the problem ?
You add ActionListener to JMenu this cannot be done for JMenu use MenuListener and set it via JMenu#addMenuListener(..)
Also you need to call revalidate() and repaint() on JMenu instance after adding/removing components from it or the changes will not be reflected (we could also call this on the containers instance i.e JMenuBar or JFrame, but we know only 1 specific JMenu will change so no need IMO).
Please watch your variable naming schemes JMWindow should be jmWindow/jMWindow variables always begin with no caps and the 1st letter of every new word thereafter gets capitalized (besides for constants and enums).
Here is an example using MenuListener:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
public class Test {
private JDesktopPane jdpDesktop;
private static int openFrameCount = 0;
final JFrame frame = new JFrame("JInternalFrame Usage Demo");
public Test() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// A specialized layered pane to be used with JInternalFrames
jdpDesktop = new JDesktopPane() {
#Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
};
for (int i = 0; i < 3; i++) {
createFrame(); // Create first window
}
frame.setContentPane(jdpDesktop);
frame.setJMenuBar(createMenuBar());
// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
frame.pack();
frame.setVisible(true);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Frame");
menu.setMnemonic(KeyEvent.VK_N);
JMenuItem menuItem = new JMenuItem("New IFrame");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createFrame();
}
});
menu.add(menuItem);
menuBar.add(menu);
final JMenu jmWindows = new JMenu("Opened Windows");
jmWindows.addMenuListener(new MenuListener() {
#Override
public void menuSelected(MenuEvent me) {
jmWindows.removeAll();//remove previous opened window jmenuitems
for (JInternalFrame ji : jdpDesktop.getAllFrames()) {
JMenuItem menuItem = new JMenuItem(ji.getTitle());
jmWindows.add(menuItem);
}
jmWindows.revalidate();
jmWindows.repaint();
jmWindows.doClick();
}
#Override
public void menuDeselected(MenuEvent me) {
}
#Override
public void menuCanceled(MenuEvent me) {
}
});
menuBar.add(jmWindows);
return menuBar;
}
protected void createFrame() {
Test.MyInternalFrame frame = new Test.MyInternalFrame();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Test();
}
});
}
class MyInternalFrame extends JInternalFrame {
static final int xPosition = 30, yPosition = 30;
public MyInternalFrame() {
super("IFrame #" + (++openFrameCount), true, // resizable
true, // closable
true, // maximizable
true);// iconifiable
setSize(300, 300);
// Set the window's location.
setLocation(xPosition * openFrameCount, yPosition
* openFrameCount);
}
}
}
You need to repaint and or validate your frame after adding or removing something.
you can use the methods repaint() and validate() in your JFrame for this
You missed defining a JMenuItem.
final JMenu JMWindows = new JMenu("Opened Windows");
JMWindows.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for(JInternalFrame ji : desktop.getAllFrames())
{
JMenuItem menuItem = new JMenuItem(ji.getTitle());
JMWindows.add(menuItem);
}
}
});

Example Program JMenubar on JInternalFrame when i Maximize the JInternalFrame

Hi I need an Example program in which
When i maximize the JInternalFrame the JMenuBar of JFrame should set on JInternalFrame
and When i minimize the JInternalFrame again the JMenuBar should leave JinternalFrame and set
to JFrame as shown below
Please provide me an Example Program in Java Swing
Seems to work fine, please post SSCCE to show specific problem:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class JInternalFrameDemo {
JDesktopPane jdpDesktop;
static int openFrameCount = 0;
public JInternalFrameDemo() {
JFrame frame = new JFrame("JInternalFrame Usage Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// A specialized layered pane to be used with JInternalFrames
jdpDesktop = new JDesktopPane() {
#Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
};
createFrame(); // Create first window
frame.setContentPane(jdpDesktop);
frame.setJMenuBar(createMenuBar());
// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
frame.pack();
frame.setVisible(true);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Frame");
menu.setMnemonic(KeyEvent.VK_N);
JMenuItem menuItem = new JMenuItem("New IFrame");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createFrame();
}
});
menu.add(menuItem);
menuBar.add(menu);
return menuBar;
}
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new JInternalFrameDemo();
}
});
}
class MyInternalFrame extends JInternalFrame {
static final int xPosition = 30, yPosition = 30;
public MyInternalFrame() {
super("IFrame #" + (++openFrameCount), true, // resizable
true, // closable
true, // maximizable
true);// iconifiable
setSize(300, 300);
// Set the window's location.
setLocation(xPosition * openFrameCount, yPosition
* openFrameCount);
}
}
}
I also have similar problem. It is a bug in JInternalFrame LookAndFell. JInternalFrames using Windows L&F, should be
more Windows like. If I maximize a JInternalWindow,
that window is not working as in Windows.
Its top bar should be one with the MDI.
Meus and Toolbars should be inside.
Maximized Internal frames should not have their
own border.
Visit
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4102061
for datails.

Categories

Resources