简体   繁体   中英

Java Applet Table Not Showing When Run

I want to display a table after the user has provided input. I can't figure out why the table won't show.

I created my table as described here (Stack Overflow) and here (Rose India) in order to create a table I can add to,

but my code doesn't add the table to the applet, particularly here:

aTable = new MDTable(mdList);
add( aTable );

Rest of the code, full paste here (Pastebin) :

public class FMCSAApplet extends Applet implements ActionListener {

String zipcode;

Label lab1;
TextField t1;
Button submitButton;
MDTable aTable;

public void init()
{
    setLayout(new FlowLayout()); 
    submitButton = new Button("Submit"); 
    lab1 = new Label("Enter a zipcode:");
    t1 = new TextField(15);

    add(lab1);
    add(t1);
    add(submitButton);
    submitButton.addActionListener(this); 
}

public void paint(Graphics g)
{
    g.drawString("Zip code: "+zipcode,30,100);
    if(zipcode!=null) {
        g.drawString("dotNum\t inputZip\t name\t\t city\t state\t mileage\t unsafe\t fatigued\t fitness\t substance\t maintenance\t inspections",30,120);
        try {
            ..Code Stripped..
                            // get info from database, add to ArayList<MData> mdList
                                    ..Code Stripped..
                                    mdList.add(motorcoach);
                }
            } // end while
            rs.close();

            aTable = new MDTable(mdList);
            add( aTable );

        }catch (Exception f) {
            System.out.println(f);
        }
    }
}

public void actionPerformed(ActionEvent evt) 
{
    // Here we will ask what component called this method
     if (evt.getSource() == submitButton) {
         zipcode = t1.getText();
         repaint();
     }
}

public boolean action(Event e,Object o)
{
    zipcode = t1.getText();
    repaint();
    return true;
}

} //end FMCSAApplet Class

class MDTable extends JPanel {
    public MDTable(ArrayList<MData> md) {
        Object[][] cellData = {
            {1,2,"3","4","5",6,7,8,9,10,11,12}     
        };

        String[] columnNames = {"dotNum","inputZip", "name","city","state","mileage","unsafe","fatigued","fitness","substance","maintenance","inspections"};
        add(  new JTable(cellData, columnNames) ) ;
        DefaultTableModel model = new DefaultTableModel(cellData,columnNames);
        JTable table = new JTable(model);
        for (MData md1 : md) {
            model.insertRow(table.getRowCount(),new Object[]{md1.getDotNum(), md1.getInputZip(), md1.getName(), md1.getCity(), md1.getState(), md1.getMileage(), md1.getUnsafe(), md1.getFatigued(), md1.getFitness(), md1.getSubstance(), md1.getMaintenance(), md1.getInspections()});
        }
    } //end MDTable Constructor
} //end MDTable Class

class MData {
        ..Code Stripped..
        public MData(int d, int i, String n, String c, String s, int m, int u, int f, int fi, int su, int ma, int in)
        ..Code Stripped..
}

You will need to validate the container after you add the MDTable component:

add( aTable );
validate();
...

Also its not a good idea to have database calls inside your paint method. paint will get called multiple times when the applet regains focus or resized for instance. One suggestion would be to extract out the database call into a separate method and add a "Reload" button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM