By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,274 Members | 2,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,274 IT Pros & Developers. It's quick & easy.

How Java Database connectivity with swing ?

P: 2
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=new JPanel();
JPanel DepositPanel=new JPanel();
JPanel WithDrawPanel=new JPanel();
JPanel TransferPanel=new JPanel();
JPanel WithDrAmPanel=new JPanel();
//------------------------------------------------------------------Panels
static MyBank w1;
public void mainPanel(){
JButton btn1=new JButton("Check Balance");
JButton btn2=new JButton("Deposit");
JButton btn3=new JButton("WithDraw");
JButton btn4=new JButton("Money Transfer");
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));

mainPanel.add(btn1);
mainPanel.add(btn2);
mainPanel.add(btn3);
mainPanel.add(btn4);
btn1.setAlignmentX(Component.CENTER_ALIGNMENT);
btn2.setAlignmentX(Component.CENTER_ALIGNMENT);
btn3.setAlignmentX(Component.CENTER_ALIGNMENT);
btn4.setAlignmentX(Component.CENTER_ALIGNMENT);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
setBackground(new Color(50,180,250));
}


public void BalancePanel(){
JLabel lbl1=new JLabel("Account Balance", SwingConstants.CENTER);
BalancePanel.add(lbl1);
JButton btn11=new JButton("Back");
BalancePanel.add(btn11);
btn11.addActionListener(this);

JLabel lbl10=new JLabel("Your Balance is", SwingConstants.CENTER);
BalancePanel.add(lbl10);
JButton btn12=new JButton("Print Account Details");
BalancePanel.add(btn12);
btn12.addActionListener(this);
//lbl1.setBorder(BorderFactory.createEmptyBorder(5,5 ,5,5));
BalancePanel.setBackground(new Color(205,10,110));


}


public void DepositPanel(){
JLabel lbl2=new JLabel("Deposit Panel");
DepositPanel.add(lbl2);
JLabel lbl20=new JLabel("How Much you want to Deposit");
DepositPanel.add(lbl20);
DepositPanel.setLayout(new BoxLayout(DepositPanel,BoxLayout.Y_AXIS));
DepositPanel.add(new JRadioButton("500 Rs", null, true));
DepositPanel.add(new JRadioButton("1000 Rs"));
DepositPanel.add(new JRadioButton("2000 Rs"));
DepositPanel.add(new JRadioButton("5000 Rs"));
DepositPanel.add(new JRadioButton("10000 Rs"));
DepositPanel.add(new JRadioButton("Others"));

JButton btn21=new JButton("Back");
DepositPanel.add(btn21);
btn21.addActionListener(this);
DepositPanel.setBackground(new Color(185,105,50));
}


public void WithDrawPanel(){
JLabel lbl3=new JLabel("WithDraw Panel");
WithDrawPanel.add(lbl3);
JLabel lbl30=new JLabel("How Much you want to WithDraw");
WithDrawPanel.add(lbl30);
WithDrawPanel.setLayout(new BoxLayout(WithDrawPanel,BoxLayout.Y_AXIS));
WithDrawPanel.add(new JRadioButton("500 Rs", null, true));
WithDrawPanel.add(new JRadioButton("1000 Rs"));
WithDrawPanel.add(new JRadioButton("2000 Rs"));
WithDrawPanel.add(new JRadioButton("5000 Rs"));
WithDrawPanel.add(new JRadioButton("8000 Rs"));
WithDrawPanel.add(new JRadioButton("10000 Rs"));
WithDrawPanel.add(new JRadioButton("12000 Rs"));
WithDrawPanel.add(new JRadioButton("15000 Rs"));
WithDrawPanel.add(new JRadioButton("20000 Rs"));

JRadioButton Rbtn32=new JRadioButton("Other Amount");
//WithDrawPanel.add(new JRadioButton("Other Amount"));
WithDrawPanel.add(Rbtn32);
Rbtn32.addActionListener(this);

JButton btn31=new JButton("Back");
WithDrawPanel.add(btn31);
btn31.addActionListener(this);
WithDrawPanel.setBackground(new Color(205,225,10));
}

