
Hello!
I am building a Java application with GUI (JFrame form) that is supposed to display all or selected elements of below 2D array (images attached):
Year Season 2002 2003 2004 2005 Spring 150 163 147 138 Summer 100 89 88 87 Autumn 157 97 96 94 Winter 184 133 129 117
I figured out so far how to display all years and seasons, also the smallest or largest element or smallest or largest sum for each row or column. What I am struggling with is how to write a code to display selected elements only (there are 2 comboboxes: 1 for Year (All years, 2002, 2003, 2004, 2005) and 1 for Season (All seasons, Spring, Summer, Autumn, Winter). The user can select any combination (including all elements in a row or column) and this selection should be displayed in the textArea field (including Year and Season headings).
Here's some code I have so far:
to display all data
private void allYearsActionPerformed(java.awt.event.ActionEvent evt) {
displayRainfall.setText("");
int[][] rainfall = {{150, 163, 147, 138}, {100, 89, 88, 87}, {157, 97, 96, 94}, {184, 133, 129, 117}};
String[] seasons = {"Spring", "Summer", "Autumn", "Winter"};
String[] years = {"2002", "2003", "2004", "2005"};
int i = 0;
int j = 0;
String rain = "\n\t\t\t\n";
rain = "\t\t2002\t2003\t2004\t2005\n\n";
for (i = 0; i < 4; i++) {
rain += "\t" + seasons[i];
for (j = 0; j < 4; j++) {
rain += "\t" + rainfall[i][j];
}
rain += "\n";
}
displayRainfall.setText(rain);
}
to display wettest year
private void getWtYrActionPerformed(java.awt.event.ActionEvent evt) {
displayWtYr.setText("");
int[][] rainfall = {{150, 163, 147, 138}, {100, 89, 88, 87}, {157, 97, 96, 94}, {184, 133, 129, 117}};
String[] seasons = {"Spring", "Summer", "Autumn", "Winter"};
String[] years = {"2002", "2003", "2004", "2005"};
int sum[] = new int[rainfall[0].length];
for (int j = 0; j < rainfall[0].length; j++) {
int total = 0;
for (int i = 0; i < rainfall.length; i++) {
total += rainfall[i][j];
sum[j] = total;
}
}
int yearWet = 000000;
int j = 0;
int wet= 0;
for (j = 0; j < rainfall[0].length; j++) {
if (sum[j] > yearWet) {
yearWet = sum[j];
wet = j;
}
}
displayWtYr.setText("The wettest year was " + years[wet]+" "+ yearWet +"-"+ " mm of rainfall");
}
Can you please give me some hints how start with the code for displaying elements of the array selected by user?
Many thanks in advance for your help!
Rafal
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.