-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentBuilder.java
More file actions
189 lines (170 loc) · 7.98 KB
/
ComponentBuilder.java
File metadata and controls
189 lines (170 loc) · 7.98 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
package Flawless_Feedback;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class ComponentBuilder {
/**
* JLabel for creating a label within application.
* @param text Set contents of label
* @param xPos X Position within application
* @param yPos Y Position within application
* @param layout Set layout used
* @param frame Related to frame usage
* @return Returns label when called upon
*/
public static JLabel CreateALabel(String text, int xPos, int yPos, SpringLayout layout, JFrame frame)
{
JLabel myNewLabel = new JLabel(text);
layout.putConstraint(SpringLayout.WEST,myNewLabel,xPos,SpringLayout.WEST,frame);
layout.putConstraint(SpringLayout.NORTH,myNewLabel,yPos,SpringLayout.NORTH,frame);
frame.add(myNewLabel);
return myNewLabel;
}
public static JLabel CreateALabel(String text, int xPos, int yPos, SpringLayout layout,
JFrame frame, Component component)
{
JLabel myNewLabel = new JLabel(text);
layout.putConstraint(SpringLayout.WEST,myNewLabel,xPos,SpringLayout.WEST,component);
layout.putConstraint(SpringLayout.NORTH,myNewLabel,yPos,SpringLayout.NORTH,component);
frame.add(myNewLabel);
return myNewLabel;
}
/**
* JLabel for creating a button within application
* @param text Set button contents
* @param width Width of button
* @param height Height of button
* @param xPos X Position within application
* @param yPos Y Position within application
* @param listener Where the buttons action will go to
* @param layout Set layout to be used
* @param frame Related to frame usage
* @return Returns JButton from method
*/
public static JButton CreateAButton(String text, int width, int height, int xPos, int yPos,
ActionListener listener, SpringLayout layout, JFrame frame)
{
JButton myNewButton = new JButton(text);
myNewButton.addActionListener(listener);
myNewButton.setPreferredSize(new Dimension(width,height));
layout.putConstraint(SpringLayout.WEST,myNewButton,xPos,SpringLayout.WEST,frame);
layout.putConstraint(SpringLayout.NORTH,myNewButton,yPos,SpringLayout.NORTH,frame);
frame.add(myNewButton);
return myNewButton;
}
public static JButton CreateAButton(String text, int width, int height, int xPos, int yPos,
ActionListener listener, SpringLayout layout, JFrame frame, Component component)
{
JButton myNewButton = new JButton(text);
myNewButton.addActionListener(listener);
myNewButton.setPreferredSize(new Dimension(width,height));
layout.putConstraint(SpringLayout.WEST,myNewButton,xPos,SpringLayout.WEST,component);
layout.putConstraint(SpringLayout.NORTH,myNewButton,yPos,SpringLayout.NORTH,component);
frame.add(myNewButton);
return myNewButton;
}
public static JTextField CreateATextField(int size, int xPos, int yPos, SpringLayout layout, JFrame frame)
{
JTextField myNewTextField = new JTextField(size);
layout.putConstraint(SpringLayout.WEST,myNewTextField,xPos,SpringLayout.WEST,frame);
layout.putConstraint(SpringLayout.NORTH,myNewTextField,yPos,SpringLayout.NORTH,frame);
frame.add(myNewTextField);
return myNewTextField;
}
/**
* Text field creator within JLabel
* @param size Size of text field
* @param xPos X Position within application
* @param yPos Y Position within application
* @param layout Layout used for organising
* @param frame Relate to frame usage
* @param component Where the text field will be placed relative to
* @return Returns a new text field
*/
public static JTextField CreateATextField(int size, int xPos, int yPos, SpringLayout layout,
JFrame frame, Component component)
{
JTextField myNewTextField = new JTextField(size);
layout.putConstraint(SpringLayout.WEST,myNewTextField,xPos,SpringLayout.WEST,component);
layout.putConstraint(SpringLayout.NORTH,myNewTextField,yPos,SpringLayout.NORTH,component);
frame.add(myNewTextField);
return myNewTextField;
}
public static JTextArea CreateATextArea(int sizeX, int sizeY, int xPos, int yPos, SpringLayout layout,
JFrame frame)
{
JTextArea myNewTextArea = new JTextArea(sizeY, sizeX);
layout.putConstraint(SpringLayout.WEST,myNewTextArea,xPos,SpringLayout.WEST,frame);
layout.putConstraint(SpringLayout.NORTH,myNewTextArea,yPos,SpringLayout.NORTH,frame);
frame.add(myNewTextArea);
return myNewTextArea;
}
public static JTextArea CreateATextArea(int sizeX, int sizeY, int xPos, int yPos, SpringLayout layout,
JFrame frame, Component component)
{
JTextArea myNewTextArea = new JTextArea(sizeY, sizeX);
layout.putConstraint(SpringLayout.WEST,myNewTextArea,xPos,SpringLayout.WEST,component);
layout.putConstraint(SpringLayout.NORTH,myNewTextArea,yPos,SpringLayout.NORTH,component);
frame.add(myNewTextArea);
return myNewTextArea;
}
//---------------------------------------------------------------------------------------------------
// Source: http://www.dreamincode.net/forums/topic/231112-from-basic-jtable-to-a-jtable-with-a-tablemodel/
// class that extends the AbstractTableModel
//---------------------------------------------------------------------------------------------------
class MyModel extends AbstractTableModel
{
ArrayList<Object[]> al;
String[] header;
int col;
// constructor
MyModel(ArrayList<Object[]> obj, String[] header)
{
// save the header
this.header = header;
// and the data
al = obj;
// get the column index
col = this.findColumn("");
}
public int getRowCount()
{
return al.size();
}
public int getColumnCount()
{
return header.length;
}
public Object getValueAt(int rowIndex, int columnIndex)
{
return al.get(rowIndex)[columnIndex];
}
public String getColumnName(int index)
{
return header[index];
}
public Class getColumnClass(int columnIndex)
{
if (columnIndex == col)
{
return Boolean.class; // For every cell in column 7, set its class to Boolean.class
}
return super.getColumnClass(columnIndex); // Otherwise, set it to the default class
}
// a method to add a new line to the table
void add(String qNo, String topic, String question)
{
// make it an array[3] as this is the way it is stored in the ArrayList
// (not best design but we want simplicity)
Object[] item = new Object[3];
item[0] = qNo;
item[1] = topic;
item[2] = question;
al.add(item);
// inform the GUI that I have change
fireTableDataChanged();
}
}
}