1

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
TvonH
  • 19
  • 1
  • 2
    *"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."* -- that's up to you. You can either read the text into a JTextArea and have it display it, or you can open a default text editor and have it display it. Which to choose depends on your needs and desires, and is something that only you can answer. – Hovercraft Full Of Eels Dec 12 '22 at 20:49
  • 2
    I will however suggest that you not use null layouts and set-bounds as this can lead to very inflexible GUI's that are hard to maintain and this can also foul-up the ability of Swing text components, such as JTextAreas, from working correctly. Instead, learn and use the Swing layout managers. You can find the layout manager tutorial here: [Layout Manager Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html). Also, you probably will probably want to read: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) – Hovercraft Full Of Eels Dec 12 '22 at 20:51
  • 1
    I would also suggest that you get most of your code out of the static world and into the instance world, to help make it more OOPs compliant and thus somewhat easier to extend and to debug. – Hovercraft Full Of Eels Dec 12 '22 at 20:55
  • 1
    Camickr's answer to [this similar question](https://stackoverflow.com/questions/5880169/loading-a-text-file-into-a-textarea) shows you how to read a text file directly into a JTextArea as will [MadProgrammer's answer here](https://stackoverflow.com/a/17499497/5224440). – Hovercraft Full Of Eels Dec 12 '22 at 20:58
  • And [this Q&A](https://stackoverflow.com/questions/6273221/open-a-text-file-in-the-default-text-editor-via-java) touches on how to open a text file in a default text editor. – Hovercraft Full Of Eels Dec 12 '22 at 21:13

0 Answers0