CENG325 - 2 - GUI
CENG325 - 2 - GUI
GUI Design
N.B You can use the old button action performed to copy the code.
How to use a Combo Box
Where to write the code?
1. Case1: In our application the user must press the button to show
the result>>> you have to set the button action perform handler>>
double click your button and write the necessary code.
2. Case2: If you want the result to be shown when the choice is
selected from the comboBox >> you have to set the comboBox
action performed handler>>double click your comboBox and
write the code. In this case, you can delete the Convert button
Clear Button
Now add a new button>>Clear button
This button is used to clear the textfields.
When the user presses Clear button, an empty string is written
in the textfields>> TextFieldVariableName.setText(“”);
But, in our time calculator a value “0” is needed to avoid exception.
So, make the Clear button writes “0” in the textfields>>
TextFieldVariableName.setText(“0”);
Class constructor
A constructor in Java is a special method that is used
to initialize objects.
It has always the same name as the class name without
a return value.
The constructor is called when an object of a class is
created.
It can be used to set initial values for object attribute.
Example
// Create a MyClass class
}
public static void main(String[] args){
MyClass myObj = new MyClass(); // Create an object of class MyClass
}
}
// Outputs 5
Default Form Constructor
public Myform() {
initComponents();
}
Row 0
Row 1
Col 0 Col 1
Show output in a Table
You can set the column header within your code using
the following:
table.getColumnModel().getColumn(0).setHeaderValue
(“Hours");
TableVariableName.setValueAt( ).
Note that the first row and column index is 0.
Show output in a Table
Modify the Convert Button action performed:
1. Define a string variable>>String resultVal
2. Change the if else statements:
1. For hours: resultVal=d*24+h+m/60+ “hours”;
2. For minutes: resultVal=d*24*60+h*60+m+ “mins”;
3. For seconds: resultVal=d*24*60*60+h*60*60+m*60+ “sec”;
3. You can now delete the result label from your design.
4. Set the table values.
Not
Show output in a Table here
This will create an instant from the class object with the
name anyname and will make it visible on button click.
Write results in an array list
public class timeform extends javax.swing.JFrame {
public timeform() {
initComponents();
table.getColumnModel().getColumn(0).setHeaderValue("Days");
row=0;
if(combo.getSelectedItem()=="in hours") {
result= D*24+H+M/60;
result= D*24*60+H*60+M;
results.add(Double.toString(result)+" minutes"); }
result= D*24*3600+H*3600+M*60;
results.add(Double.toString(result)+" seconds"); }
Open a new form from a button
Now you can show the results in the new form using the
array list from your initial form (timeform)
This should be done as soon as opening the new
showresults form in the constructor of the new form.
public showresults() {
initComponents();
table.setValueAt(timeform.results.get(i), i, 0); }
Assignment1:
Temperature Converter
Assignment1: Temperature Converter
The main idea of this assignment is to create a temperature converter
from Celsius to Fahrenheit or to Kelvin.
The entered values will be saved into a file.
They will be then read from the file, and shown in a dynamically
created table inside a dynamically created form
We need to create the following form:
The action for Convert button
To convert from Celsius to Fahrenheit, we need to apply the
following conversion equation:
Temperature in Fahrenheit = (Temperature in Celsius)*1.8 + 32
To convert from Celsius to Kelvin, we need a different equation:
Temperature in Kelvin = (Temperature in Celsius) + 273.15
FileWriter out;
try {
out = new FileWriter("Temp.txt", true);
out.write(output.getText()+"\n");
out.close();
} catch(IOException e) {
System.out.println("Error appending to file " + "Temp.txt");
}
The action of Show results button
ArrayList<String> myArr = new ArrayList<>();
int rows;
try {
FileInputStream fStream = new FileInputStream("Temp.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(fStream));
while (in.ready()) {
myArr.add(in.readLine());
}
in.close();
}
catch (IOException e) {
System.out.println("File input error");
}
The action of Show results button (part2)
rows = myArr.size();
table.getColumnModel().getColumn(0).setHeaderValue("Temperature");