Is There a Way to Make a Java Method that Opens a .txt File as a pop-up?

211 Views Asked by At

I am currently making a java project that simulates a website for swimmers. It has multiple classes and incorporates the Jswing GUI. I have buttons that are used to display other classes GUI and data, but I was wondering if there was a way to have it so when I click a button, it would pull up a .txt file as a pop up externally, as opposed to just reading and writing the .txt files data into my console. I had initially tried to display information from a .txt file into a JLabel or JTextField so it could be seen but I had abandoned that idea in order to pursue the easier and simplier idea of bringing up a entirely new window of a .txt file

package finalProject;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class FrontInterface extends JPanel {
    private static JButton nSwimmer;
    private static JButton cSwimmer;
    private static JButton coach;
    private JLabel heading;
    private static JButton equipment;

public FrontInterface() {
    //construct components
    nSwimmer = new JButton ("New Swimmer");
    cSwimmer = new JButton ("Current Swimmer");
    coach = new JButton ("Coach");
    heading = new JLabel ("Welcome to the Swimming Station");
    equipment = new JButton ("Equipment");

    //adjust size and set layout
    setPreferredSize (new Dimension (752, 425));
    setLayout (null);

    //add components
    add (nSwimmer);
    add (cSwimmer);
    add (coach);
    add (heading);
    add (equipment);

    //set component bounds (only needed by Absolute Positioning)
    nSwimmer.setBounds (280, 150, 200, 20);
    cSwimmer.setBounds (280, 175, 200, 20);
    coach.setBounds (280, 200, 200, 20);
    heading.setBounds (280, 95, 195, 30);
    equipment.setBounds (280, 225, 200, 25);
}

public static void showWindow(){
    JFrame frame = new JFrame ("Front Interface");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new FrontInterface());
    frame.pack();
    frame.setVisible(true);
    
    nSwimmer.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            frame.setVisible(false);
            NewSwimmerForm w2 = new NewSwimmerForm();
            w2.showWindow();
        }   
    });
    
    cSwimmer.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            frame.setVisible(false);
            CurrentSwimmer w2_1 = new CurrentSwimmer();
            w2_1.showWindow();
        }   
    });
    
    coach.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            frame.setVisible(false);
            NewSwimmerForm w2 = new NewSwimmerForm();
            w2.showWindow();
        }   
    });
    
    equipment.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            frame.setVisible(false);
            NewSwimmerForm w2 = new NewSwimmerForm();
            w2.showWindow();
        }   
    });
    
    frame.setVisible(true);
}    

    public static void main (String[] args) {
        showWindow();
    }
}

And below is the code that the cSwimmer button will take you to upon clicking the cSwimmer button:

package finalProject; 

import finalProject.FrontInterface;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.*;
import java.awt.*;
import java.io.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class CurrentSwimmer extends JPanel {
private static JTextArea informationHeader;

public static void showWindow(){
    try{
     File file = new File("C:\\Users\\trent\\Desktop\\Intro to Computer Science\\Eclipse Codes\\FinalProject\\src\\finalProject\\history.txt");
       System.out.println(file.getCanonicalPath());
       FileInputStream ft = new FileInputStream(file);

       DataInputStream in = new DataInputStream(ft);
       BufferedReader br = new BufferedReader(new InputStreamReader(in));
       String strline;

       while((strline = br.readLine()) != null){
           System.out.println(strline);
       }
       in.close();
   }catch(Exception e){
       System.err.println("Error: " + e.getMessage());
       
   }
}
    public static void main(String [] args) {               
    showWindow(); 
    }
}

Now, I am completely unsure of how to go about my situation, whether it be I display the information from the .txt file onto a jTextArea or if I should just create a method to make a .txt file open externally and replace the pop up window I would otherwise have with the writing to JTextArea.

0

There are 0 best solutions below