-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountLayout.java
More file actions
203 lines (175 loc) · 6.3 KB
/
AccountLayout.java
File metadata and controls
203 lines (175 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package lab_7_pack;
// This code gives the layout for the UI and
// demonstrates two ways of updating the data
// in a JTable.
// Another option to consider when using JTable is
// creating your own data model by overriding
// AbstractTableModel. You might use this option
// if data for table was coming from say a DB.
// One example: http://www.java2s.com/Code/Java/Swing-JFC/CreatingsimpleJTableusingAbstractTableModel.htm
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class AccountLayout extends JFrame implements ActionListener {
private JTable table;
private JButton clearButton;
private JButton transferButton;
private JTextField toField;
private JTextField amountField;
private JTextField fromField;
//data to stream in
private DBTable data;
//list of accounts in table
private ArrayList<Account> accounts=new ArrayList<Account>();
private String[] columnNames = {"Account ID",
"Account Name",
"Balance"};
public AccountLayout() {
setTable(); //get connected to database with the account table
Object[][] account_data = {
{accounts.get(0).getID(),accounts.get(0).getType(),
accounts.get(0).getBalance()},
{accounts.get(1).getID(),accounts.get(1).getType(),
accounts.get(1).getBalance()},
{accounts.get(2).getID(),accounts.get(2).getType(),
accounts.get(2).getBalance()}
};
Container contentPane = getContentPane();
contentPane.setLayout(new GridBagLayout());
DefaultTableModel dtm = new DefaultTableModel(account_data,columnNames);
table = new JTable(dtm);
// The default size of a JTable is something like
// 450 X 400.
Dimension smallerSize = new Dimension(450, 50);
table.setPreferredScrollableViewportSize(smallerSize );
JScrollPane scrollPaneForTable = new JScrollPane(table);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.weightx = 1;
constraints.weighty = 1;
constraints.insets = new Insets(4, 4, 4, 4);
constraints.fill = GridBagConstraints.BOTH;
contentPane.add(scrollPaneForTable,constraints);
constraints.gridx = 0;
// constraints.gridy = 1;
constraints.weighty = 0;
constraints.gridy = GridBagConstraints.RELATIVE;
constraints.insets = new Insets(2, 4, 2, 4);
constraints.fill = GridBagConstraints.NONE;
constraints.gridwidth = 1;
constraints.anchor = GridBagConstraints.NORTHEAST;
JLabel toLabel = new JLabel("From:");
contentPane.add(toLabel,constraints);
constraints.gridx = 1;
constraints.anchor = GridBagConstraints.NORTHWEST;
fromField = new JTextField("3",8);
// Workaround, because of: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4247013
fromField.setMinimumSize(fromField.getPreferredSize());
contentPane.add(fromField,constraints);
constraints.gridx = 0;
// constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHEAST;
JLabel fromLabel = new JLabel("To:");
contentPane.add(fromLabel,constraints);
constraints.gridx = 1;
// constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHWEST;
toField = new JTextField("4",8);
toField.setMinimumSize(toField.getPreferredSize());
contentPane.add(toField,constraints);
constraints.gridx = 0;
// constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHEAST;
JLabel amountLabel = new JLabel("Amount:");
contentPane.add(amountLabel,constraints);
constraints.gridx = 1;
// constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHWEST;
amountField = new JTextField("100",8);
amountField.setMinimumSize(amountField.getPreferredSize());
contentPane.add(amountField,constraints);
constraints.gridx = 0;
// constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHEAST;
clearButton = new JButton("Clear");
contentPane.add(clearButton,constraints);
clearButton.addActionListener(this);/* {
@Override
public void actionPerformed(ActionEvent e) {
DefaultTableModel dtm = (DefaultTableModel) (table.getModel());
int row = 0;
int column = 2;
// Example of how to read/write values from/to a JTable
System.out.println("Old value: " + dtm.getValueAt(row, column));
dtm.setValueAt(new Integer(999), row, column);
}
});*/
constraints.gridx = 1;
// constraints.gridy = 2;
constraints.anchor = GridBagConstraints.NORTHWEST;
transferButton = new JButton("Transfer");
transferButton.addActionListener(this);
/*@Override
public void actionPerformed(ActionEvent e) {
Object[][] newData = {
//populate data here from database
{new Integer(3), "Savings", new Integer(400)},
{new Integer(4), "Checking", new Integer(370)}};
// Example of how to change the table model of an
// existing JTable
table.setModel(new DefaultTableModel(newData,columnNames));
}
});*/
contentPane.add(transferButton,constraints);
}
public void actionPerformed(ActionEvent evt){
if(evt.getSource()==clearButton){
//set text fields blank
fromField.setText("");
toField.setText("");
amountField.setText("");
}
if(evt.getSource()==transferButton){
int from=Integer.parseInt(fromField.getText());
int to=Integer.parseInt(toField.getText());
int amount=Integer.parseInt(amountField.getText());
if(amount>0){
DBTable.transfer(to,from,amount);
}
else{
JOptionPane.showMessageDialog(null,"Amount cannot be transferred");
}
}
}
public void setTable(){
try{
data=new DBTable();
data.createTable();
accounts=data.retrieveTable();
System.out.println("Hi");
}
catch(SQLException e){
System.out.println("Could not connect to the database");
System.err.println(e);
}
}
public void getAccounts(){
//for()
}
public void makeTable(){
//for(int i=0;i<)
}
public static void main(String[] args) {
JFrame frame = new AccountLayout();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}