SETTING .NET APPLICATION TO RUN ON WINDOWS STARTUP

It becomes sometimes tricky and difficult just to set your application to run from start up of the operating system.

To start with just create a class like the one below

Imports Microsoft.Win32

 

Public Class RegSettings

‘Setting the source where startup programs runs from the registry

 Const RunAtStartupKey As String = "Software\Microsoft\Windows\CurrentVersion\Run"

 Public Shared Property RunAtStartUp() As Boolean

 Get

 Try

 

Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(RunAtStartupKey)

‘User_face been the application name the will be stored in the registry

Return CStr(key.GetValue("User_Face", String.Empty)) <> String.Empty

Catch

‘ No errors if something goes wrong.

‘ Just fail silently.

End Try

End Get

Set(ByVal Value As Boolean)

Try

‘ Dim reg As Registry

Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey(RunAtStartupKey, True)

If Value Then

‘setting the source of where the application should run from

key.SetValue("User_Face", Application.ExecutablePath)

Else

key.DeleteValue("User_Face")

End If

Catch

‘ No errors if something goes wrong.

‘ Just fail silently.

End Try

End Set

End Property

End Class

You then create a form add a check box and a button

Add the code below to the button click event

RegSettings.RunAtStartUp = Me.chkStarup.Checked

Where "chkStarup” will be the check box name

HOW TO BIND KEYS TO CONTROLS IN JAVA

This is a simple method to bind keys to controls in java. Sometimes you may also want to change the way by default a control respond to a key press. For example when the “Enter” is pressed it navigates through the rows and column but if you want to change this behavior then you are at the correct place.

Below is a simple method to get you through.

private void tbProductKeypress() {

tbProducts.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),"check1");

tbProducts.getActionMap().put("check1", new AbstractAction() {

public void actionPerformed(ActionEvent e) {

//add your code here

}

});

}

Note:This method is added after the control has been created. Also tbProducts is the name of the JTable (control) to bind the key to.

Creating MDI Applications in Java Using Netbeans

This is a simple process for Java users who want to create Multiple Document Interface Applications in Java. The application makes use of JPanels , a JFrame and a JDesktopPane.

To start with, all your child forms should be created using panels (JPanels) as the container. A frame is then created as the parent form. A desktop pane is then added to the frame to be container for the panels on the JFrame.

Below is how the application Works

1.   Create your child forms on a panel

2.   Create a frame and add a desktoppane to it as shown below

main

The black color is showing the JDesktopPane. You can set the “Anchor” and “Auto Resizing” properties of the DesktopPane so that it fits the desired size of your choice.

Add the method below to your Frame Class

public void addFrame(JPanel panel, String title) {

try {

JInternalFrame[] children = jDesktopPane1.getAllFrames();

//creating a new internal frame from the Panel passes as parameter

internalframe j = new internalframe(panel, title);

//checking to see if the child form already exist

for (JInternalFrame f : children) {

if (f.getTitle().equals(j.getTitle())) {

f.setSelected(true);

return;

}

}

//adding a new child

jDesktopPane1.add(j);

j.setVisible(true);

j.setSelected(true);

} catch (PropertyVetoException ex) {

Logger.getLogger(mainxpframe.class.getName()).log(Level.SEVERE, null, ex);

}

}

To show the child forms add the code below to the button click event

customersdetailpanel sd=new customersdetailpanel();

sd.setSize(750, 600);

addFrame(sd, "Customers Information");

the “customersdetailpanel” is one of the child forms.

Below is an interface showing showing the MDI Application

sub

Hope this will be helpful

SAVING IMAGES IN A DATABASE USING EJBS

It is sometimes troublesome to save and retrieve images in  a database using java. Below are methods that are useful in saving and retrieving images from a database

To save an image in a database the datatype of the column should be “blob”. One thing to note is that in the EJBs the blob datatype is change to an array of bytes (“byte[]”)

A function to change an image file into its bytes

public Function byte[] setImagebytes(File imageFile) {

byte[] imagebytes = null;

try {

FileInputStream fis = new FileInputStream(imageFile);

imagebytes = new byte[(int) imageFile.length()];

fis.read(imagebytes);

} catch (FileNotFoundException ex) {

ex.printStackTrace();

} catch (IOException ex) {

ex.printStackTrace();

}

Return imagebytes;

}

A method to browse for an image file from its location and show it in a label

private void btBrowseActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

// a method to browse for a picture and pass the image file to the setImagebytes method

fc.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "gif", "bmp"));

if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {

fc.setCurrentDirectory(fc.getCurrentDirectory());

ImageIcon icon = new ImageIcon(fc.getSelectedFile().getAbsolutePath());

icon = new ImageIcon(icon.getImage().getScaledInstance(150, 150, Image.SCALE_DEFAULT));

this.lbPicture.setIcon(icon);

//passing the image file to the setImagebytes to create the image bytes

this.setImagebytes(fc.getSelectedFile());

//icon.getImage()

repaint();

}

}

A method to convert the bytes array (byte[]) into a pictures to display in a label

Private void getRealImage(byte[] imagebytes){

if (imagebytes != null) {

ImageIcon icon = new ImageIcon(imagebytes);

icon = new ImageIcon(icon.getImage().getScaledInstance(150, 150, Image.SCALE_DEFAULT));

this.lbPicture.setIcon(icon);

} else {

ImageIcon icon = new ImageIcon();

//showing the image in a label

this.lbPicture.setIcon(icon);

}

}