Wednesday, June 26, 2013

Adding and removing elements from list during runtime


By default List don’t support methods to add/remove elements from list

Why ???
At the time of creation of List jList creates a ListModel object which stores
data of list and it does not support runtime addition or deletion of objects
For adding elements at runtime you need to change ListModel to DefaultListModel
How to do that ?
Here are the steps to do it.
·         Add a List to your Form
·         Right click on List and Select Properties
·         Now look for Model Property
·         Click ellipsis( button that look like [..]) which is in front of model property
·         Now a dialog box will open
·         Now select custom Code from Combobutton
·         And type code new DefaultListModel()  in the TextField. And click OK
·         Now Open Source Code

To desired event or method write the code

   //Getting ListModel in DefaultListModel
   DefaultListModel  dList = (DefaultListModel) list.getmodel();
//You can add Elements in list in two ways
// 1st : adding item at the end of list
dList.addelement(“new item”);
//2nd : Adding eelement at a specific index
dList.add(0,”new Item”);
//here 0 is the index
//to remove any element
dList.removeElementAt(index no);
//OR
dList.removeElement(“Element x”);

Monday, June 24, 2013

Adding an Image as background in Netbeans 2nd Method

This Method is for beginners who are using Netbeans and don't have knowledge
about methods,method overriding etc

This is how you can do it

  • Open your application
  • Add a new frame to it
  • Right Click on Main frame
  • Select Set Layout
  • Now Select Null Layout



Now your Components (If you added) will Collapse and your Frame will turn into a mini frame


  • Re size Frame Add Components (Button,TextField etc)
  • Add a new Label
  • Right click on the label you've just added
  • Select Properties
 
  • Look for icon Menu and Select it
  • Select the Image
 
Now Resize the Frame and re size over the whole Frame


Final Look of Frame


 


Note 

  • You will have to set Minimum size of frame from properties
Otherwise Frame will turn into a Small frame that need to be resided every time

Sunday, June 23, 2013

Adding an Image as background in Netbeans


Adding an Image as background is little different as no function is available for that.
So what you need is Custom Code.You can add an Image as background
using net beans while using features of Net beans
So here is my Code
Note
1)    Image in background will be visible only at run time not design time
2)    I have Skipped some Part of Code So that you could Understand the Code easily
Add A New Frame to you Application make Changes according to given Code

File :Main Frame.class


import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;

class backImage extends JComponent {

Image i;

public backImage(Image i) {
this.i = i;

}

@Override
public void paintComponent(Graphics g) {
g.drawImage(i, 0, 0, null);
}
}

public class MainFrame extends javax.swing.JFrame {
  
    public MainFrame() throws IOException {
          BufferedImage bf = ImageIO.read(new File("E:/bohemia.jpg"));
this.setContentPane(new backImage(bf));
        initComponents();
    }

    public static void main(String args[]) throws IOException {
    
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new MainFrame().setVisible(true);
                } catch (IOException ex) {
                    System.out.print(e.getMessage());
                }
               
            }
        });
    }
                  
}

Result: