Friday, May 25, 2012

How to use jTabbedPane in Netbeans

2 comments
In this post we learn how to use jTabbedPane and how to create jTabbedPane container in netbeans. jTabbedPane is a java swing container useful to customize GUI in small frame.
  • Open new project in Netbeans.

  • Find Tabbed Pane in pallet ---> just drag and drop Tabbed pane over the jFrame or You can also use jPanel in background.




  • Enlarge the TabbedPane as you require. Here is the main step for creating jTabbedPane Container.

  • Here you have to Select Drag and Drop jPanel over the TabbedPane. See how it look like.




  • Repeat this step and create tabs.

  • While putting jPanel into Tabbed Pane, Put the jPanel into Tabbed Pane only when you see whole area of tabbed is shown by doted line this is the indication of TabbedPane Container.

  • See how it is look when you put jPanel on second time.



  • Out Put :






Continue reading →
Wednesday, May 16, 2012

How to generate javadocs using NetBeans

1 comments
Javadocs is same as java API which is use for programming. Here is a details on how to generate javadocs using netbeans. Always While completing new projects we need to generate separate documentation for projects. Javadocs is a answer for all. javadocs build on classes which is in your project. Format of javadocs is in HTML files which is same as java API documentation. You can add your own comments, and more about your projects. Javadocs is a very helpful tool by Sun Microsystems helps to avoid all complication to generate separate documentation for large projects.
See how to create javadocs using netbeans.

  • First open your project in Netbeans.
  • Right click on project --> Click on Generate Javadocs.


  • Location of generated javadocs is your projectfolder/dist/javadocs/
  • Your generated javadocs look like as, see few screen shots.
  • Home Page.
  • All classes.
  • Methods in selected class.
Continue reading →
Saturday, May 12, 2012

How to customize jTable using Netbeans

2 comments
Learn how to customize jtable using Netbeans. In this post we learn how to set color grid line,Visibility of vertical and horizontal grid line, and how to set foreground color to selected cell. A simple single line code tell us how to customize jtable.
  • First create global object for DefaultTableModel and create settable() method in constructor.
  • See my previous post How to set color in jTable column header using NetBeans for how to call settable() method.
  • Code for settable() method :
 private void settable() {
        String values[][] = {{"Arun", "www.java4projects.blogspot.in"}, {"Digvijay", "www.tutorialdata.com"}, {"Raj", "www.how2java.com"}};
        String title[] = new String[]{"Blogger Name", "Blogger website"};
        dm = new DefaultTableModel(values, title);
        dm.setColumnIdentifiers(title);
        jTable1.getTableHeader().setBackground(Color.PINK);
        jTable1.getTableHeader().setForeground(Color.BLUE);
        jTable1.setModel(dm);
    }
  • Out put :
  • Setting grid line color add single line code given below in the settable() method. As we know jtable has default color which is black.
    jTable1.setGridColor(Color.RED);
    
  • Out Put :

  • Display jTable without grid layout by adding single line code in settable() method.
    jTable1.setShowGrid(false);
    
  • Out Put :
  • Showing only horizontal and vertical grid lines.Change the code alternatively below you get out puts likes this.
  • For Displaying only horizontal lines.
     jTable1.setShowVerticalLines(false);
    
  • Out put :
  • Display only Vertical grid lines.
    jTable1.setShowHorizontalLines(false);
    
  • Setting foreground color to selected cell in jTable.
    jTable1.setSelectionForeground(Color.blue);
    

    Thanks for reading this post. Hope you find best.

Continue reading →
Friday, May 11, 2012

How to set color in jTable Column Header using NetBeans

2 comments
All NetBeans user see How to set background color and foreground color to jTable Column Header by use of DefaultTableModel.In my preveous post we learn how to display values in jTable by using DefaultTableModel.Netbeans provides Swings Controls which minimize our coding time and helps to build GUI faster than core coding.

Discription of program :
  • Create a New jframe in NetBeans.
  • From Palette drag and drop table over the jFrame.
  • Another way to open palette in NetBeans, windows --> palette.
  • See how to program.
  • DefaultTableModel is first must be declare as global variable.
  • Define separate method for set DefaultTableModel to jTable model or you code directly without creating separate method.Defining such type of methods improve coding standards so always go for this way.
  • After defining method call that method in constructor.
  • Here we define method is settable();
  • In settable() method we use ColumnIdentifiers to set column header name. The method getTableHeader used to get column header and methods setBackground(Color.PINK) and setForeground(Color.blue) used for set background color to column header and set separate color to column header name.
  • Source Code :
  • See how to call settable() methode in constructor.
