I am writing a program in which I have done ALOT of coding already but there are still problems and features to be added.

So far my program:

1) Takes data from a .dat file and displays it on a table
2) There is a button for which you can add to the .dat file

Concerning the .dat file, it is organized into lines. Each line is data for a member....for example:

12345678Kenneth Wardman Westfirst High School 2007I12500

That is the first line of my data file..first 8 characters are the member #, next 20 is the member name, next 25 is the member school, next 4 is the year joined, next 1 is I/A for inactive/active, and the next 5 characters represent the $ owed. This pattern is inherent for all the lines of the .dat file

I have a function that successfully splits each line into sections and displays them in an organized fashion. I also have a function (as i said before) thats prompts the user for info and then adds the line to the .dat file in the above format.

What I have left to do is:

1) Make it so I can somehow change the member data (you'll see how i've organized things it in my code that i have attached)

2) Make it so that the table can "refresh" so i don't have to exit and then rerun the program in order to view the changes (the lines that I've added)

3) Get the button BELOW the table rather than covering the top part of the table (i stink at swing).

4) For some reason, my actionlistener doesnt function quite right. I wrote

source = event.getSource(); //event is the actionlistener parameter

if (source == addInfo) //addInfo is the name of my button
//at this point it says that addInfo is initialized or somthing...

This is in my code that is attached.

I have attached my .java files and the .dat file as .txt files. (just incase people are suspicious...)

model.txt -> copy and paste text into notepad and save as model.java
table.txt -> copy and paste text into notepad and save as table.java and put in same folder as model.java
nlc2008.txt -> copy and paste text into notepad and save as nlc2008.dat and put in the same folder (or whereever it needs to be for the file to be found by the program)
I really am in desperate need of expert advice....

Thanks in advance!

model.txt

table.txt

nlc2008.txt

Recommended Answers

All 2 Replies

People are normally averse to opening attachments; it would be better to paste the *relevant* piece of code and describe a *single* problem at a time rather than confusing the reader with a lot of details.

For class names use first letter big. (public class Table)
You need decide where your data is first plane. Inside table-model or inside file?

a few clarifications inside code-text
-ActionListener - button recognize
-Layout
-ViewportView scroll bar policy
-table.repaint
-table-model update

//DOCUMENT EVERYTHING
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Table extends JPanel implements ActionListener {

    // make table as Table-class member, to be visible inside
    // public void actionPerformed(ActionEvent event) {
    private JTable table;//////
    private static final String ADD_DATA = "Add Data";/////////////

    public Table(String dataFilePath) {
        Model model;
        Font f;
        JButton addInfo;
        f = new Font("SanSerif", Font.PLAIN, 24);
        setFont(f);
        setLayout(new BorderLayout());
        model = new Model(dataFilePath);
        table = new JTable();
        table.setModel(model);
        table.createDefaultColumnsFromModel();
        table.setFillsViewportHeight(true);////////////MAGIC LINE
        //GridLayout layout = new GridLayout(80, 30);

        addInfo = new JButton(ADD_DATA) {
            //@Override

            public Dimension getPreferredSize() {
                return new Dimension(80, 30);
            }
        };
        // addInfo.setSize(180, 30); 
        addInfo.addActionListener(this); //action listener is somewhere below
        //i'm not good at swing...and I don't know how to make the button 
        //appear AFTER the table rather than right on top of it...
        //I could use some advice here...but not the most important of things
        JPanel buttonPanel = new JPanel();/////////////
        buttonPanel.setLayout(new FlowLayout());///////
        buttonPanel.add(addInfo);//////////////////////
        add(buttonPanel, BorderLayout.SOUTH);//////////
        //////////////

        JScrollPane scrollpane = new JScrollPane();
        scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollpane.setCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER, new JLabel("#"));
        scrollpane.setWheelScrollingEnabled(true);//for future use
        scrollpane.setViewportView(table);
        add(scrollpane, BorderLayout.CENTER);///////////

        //LAF......
    }

    public void actionPerformed(ActionEvent event) {
        // to look
        System.out.println(event);
        System.out.println(event.getSource());
        System.out.println(event.getSource().getClass());
        System.out.println(event.getSource().getClass().equals(JButton.class));
        if ((event.getSource().getClass().equals(JButton.class))) {
            String source = ((JButton) event.getSource()).getText();
            if (source.equals(ADD_DATA)) {
                System.out.println(source);
               // your ADD_DATA button code
              
/// trimmer...

               Model.addData(addLine); //calling addData function from model, to write to file  //// and to write to table-model

              table.repaint();////////////////////////////
            }
        }
    }
///.... rest
}

Make a possibility to update table-model
// split to two separate methods ?

public class Model extends AbstractTableModel {
//...
    public static void addData(String str) {
        //part 1 - add data to model
       // same code - redundant! - same as used during read from file
       // make reuseble
        String[] characters = new String[str.length()];
        for (int i = 0; i < str.length(); ++i) {
            characters[i] = String.valueOf(str.charAt(i));
        }
        // make xxxCutter(characters) methods - static 
        data.addElement(numCutter(characters)); //see the numCutter function below
        data.addElement(nameCutter(characters));
        data.addElement(schoolCutter(characters));
        data.addElement(yearCutter(characters));
        data.addElement(actCutter(characters));
        data.addElement(owedCutter(characters));
        //part 2 - add data to file
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter("nlc2008.dat", true));
            out.write(str);
            out.newLine();
            out.close();
        } catch (IOException e) {
        }
    }
//...
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.