How Java Database connectivity with swing ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nuwang
    New Member
    • Oct 2006
    • 2

    #1

    How Java Database connectivity with swing ?

    Hai,

    I'm using a swing to connect database with swing application...

    1. How this swing connect with access database...

    2. Important thing is how change Button size and Font size
    Still I couldn't manage my swing in proper layout pls run and check

    Code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event. *;
    public class MyBank extends JFrame implements ActionListener{
    JPanel mainPanel=new JPanel();
    JPanel BalancePanel=ne w JPanel();
    JPanel DepositPanel=ne w JPanel();
    JPanel WithDrawPanel=n ew JPanel();
    JPanel TransferPanel=n ew JPanel();
    JPanel WithDrAmPanel=n ew JPanel();
    //------------------------------------------------------------------Panels
    static MyBank w1;
    public void mainPanel(){
    JButton btn1=new JButton("Check Balance");
    JButton btn2=new JButton("Deposi t");
    JButton btn3=new JButton("WithDr aw");
    JButton btn4=new JButton("Money Transfer");
    mainPanel.setLa yout(new BoxLayout(mainP anel,BoxLayout. Y_AXIS));

    mainPanel.add(b tn1);
    mainPanel.add(b tn2);
    mainPanel.add(b tn3);
    mainPanel.add(b tn4);
    btn1.setAlignme ntX(Component.C ENTER_ALIGNMENT );
    btn2.setAlignme ntX(Component.C ENTER_ALIGNMENT );
    btn3.setAlignme ntX(Component.C ENTER_ALIGNMENT );
    btn4.setAlignme ntX(Component.C ENTER_ALIGNMENT );
    btn1.addActionL istener(this);
    btn2.addActionL istener(this);
    btn3.addActionL istener(this);
    btn4.addActionL istener(this);
    setBackground(n ew Color(50,180,25 0));
    }


    public void BalancePanel(){
    JLabel lbl1=new JLabel("Account Balance", SwingConstants. CENTER);
    BalancePanel.ad d(lbl1);
    JButton btn11=new JButton("Back") ;
    BalancePanel.ad d(btn11);
    btn11.addAction Listener(this);

    JLabel lbl10=new JLabel("Your Balance is", SwingConstants. CENTER);
    BalancePanel.ad d(lbl10);
    JButton btn12=new JButton("Print Account Details");
    BalancePanel.ad d(btn12);
    btn12.addAction Listener(this);
    //lbl1.setBorder( BorderFactory.c reateEmptyBorde r(5,5,5,5));
    BalancePanel.se tBackground(new Color(205,10,11 0));


    }


    public void DepositPanel(){
    JLabel lbl2=new JLabel("Deposit Panel");
    DepositPanel.ad d(lbl2);
    JLabel lbl20=new JLabel("How Much you want to Deposit");
    DepositPanel.ad d(lbl20);
    DepositPanel.se tLayout(new BoxLayout(Depos itPanel,BoxLayo ut.Y_AXIS));
    DepositPanel.ad d(new JRadioButton("5 00 Rs", null, true));
    DepositPanel.ad d(new JRadioButton("1 000 Rs"));
    DepositPanel.ad d(new JRadioButton("2 000 Rs"));
    DepositPanel.ad d(new JRadioButton("5 000 Rs"));
    DepositPanel.ad d(new JRadioButton("1 0000 Rs"));
    DepositPanel.ad d(new JRadioButton("O thers"));

    JButton btn21=new JButton("Back") ;
    DepositPanel.ad d(btn21);
    btn21.addAction Listener(this);
    DepositPanel.se tBackground(new Color(185,105,5 0));
    }


    public void WithDrawPanel() {
    JLabel lbl3=new JLabel("WithDra w Panel");
    WithDrawPanel.a dd(lbl3);
    JLabel lbl30=new JLabel("How Much you want to WithDraw");
    WithDrawPanel.a dd(lbl30);
    WithDrawPanel.s etLayout(new BoxLayout(WithD rawPanel,BoxLay out.Y_AXIS));
    WithDrawPanel.a dd(new JRadioButton("5 00 Rs", null, true));
    WithDrawPanel.a dd(new JRadioButton("1 000 Rs"));
    WithDrawPanel.a dd(new JRadioButton("2 000 Rs"));
    WithDrawPanel.a dd(new JRadioButton("5 000 Rs"));
    WithDrawPanel.a dd(new JRadioButton("8 000 Rs"));
    WithDrawPanel.a dd(new JRadioButton("1 0000 Rs"));
    WithDrawPanel.a dd(new JRadioButton("1 2000 Rs"));
    WithDrawPanel.a dd(new JRadioButton("1 5000 Rs"));
    WithDrawPanel.a dd(new JRadioButton("2 0000 Rs"));

    JRadioButton Rbtn32=new JRadioButton("O ther Amount");
    //WithDrawPanel.a dd(new JRadioButton("O ther Amount"));
    WithDrawPanel.a dd(Rbtn32);
    Rbtn32.addActio nListener(this) ;

    JButton btn31=new JButton("Back") ;
    WithDrawPanel.a dd(btn31);
    btn31.addAction Listener(this);
    WithDrawPanel.s etBackground(ne w Color(205,225,1 0));
    }

    public void WithDrAmPanel() {
    JLabel lbl5=new JLabel("How much, Please Input");
    WithDrAmPanel.a dd(lbl5);
    JButton btn51=new JButton("Back to WithDraw");
    WithDrAmPanel.a dd(btn51);
    btn51.addAction Listener(this);
    WithDrAmPanel.s etBackground(ne w Color(15,225,10 ));
    w1.pack();
    w1.setVisible(t rue);
    w1.setSize(500, 500);
    }

    public void TransferPanel() {
    JLabel lbl4=new JLabel("Transfe r Panel");
    TransferPanel.a dd(lbl4);
    JButton btn41=new JButton("Back") ;
    TransferPanel.a dd(btn41);
    btn41.addAction Listener(this);
    TransferPanel.s etBackground(ne w Color(15,225,10 ));
    }

    //------------------------------------------------------------------ActionEvents
    public void actionPerformed (ActionEvent e){
    if(e.getActionC ommand()=="With Draw"){
    getContentPane( ).add(w1.WithDr awPanel);
    getContentPane( ).remove(w1.mai nPanel);
    //w1.pack();
    w1.setVisible(t rue);
    System.out.prin tln("Do something");
    }

    if(e.getActionC ommand()=="Othe r Amount"){
    getContentPane( ).add(w1.WithDr AmPanel);
    getContentPane( ).remove(w1.Wit hDrawPanel);
    //w1.pack();
    w1.setVisible(t rue);
    System.out.prin tln("Withdraw") ;
    }

    if(e.getActionC ommand()=="Back to WithDraw"){
    getContentPane( ).add(w1.WithDr awPanel);
    getContentPane( ).remove(w1.mai nPanel);
    getContentPane( ).remove(w1.Wit hDrAmPanel);
    WithDrawPanel.s etBackground(ne w Color(205,225,1 0));
    //WithDrawPanel.R btn32.Null
    w1.pack();
    w1.setVisible(t rue);
    w1.setSize(500, 500);
    System.out.prin tln("Back to Withdraw");
    }
    //----------------------------------------------------Withdraw end

    if(e.getActionC ommand()=="Depo sit"){
    getContentPane( ).add(w1.Deposi tPanel);
    getContentPane( ).remove(w1.mai nPanel);
    //w1.pack();
    w1.setVisible(t rue);
    System.out.prin tln("How to Deposit");
    }

    if(e.getActionC ommand()=="Chec k Balance"){
    getContentPane( ).add(w1.Balanc ePanel);
    getContentPane( ).remove(w1.mai nPanel);
    //w1.pack();
    //w1.setDefaultLo okAndFeelDecora ted(true);
    w1.setVisible(t rue);
    System.out.prin tln("Tell me balance");
    }

    if(e.getActionC ommand()=="Mone y Transfer"){
    getContentPane( ).add(w1.Transf erPanel);
    getContentPane( ).remove(w1.mai nPanel);
    //w1.pack();
    w1.setVisible(t rue);
    System.out.prin tln("Money Transfer");

    }

    if(e.getActionC ommand()=="Back "){
    getContentPane( ).add(w1.mainPa nel);
    getContentPane( ).remove(w1.Bal ancePanel);
    getContentPane( ).remove(w1.Dep ositPanel);
    getContentPane( ).remove(w1.Wit hDrawPanel);
    getContentPane( ).remove(w1.Tra nsferPanel);
    System.out.prin tln("I'm Back");
    mainPanel.setBa ckground(new Color(50,180,25 0));
    w1.pack();
    w1.setVisible(t rue);
    w1.setSize(500, 500);
    }
    }

    //------------------------------------------------------------------Main
    public static void main(String arg[]){
    w1= new MyBank();
    w1.mainPanel();
    w1.BalancePanel ();
    w1.DepositPanel ();
    w1.WithDrawPane l();
    w1.WithDrAmPane l();
    w1.TransferPane l();
    w1.getContentPa ne().add(w1.mai nPanel);
    w1.pack();
    w1.setVisible(t rue);
    w1.setSize(500, 500);
    w1.setDefaultCl oseOperation (JFrame.EXIT_ON _CLOSE);
    }
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by Nuwang
    Hai,

    I'm using a swing to connect database with swing application...

    1. How this swing connect with access database...

    2. Important thing is how change Button size and Font size
    Still I couldn't manage my swing in proper layout pls run and check

    Code:
    What database are you planning to use?

    JDBC Java DataBase Connectivity provides a means to connect java programs to a relational database. It provides classes and methods that create connections with a database and allow the issue of SQL commands. For a tutorial see
    http://java.sun.com/docs/books/tutorial/jdbc/index.html

    it is difficult to advise on layout without knowing how you want the GUI to look. The following tutorial deals with layout problems
    http://java.sun.com/docs/books/tutorial/uiswing/layout/problems.html

    Comment

    • Nuwang
      New Member
      • Oct 2006
      • 2

      #3
      Originally posted by horace1
      What database are you planning to use?

      JDBC Java DataBase Connectivity provides a means to connect java programs to a relational database. It provides classes and methods that create connections with a database and allow the issue of SQL commands. For a tutorial see
      http://java.sun.com/docs/books/tutorial/jdbc/index.html

      it is difficult to advise on layout without knowing how you want the GUI to look. The following tutorial deals with layout problems
      http://java.sun.com/docs/books/tutorial/uiswing/layout/problems.html

      Reply:-
      ok...
      Actually I'm beginning of swing
      Take mainpanel
      Tell me how to change font size of lable and button.
      ( pls, give one example in same file)

      Also want to button size increase (because when run above class file all the buttons move left side and very small buttons, I want to fill above mainpanel(Frame ) with given 4 buttons with boxLayout)

      My first question is little messedup (about access database)
      Eg:- Take bank.mdb file with 2 colomns (No, name)
      I want to know how to get and delete records of above bank.mdb using
      WithDrawPanel.( add a button and labels on WithDrawPanel in GridLayout)

      tnks

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4


        For layout use a layout manager

        For jdbc, read the tutorial you were given above.

        Comment

        Working...