-1

my current program takes in text from some JTextFields and adds it to a file to keep track of medications. What i want to do, is add another button to the bottom of the program that will open a second jframe to display the medications that are already documented, but all attempts have been brutally unsuccessful. Below is the code that i am currently working with.

Much Thanks.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


@SuppressWarnings({ "serial" })
public class MedGUITest extends JFrame {

    private JLabel medName;
    private JLabel medTime;
    private JLabel medDose;
    private JLabel finished;

    private JTextField textFieldMed;
    private JTextField textFieldTime;
    private JTextField textFieldDose;

    private JButton submitButton;
    private JButton meds;

    public MedGUITest(){
        setLayout(new FlowLayout());

        medName = new JLabel("Type Medication Here:");
        add(medName);

        textFieldMed = new JTextField("",15);
        add(textFieldMed);

        medDose = new JLabel("Type Medication Dose:");
        add(medDose);

        textFieldDose = new JTextField("",15);
        add(textFieldDose);

        medTime = new JLabel("Type Medication Time:");
        add(medTime);

        textFieldTime = new JTextField("",15);
        add(textFieldTime);

        submitButton = new JButton("Click Here to Add");
        event e = new event();
        submitButton.addActionListener(e);
        add(submitButton);

        meds = new JButton("Click Here to see meds");
        event2 r = new event2();
        meds.addActionListener(r);
        add(meds);

        finished = new JLabel("");
        add(finished);
    }


    public class event implements ActionListener {

        public void actionPerformed(ActionEvent e){
            String medName = textFieldMed.getText();
            String medDose = textFieldDose.getText();
            String medTime = textFieldTime.getText();

            File med = new File("med.txt");

            try(PrintWriter out= new PrintWriter(new FileWriter(med,true))) {
                out.println("Medication: " + medName + " " +"Dosage: "+ medDose + " Mg"+ " " +"Time of day: "+ medTime);
                finished.setText("Your Med has been added");
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }
    }

    public class event2 implements ActionListener{

        public void actionPerformed(ActionEvent e){

        }

    }

    public static void main(String args[]) throws IOException{

    int winWidth = 300;
    int winLength = 300;

    MedGUITest gui = new MedGUITest();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setSize(winWidth, winLength);
    gui.setVisible(true);
    gui.setTitle("Standard GUI");


    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
sirnomnomz
  • 623
  • 1
  • 7
  • 20
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Oct 06 '14 at 00:10
  • *"..all attempts have been brutally unsuccessful."* That code only shows any reference to a single frame. Show us one of your attempts as opposed to posting a 'template' for others to complete. – Andrew Thompson Oct 06 '14 at 00:11
  • Take a look at [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer Oct 06 '14 at 00:18

1 Answers1

3

You would probably want to use a JDialog instead of another JFrame. You just set it up the same way you set up any other JFrame or JDialog. You create the frame/dialog, add components and then call pack() then setvisible(true). You just want to make sure that you don't set JFrame.setDefaultCloseOperation() to close when the close your second JFrame.

I find it easier to set up my classes to sub-class JPanel instead of JFrame so then it is easier to reuse it in other places.

markbernard
  • 1,412
  • 9
  • 18