Background Image for MDI Form using java

Step 1: Create a class that inherit(extends) JDesktopPane class

class JDesktopBackground extends JDesktopPane {

private Image image;
private BufferedImage scaledImage;
private int cachedHeight = -1;
private int cachedWidth = -1;
public JDesktopBackground(Image image) {
super();
this.image = image;
}

protected void paintComponent(Graphics g) {
if (cachedHeight != getHeight() && cachedWidth != getWidth())
createScaledImage();
g.drawImage(scaledImage, 0, 0, this);
}

private void createScaledImage() {
cachedWidth = getWidth();
cachedHeight = getHeight();
if (cachedWidth > 0 && cachedHeight > 0) {
scaledImage = new BufferedImage(cachedWidth, cachedHeight,
BufferedImage.TYPE_INT_RGB);
scaledImage.createGraphics().drawImage(
image,
AffineTransform.getScaleInstance(((float) cachedWidth)
/ image.getWidth(this), ((float) cachedHeight)
/ image.getHeight(this)), null);
} else
scaledImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
}

}
Step 2: Create MDI Form
Here i create MDI Form named addpicinmdi and do the following (see statement written in italic forms)

public class addpicinmdi extends javax.swing.JFrame {

JDesktopPane desktopPane;
BufferedImage bf=null;
public addpicinmdi() throws IOException {
bf=ImageIO.read(getClass().getResourceAsStream("/test/r.jpg"));
//initialize variable
desktopPane = new JDesktopBackground(bf);

}

keyboard shortcut for a button using java

Here it show how to install a keyboard shortcut to a Swing JButton so the button behaves as though it was clicked when the key is pressed.

JButton jButton1 = new JButton();
jButton1.setText("Save");

int key=2 ;
//key=1->shift, key =2 -> ctrl
InputMap inputMap = jButton1.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW);
KeyStroke sav = KeyStroke.getKeyStroke(KeyEvent.VK_S, key);
inputMap.put(sav, "Save");
jButton1.getActionMap().put("Save", new ClickAction(jButton1));

jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("Save Operation");
}
});

------------------
------------------

class ClickAction1 extends AbstractAction {
private JButton button;

public ClickAction1(JButton button) {
this.button = button;
}

public void actionPerformed(ActionEvent e) {
button.doClick();
}
}