hi all i am making a weather data program and i having a problem for bringinng my text file to the table. here i am putting all my code . and i also have to import a second output window for my code. for example when ever i enter ome value int he window to compute it should show that value in the other window after compute. i have that code ready but i dont know how to implement in this one.
package windchill;
import java.awt.Color;
import java.awt.Desktop.Action;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.*;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;
import javax.swing.JFileChooser;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GUI extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel label1, label2, label3, label4;
JTextField t1, t2, t3;
JButton fileButton, computeButton, cancelButton;
public GUI() {
setLayout(null);
setSize(500, 500);
setTitle(" Weather Project");
label1 = new JLabel("Wind Chill Factor");
label1.setBounds(50, 30, 150, 30);
label2 = new JLabel("Enter The Temperature In Degrees (Fahreheit)");
label2.setBounds(30, 100, 270, 30);
label3 = new JLabel("Enter The Wind Speed In (mph)");
label3.setBounds(30, 200, 200, 30);
label4 = new JLabel("Enter The Dew Point In Degrees (Fahreheit)");
label4.setBounds(30, 300, 250, 30);
t1 = new JTextField();
t1.setBounds(300, 100, 120, 30);
t2 = new JTextField();
t2.setBounds(300, 200, 120, 30);
t3 = new JTextField();
t3.setBounds(300, 300, 120, 30);
fileButton = new JButton("File Entry");
fileButton.setBackground(Color.ORANGE);
fileButton.setBounds(30, 380, 120, 30);
computeButton = new JButton("Compute");
computeButton.setBackground(Color.ORANGE);
computeButton.setBounds(300, 380, 120, 30);
cancelButton = new JButton("Cancel");
cancelButton.setBackground(Color.ORANGE);
cancelButton.setBounds(300, 380, 120, 30);
add(label1);
add(label2);
add(label3);
add(label4);
add(t1);
add(t2);
add(t3);
add(fileButton);
add(computeButton);
add(cancelButton);
t1.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!(Character.isDigit(c))) {
ke.consume();
}
}
});
t2.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!(Character.isDigit(c))) {
ke.consume();
}
}
});
t3.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
char c = ke.getKeyChar();
if (!(Character.isDigit(c))) {
ke.consume();
}
}
});
fileButton.addActionListener(this);
computeButton.addActionListener(this);
cancelButton.addActionListener(this);
}
// Action listener on Choose File
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == fileButton) {
FileChooser fc = new FileChooser();
String data[][] = fc.get();
ListShow listShow = new ListShow();
listShow.showFunc(data);
}
//Action listener on Compute Button
if (ae.getSource() == computeButton) {
try {
if ((t1.getText().equals("")) || (t2.getText().equals(""))
|| (t3.getText().equals(""))) {
JOptionPane.showMessageDialog(null,
"Fields Cannot Be Empty");
}
double temp = Double.parseDouble(t1.getText());
double speed = Double.parseDouble(t2.getText());
double dew = Double.parseDouble(t3.getText());
if (temp <= 50 && speed >= 3) {
WindChill wc = new WindChill(temp, speed, dew);
wc.setVisible(true);
} else {
JOptionPane.showMessageDialog(null,
"An Invalid Value Has Been Entered");
}
} catch (Exception e) {
System.out.println("Fields Cannot Be Empty");
}
}
}
public static void main(String[] args) {
GUI g1 = new GUI();
g1.setVisible(true);
g1.setIconImage(Toolkit.getDefaultToolkit().getImage("Users\\mihirpatel8138\\Desktop\\weather.png"));
}
public class WindChill extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel l1, l2, l3;
JButton windChillButton, cloudBaseButton, showDataInTable;
JTextField t1, t2, t3, t4, t5;
int temp, speed, dew;
private String p1;
public WindChill(double temp2, double speed2, double dew2) {
setLayout(null);
setSize(550, 550);
l1 = new JLabel("Temperature in Farenhiet is: ");
l1.setBounds(30, 100, 270, 30);
t1 = new JTextField("" + temp2);
t1.setBounds(300, 100, 120, 30);
l2 = new JLabel("Speed in mph is: ");
l2.setBounds(30, 200, 200, 30);
t2 = new JTextField("" + speed2);
t2.setBounds(300, 200, 120, 30);
l3 = new JLabel("Dew is: ");
l3.setBounds(30, 300, 250, 30);
t3 = new JTextField("" + dew2);
t3.setBounds(300, 300, 120, 30);
windChillButton = new JButton("Calculate Wind Chill Factor");
windChillButton.setBackground(Color.ORANGE);
windChillButton.setBounds(30,350,150,30);
cloudBaseButton = new JButton("Calculate Cloud Base Altitude");
cloudBaseButton.setBackground(Color.ORANGE);
cloudBaseButton.setBounds(30, 400,150,30);
showDataInTable = new JButton("Save Values to Table");
showDataInTable.setBounds(100, 450, 300, 30);
t4 = new JTextField(20);
t4.setBounds(300, 350, 120, 30);
t5 = new JTextField(20);
t5.setBounds(300, 400, 120, 30);
t1.setEditable(false);
t2.setEditable(false);
t3.setEditable(false);
t4.setEditable(false);
t5.setEditable(false);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(windChillButton);
add(cloudBaseButton);
add(t4);
add(t5);
add(showDataInTable);
windChillButton.addActionListener(this);
cloudBaseButton.addActionListener(this);
showDataInTable.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
double temp = Double.parseDouble(t1.getText());
double speed = Double.parseDouble(t2.getText());
double dew = Double.parseDouble(t3.getText());
if (e.getSource() == windChillButton) {
double windChill = 35.74 + (.6215 * temp) - (35.75 * (Math.pow(speed, 0.16))) + .4275 * temp * (Math.pow(speed, 0.16)); // Minified formula to calculate windChill
String s = String.format("%.2f", windChill);
System.out.println(s);
t4.setText(s);
}
if (e.getSource() == cloudBaseButton) {
double cloudbase = (temp - dew) / 4.4 * 1000;
String s1 = String.format("%.2f", cloudbase);
System.out.printf("%.2f",cloudbase);
t5.setText(s1);
}
if (e.getSource() == showDataInTable)
{
String p2, p3, p4, p5;
p1 = t1.getText();
p2 = t2.getText();
p3 = t3.getText();
p4 = t4.getText();
p5 = t5.getText();
ListShow l1 = new ListShow();
l1.setVisible(true);
}
}
}
public static class FileChooser extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JFileChooser JFileChooser;
int j;
String a1 = "";
static int countLine=0;
private BufferedReader br;
String[][] get() {
String[][] data = new String[50][];
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fileChooser.showOpenDialog(this);
File selectedFile = null;
if (result == javax.swing.JFileChooser.APPROVE_OPTION) {
selectedFile = fileChooser.getSelectedFile();
System.out.println("Selected file: " + selectedFile.getAbsolutePath());
}
try {
String s1 = selectedFile.getAbsolutePath();
FileReader fr = new FileReader(s1); // Reading of file
br = new BufferedReader(fr);
String line = br.readLine();
int j = 0;
while (line != null && !line.isEmpty()) {
data[j] = line.split("-");
j++;
line = br.readLine();
countLine++;
}
}catch (Exception e) {
System.out.println("No file has been choosen");
}
return data;
}
}
public class ListShow extends JFrame {
private static final long serialVersionUID = 1L;
DefaultTableModel model = null;
JFrame screen = new JFrame("Calculation Table");
JPanel pnl = new JPanel();
ListShow() { // Constructor of ListShow class
String h[] = { "Temperature", "Speed", "Dew Point",
"Wind Chill Factor", "Cloud Base Altitude" };
model = new DefaultTableModel(h, 0);
}
void showFunc(String[][] data) { // function to show the data in the Table
setLayout(new FlowLayout());
setSize(100, 500);
for (int i = 0; i < 0; i++) {
double temp = Double.parseDouble(data[i][0]);
double speed = Double.parseDouble(data[i][1]);
double dew = Double.parseDouble(data[i][2]);
double chillFactor = 35.74 + (.6215 * temp) - (35.75 * (Math.pow(speed, 0.16))) + .4275 * temp * (Math.pow(speed, 0.16));
double cloudBase = (temp - dew) / 4.4 * 1000;
data[i][3] = String.valueOf(chillFactor);
data[i][4] = String.valueOf(cloudBase);
model.addRow(data[i]);
}
JTable jtable = new JTable(model);
pnl.setLayout(new BorderLayout());
JScrollPane tableContainer = new JScrollPane(jtable);
pnl.add(tableContainer, BorderLayout.CENTER);
screen.setBackground(Color.ORANGE);
screen.getContentPane().add(pnl);
screen.pack();
screen.setVisible(true);
screen.setIconImage(Toolkit.getDefaultToolkit().getImage("Users\\mihirptel8138\\Desktop\\weather.png"));
}
}
}