Best way to go about this swing application?

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
In trying to build up my experience with swing I've decided to create a little program to get me more familiar with the basics. I'm creating a program that will display information on a list of medications in a well presented manner (for some pharmacology stuff I'm working on).

My plan is to be able to provide a list of medications which the program then puts into JPanels based on their class and then allows the user to click on them bringing up a new window with information on them.

Now I've created a main class which creates the JFrame and main panels and then a class which fills up the panels with loads of buttons based on the class of medication. But I'm unsure of where to go from here, do I create a new class that extends JFrame and give each jbutton a reference to it so that when I click on the button it brings up a new JFrame, instead of using JButtons should I be using a class that extends JButton?

This is what I've got so far, presentation is still a bit messy at the moment.

Code:
package medchecker;
import javax.swing.*;
import java.awt.*;

public class Main extends JFrame {
    JPanel mainPanel;
    GridLayout mainLayout;

    public Main() {
        super("Anxiolytics");
        mainPanel = new JPanel();
        mainLayout = new GridLayout(4, 4, 10, 10);

        for (int x = 0; x < 16; x++) {
            mainPanel.add(new MedClass(x));
        }

        this.setContentPane(mainPanel);
        this.setLayout(mainLayout);
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

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

Code:
package medchecker;
import javax.swing.*;
import java.awt.*;
import javax.swing.BorderFactory;
import java.io.*;

public class MedClass extends JPanel {

    GridLayout layout;
    JLabel className;

    public MedClass(int number) {
        super();
        className = new JLabel();
        layout = new GridLayout(0, 3, 5, 5);
        getMeds(number);
        this.setLayout(layout);
        this.setBorder(BorderFactory.createLineBorder(Color.black));
    }

    public void getMeds(int number) {
        BufferedReader bufferedReader;
	String data;
        int numberDone = -1;

	try {
            bufferedReader = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Documents\\meds.txt"));
            while ((data = bufferedReader.readLine()) != null) {
                if (data.contains("*")) {
                    numberDone += 1;
                    if (numberDone == number) {
                        className.setText(data);
                    }
                }
                if (!data.contains("*") && !data.contains("_") && numberDone == number) {
                   this.add(new JButton(data));
                } 
            }

	} catch (FileNotFoundException fnfe) {
            JOptionPane.showMessageDialog(null, "Medication file not found!");
	} catch (IOException ioe) {
            JOptionPane.showMessageDialog(null, "File not formatted correctly!");
	}
    }
}

Code:
Selective Serotonin  Reuptake Inhibitors*
Escitalopram
Fluoxetine
Fluvoxamine
Paroxetine
Sertraline
Noradrenaline Reuptake Inhibitors*
Atomoxetine
Mazindol
Reboxetine
Viloxazine
Serotonin-Noradrenaline Reuptake Inhibitors*
Desvenlafaxine
Duloxetine
Milnacipran
Sibutramine
Noradrenaline-Dopamine Reuptake Inhibitors*
Bupropion
Cocaine
Dextroamphetamine
Dextromethamphetamine
Dextromethylphenidate
Tetracyclics*
Maprotiline
Mianserin
Mirtazapine
Setiptiline
Tricyclics*
Amitriptyline
Amitriptylinoxide
Butriptyline
Clomipramine
Demexiptiline
Desipramine
Dibenzepin
Dimetacrine
Dosulepin
Doxepin
Imipramine
Imipraminoxide
Iprindole
Lofepramine
Melitracen
Metapramine
Nitroxazepine
Nortriptyline
Noxiptiline
Propizepine
Protriptyline
Quinupramine
Trimipramine
Opipramol
Monoamine Oxidase Inhibitors*
Non-selective_
Caroxazone
Isocarboxazid
Nialamide
Phenelzine
Safrazine
Tranylcypromine
MAO-A selective_
Brofaromine
Metralindole
Moclobemide
Pirlindole
Toloxatone
MAO-B selective_
Pargyline
Rasagiline
Selegiline
Benzodiazepines*
Alprazolam
Bromazepam
Chlordiazepoxide
Cinolazepam
Clonazepam
Cloxazolam
Clorazepate
Diazepam
Estazolam
Flunitrazepam
Flurazepam
Flutoprazepam
Halazepam
Ketazolam
Loprazolam
Lorazepam
Lormetazepam
Medazepam
Nimetazepam
Nitrazepam
Nordazepam
Oxazepam
Phenazepam
Pinazepam
Prazepam
Premazepam
Quazepam
Temazepam
Tetrazepam
Barbiturates*
Allobarbital
Alphenal
Amobarbital
Aprobarbita
Brallobarbital
Butobarbital
Butalbital
Cyclobarbital
Methylphenobarbital
MephobarbitalPentobarbital
Phenobarbital
Secobarbital
Talbutal
Opioids*
Buprenorphine
Codeine
Dihydrocodeine
Fentanyl
Hydrocodone
Hydromorphone
Methadone
Morphine
Oxycodone
Oxymorphone
Propoxyphene
Tramadol
NMDA Antagonists*
Amantadine
Dextromethorphan
Esketamine
Memantine
Rimantadine
Tiletamine
Beta Blockers*
Atenolol
Metoprolol
Propranolol
Carbamates*
Emylcamate
Mebutamate
Meprobamate
Phenprobamate
Procymate
D2-Like Agonists*
Apomorphine
Bromocriptine
Piribedil
Pramipexole
Ropinirole
Rotigotine
Miscellaneous*
Afobazole
Agomelatine
Buspirone
Chloral-Hydrate
Gamma-hydroxybutyrate
Nefazadone
Nicotine
Cannabis
Riluzole
Tandospirone
Tianeptine
Trazadone

javaprogram.jpg
 
Last edited:
I would probably look to add the JPanels for each class of medication to a JTabbedPane on your JFrame rather than to the JFrame itself. That should make navigation a bit simpler.

For displaying the information I would probably create a new class that extends JFrame and add a JLabel to it. An action handler added to each button could retreive the information from whatever data source you are using and set that as the text for the JLabel and display the JFrame.

I've only started using swing myself so hopefully that makes some sense.
 
Back
Top Bottom