package jdefaulttabledemo;
import java.awt.Color;
import javax.swing.table.DefaultTableModel;

public class jTableHeader extends javax.swing.JFrame {

    DefaultTableModel dm;
    public jTableHeader() {
        initComponents();
        settable();
        
    }
  • Source Code for settable() method.
 private void settable() {
       String s[][]={{"Arun","www.java4projects.blogspot.in"},{"Digvijay","www.tutorialdata.com"}};
       String s1[]=new String []{"Blogger Name","Blogger website"};
       dm = new DefaultTableModel(s, s1);
       dm.setColumnIdentifiers(s1);
        jTable1.getTableHeader().setBackground(Color.PINK);
        jTable1.getTableHeader().setForeground(Color.BLUE);
        jTable1.setModel(dm);
    }
  • Out Put :
Continue reading →
Tuesday, May 8, 2012

How to add jInternal frame and jPanel into jDesktopPane

0 comments
jInternal frame is a nothing but another window which can helps to improve GUI in your project. jPanel is another side which is widely use in projects for making better GUI. Can we use jInternal frame and jPanel together?.
Answer is Yes. See how to add jInternal frame and jPanel into jDesktopPane.
  • Make GUI using Netbeans as per your choice.Here is a sample GUI.
  • When you run this GUI,This is look like as..
  • Code for adding jPanel into jDesktopPane.
  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                       
     jDesktopPane1.removeAll();  
     jDesktopPane1.repaint();  
     NewJPanel panel = new NewJPanel();  
     Dimension screenSize = jDesktopPane1.getSize();  
     panel.setSize(screenSize.width, screenSize.height);  
     jDesktopPane1.add(panel);  
     int width = panel.getWidth();  
     int height = panel.getHeight();  
     panel.setBounds(((screenSize.width / 2) - (width / 2)), ((screenSize.height / 2) - (height / 2)), width, height);  
     panel.setVisible(true);  
   }   
  • The repaint() and removeAll() method is for refreshing jDestopPane. That means if we go for second form to display it overrides on first form and in a result both are visible so for cleanness purpose that methods are used.
  • setBounds() and setSize() methods are used for set frame size as jDesktopPane size set.That methods dynamically adjust size as jDesktopPane size change.
  • Note that main thing we do here,We create the object for jPanel and it add into jDesktopPane. The same thing do for jInternal frame.
  • Output for jPanel Form.
  • Also same for jInternal Frame.
   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                       
     jDesktopPane1.removeAll();  
     jDesktopPane1.repaint();  
     NewJInternalFrame frame = new NewJInternalFrame();  
     Dimension screenSize = jDesktopPane1.getSize();  
     frame.setSize(screenSize.width, screenSize.height);  
     jDesktopPane1.add(frame);  
     int width = frame.getWidth();  
     int height = frame.getHeight();  
     frame.setBounds(((screenSize.width / 2) - (width / 2)), ((screenSize.height / 2) - (height / 2)), width, height);  
     frame.setVisible(true);  
   }  
  • See the output for jInternal Frame.
  • Implementing both jPanel and jInternal frame together we can build effective GUI.
  • Hope you find best. Thanks for reading.
Continue reading →

How to display values in jTable in netbeans

4 comments