public void WithDrAmPanel(){
JLabel lbl5=new JLabel("How much, Please Input");
WithDrAmPanel.add(lbl5);
JButton btn51=new JButton("Back to WithDraw");
WithDrAmPanel.add(btn51);
btn51.addActionListener(this);
WithDrAmPanel.setBackground(new Color(15,225,10));
w1.pack();
w1.setVisible(true);
w1.setSize(500,500);
}

public void TransferPanel(){
JLabel lbl4=new JLabel("Transfer Panel");
TransferPanel.add(lbl4);
JButton btn41=new JButton("Back");
TransferPanel.add(btn41);
btn41.addActionListener(this);
TransferPanel.setBackground(new Color(15,225,10));
}

//------------------------------------------------------------------ActionEvents
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="WithDraw"){
getContentPane().add(w1.WithDrawPanel);
getContentPane().remove(w1.mainPanel);
//w1.pack();
w1.setVisible(true);
System.out.println("Do something");
}

if(e.getActionCommand()=="Other Amount"){
getContentPane().add(w1.WithDrAmPanel);
getContentPane().remove(w1.WithDrawPanel);
//w1.pack();
w1.setVisible(true);
System.out.println("Withdraw");
}

if(e.getActionCommand()=="Back to WithDraw"){
getContentPane().add(w1.WithDrawPanel);
getContentPane().remove(w1.mainPanel);
getContentPane().remove(w1.WithDrAmPanel);
WithDrawPanel.setBackground(new Color(205,225,10));
//WithDrawPanel.Rbtn32.Null
w1.pack();
w1.setVisible(true);
w1.setSize(500,500);
System.out.println("Back to Withdraw");
}
//----------------------------------------------------Withdraw end

if(e.getActionCommand()=="Deposit"){
getContentPane().add(w1.DepositPanel);
getContentPane().remove(w1.mainPanel);
//w1.pack();
w1.setVisible(true);
System.out.println("How to Deposit");
}

if(e.getActionCommand()=="Check Balance"){
getContentPane().add(w1.BalancePanel);
getContentPane().remove(w1.mainPanel);
//w1.pack();
//w1.setDefaultLookAndFeelDecorated(true);
w1.setVisible(true);
System.out.println("Tell me balance");
}

if(e.getActionCommand()=="Money Transfer"){
getContentPane().add(w1.TransferPanel);
getContentPane().remove(w1.mainPanel);
//w1.pack();
w1.setVisible(true);
System.out.println("Money Transfer");

}

if(e.getActionCommand()=="Back"){
getContentPane().add(w1.mainPanel);
getContentPane().remove(w1.BalancePanel);
getContentPane().remove(w1.DepositPanel);
getContentPane().remove(w1.WithDrawPanel);
getContentPane().remove(w1.TransferPanel);
System.out.println("I'm Back");
mainPanel.setBackground(new Color(50,180,250));
w1.pack();
w1.setVisible(true);
w1.setSize(500,500);
}
}

//------------------------------------------------------------------Main
public static void main(String arg[]){
w1= new MyBank();
w1.mainPanel();
w1.BalancePanel();
w1.DepositPanel();
w1.WithDrawPanel();
w1.WithDrAmPanel();
w1.TransferPanel();
w1.getContentPane().add(w1.mainPanel);
w1.pack();
w1.setVisible(true);
w1.setSize(500,500);
w1.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
Nov 8 '06 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 1,510
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
Nov 8 '06 #2

P: 2
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
Nov 8 '06 #3

10K+
P: 13,264
http://www.apl.jhu.edu/~hall/java/Sw...al-JLabel.html

For layout use a layout manager

For jdbc, read the tutorial you were given above.
Nov 9 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.