Now we take a look of how to display values in jTable using netbeans with source code.
  • First create a simple jFrame form an build GUI like this.
  • For building GUI don't bother about it, You have to just drag and drop the components from palette.
  • Once you completed GUI then we need to create database for storing values.
  • Use any database here is a sample database with table.
  • If you create database in Ms Access you must set the database connectivity.To set connectivity.
  • Control panel --> Administrative tools --> Data Source(ODBC) --> Add --> select Microsoft Access Driver(*.mdb,*.accdb)-->Finish --> Type database name in Data Source Name Field --> Select --> select location or path of database --> ok.
  • Now actual implementation code.
  • First crate Action class for getting dynamic database connection overall the project.
  package jdefaulttabledemo;  
 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.SQLException;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 public class Action {  
   public static Connection getDBConnection() {  
     Connection con = null;  
     try {  
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
       con = DriverManager.getConnection("jdbc:odbc:jTabelDemo");  
     } catch (SQLException ex) {  
       Logger.getLogger(Action.class.getName()).log(Level.SEVERE, null, ex);  
     } catch (ClassNotFoundException ex) {  
       Logger.getLogger(Action.class.getName()).log(Level.SEVERE, null, ex);  
     }  
     return con;  
   }  
 }  
  • Define two methods named setTable() and getdata().
  • Purpose of setTable() method is we use DefaultTableModel to give a dynamic look for jTable. Are you confused? Let me explain. Normally when we take jtable we go through jTable property to set row and column. Here by help of DefaultTableModel we not worry about setting rows and columns. Look at method then I explained in detail.
  private void setTable() {  
     dm = new DefaultTableModel(0, 0);  
     String s[] = new String[]{"Blogger Name", "Website Name"};  
     dm.setColumnIdentifiers(s);  
     jTable1.setModel(dm);  
   }  
  • String array is used to store all column name and columnIdentifier is used to set that all column name to jTable at run time.
  • Now take a look at getdata() method. It define for retrieving database values and set to jTabel.
  private void getdata() {  
     Connection con;  
       try {  
       con = Action.getDBConnection();  
       PreparedStatement pst = con.prepareStatement("select * from jtabel");  
       ResultSet rs = pst.executeQuery();  
       while (rs.next()) {  
         String bloggername = rs.getString(1);  
         String websitename = rs.getString(2);  
         Vector<String> vector = new Vector<String>();  
         //     vector.add(name);  
         vector.add(bloggername);  
         vector.add(websitename);  
         dm.addRow(vector);  
       }  
     } catch (SQLException ex) {  
       Logger.getLogger(JdefaluttableDemo.class.getName()).log(Level.SEVERE, null, ex);  
     }  
   }  
  • Event on save button.
  • To insert values in database, First open database connection and prepare sql query for inserting values in database.
  • To display values in jTable and for retrieving values, call setTable() and getdata() methods end of the query.
  • But First must call that methods in constructor to set DefaultTableModel to jTabel.
  • Code for save button.
 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {  
     Connection con = Action.getDBConnection();  
     try {  
       PreparedStatement pst = con.prepareStatement("insert into jtabel values(?,?)");  
       pst.setString(1,jTextField1.getText());  
       pst.setString(2,jTextField2.getText());  
       pst.executeUpdate();  
       JOptionPane.showMessageDialog(this,"save successfully.");  
     } catch (SQLException ex) {  
       Logger.getLogger(JdefaluttableDemo.class.getName()).log(Level.SEVERE, null, ex);  
     }  
     setTable();  
     getdata();  
   }  
  • Finally see how to call methods in constructor and import statements.
 import java.sql.Connection;  
 import java.sql.PreparedStatement;  
 import java.sql.ResultSet;  
 import java.sql.SQLException;  
 import java.util.Vector;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 import javax.swing.JOptionPane;  
 import javax.swing.table.DefaultTableModel;  
 public class JdefaluttableDemo extends javax.swing.JFrame {  
   DefaultTableModel dm;  
   public JdefaluttableDemo() {  
     initComponents();  
     setTable();  
     getdata();  
   }  
  • Output of this code.
Continue reading →
Sunday, May 6, 2012

How to set background image to jFrame form in netbeans

6 comments

Some times we get problem while customizing code.Problem is we don't know how?. Here is simple example of how to set background image to jFrame form in netbeans.

  • First open new project and name it.
  • Right click on source package and add new jFrame form
  • Set the title for jFrame form, Right click on jFrame and type title in front of title option.
  • For setting background image to jFrame form first we should set Null Layout because default layout is Free Design Layout.
  • To set Null Layout, Right Click on jFrame --> set layout --> select Null Layout.
  • After setting null layout drag and drop jlabel from pallet.
  • Drag jLabel over the full jFrame.
  • To set background image, Right click on jLabel --> Icon --> select image
  • Your jFarme is look like as.
  • Again if you go for another jLabel you find that jLabel is not visible it just behind the jFrame.
  • For that just select second jLabel, Drag and drop above the first jLabel.
  • See the output for this jFrame. By this way you can customize your jFrame and add another components.
Continue reading →