添加行(来自2个类的数据)Java后刷新JTable

时间:2014-07-27 13:32:26

标签: java swing oop jtable

这是我的第一个问题......

我有一个小程序,它有一个主框架,其中有两个独立的面板(workConfigWorkTable)。

在第一个面板上添加了Jtable,在第二个面板上添加了一个小配置.. 所以在我向我的jtextfields添加了一些文本后,我将所有数据放在一起并将其交给我的名为workstate的类。在此课程中,方法addCompany()应将数据添加到unternehmen(英语:公司)和LinkedList中的班级jTable。添加两个东西都没问题,但我的Jtable在添加新数据后没有刷新....

我尝试使用repaint()fireChangedData()解决问题,但没有任何效果...... 有人知道如何解决它?

这是我的班级工作状态

public class WorkState {

public LinkedList <Unternehmen> company = new LinkedList();
public WorkTable workTable = new WorkTable();

JLabel label;

public WorkState(){

}

public void addCompany(Unternehmen neu, JLabel label) {
    // TODO Auto-generated method stub
    company.add(neu);
    workTable.addCompany(neu);
    setLable(label);
    System.out.println(calCost());

}

public LinkedList<Unternehmen> getCompanyList(){
    return company;
}


public String setString(){
    String a = "0" ;
    for (int i = 0; i < company.size(); i++){
        a = a + " " + company.get(i).cost;
    }
return a;

}

public int calCost(){
    int all = 0;
    for (int i = 0; i< company.size();i++){
    try {
    String cost = company.get(i).cost;
    NumberFormat numberFormat = NumberFormat.getCurrencyInstance(); 
    final Number parse = numberFormat.parse(cost);
    all += parse.intValue();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
    return all;
}

public void setLable(JLabel label){
    String cost = String.valueOf(calCost());
    label.setText(cost);
}
}

这里是我的班级工作台

public class WorkTable extends JPanel {

/**
 * 
 */
private static final long serialVersionUID = 2053446323153016974L;

final String[] columnNames = {"Firma", "Datum", "Kosten"};
public DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
public JTable table = new JTable(tableModel);

public WorkTable(){
    createGui();

    }


private void createGui() {
    JScrollPane pane = new JScrollPane(table);
    setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Unternehmen"));
    setLayout(new BorderLayout());
    tableModel.addTableModelListener(new TableModelListener(){

        @Override
        public void tableChanged(TableModelEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println("Anzahl: " + table.getRowCount());
            System.out.println("Anzahl der Element: "  +Main.state.company.size());
        }

    });
    add(pane, BorderLayout.CENTER);

    // TODO Auto-generated method stub

}



public void addCompany(Unternehmen u){
    tableModel.addRow(new Object []{u.name,u.date,u.cost});

}



}

使用此类actionEvent我将我的数据提供给workState

public class WorkConfig extends JPanel{

    JCheckBox ja, nein;     
    final public static JTextField tfdCompany = new JTextField();
    final public static JFormattedTextField tfdCost = new JFormattedTextField( NumberFormat.getCurrencyInstance());
    final public static JFormattedTextField tfdDate = new JFormattedTextField(new Date());
    final public static JLabel lbKosten = new JLabel("");
    final public static JCheckBox yes = new JCheckBox("Ja");

    final public JButton btnAdd = new JButton ("hinzufügen");
    final public JButton btnDelete = new JButton("löschen");

    private static final long serialVersionUID = -8912567725873559551L;

    public WorkConfig(){
        createGui();

    }

    private void createGui() {          
        // TODO Auto-generated method stub
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

        //create configPanel
        JPanel pnlConfig = new JPanel();
        pnlConfig.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Konfiguration"));     
        pnlConfig.setLayout(new GridLayout(2,2));
        pnlConfig.add(new JLabel("Unternehmen: "));
        pnlConfig.add(tfdCompany);

        tfdCompany.setFont(new Font("Dialog", 0, 15));
        pnlConfig.add(new JLabel("gleich: "));
        pnlConfig.add(yes);

       pnlConfig.setMinimumSize(new Dimension(300,300));
       pnlConfig.setMaximumSize(new Dimension(300,100));
       pnlConfig.setPreferredSize(new Dimension(300,100));
        this.add(pnlConfig);

        // create costPanel
        JPanel costConfig = new JPanel();
        costConfig.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Kosten"));       
        costConfig.setLayout(new GridLayout(0,2));
        costConfig.add(new JLabel("Kosten: "));;
        tfdCost.setValue(0);
        costConfig.add(tfdCost);
        tfdCost.setFont(new Font("Dialog",0,15));
        costConfig.setMinimumSize(new Dimension(300,75));
        costConfig.setMaximumSize(new Dimension(300,75));
        costConfig.setPreferredSize(new Dimension(300,75));
        this.add(costConfig);

        //create datePanel
        JPanel dateConfig = new JPanel();
        dateConfig.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Datum"));        
        dateConfig.setLayout(new GridLayout(0,2));
        dateConfig.add(new JLabel("Datum: "));;
        dateConfig.add(tfdDate);
        dateConfig.setMinimumSize(new Dimension(300,75));
        dateConfig.setMaximumSize(new Dimension(300,75));
        dateConfig.setPreferredSize(new Dimension(300,75)); 
        this.add(dateConfig);

        //create buttonPanel
         JPanel buttonConfig = new JPanel();
         buttonConfig.setLayout(new GridLayout(0,2));
         buttonConfig.add(btnAdd);;
         btnAdd.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                try {
            //set data  
                    tfdCompany.setText("asd");
                    tfdCost.setValue(2);
                    tfdDate.setText("12.2.2011");
                    checkConfig();

                    Main.state.setLable(lbKosten);
                    addCompany();

                }catch (WorkConfigException ice) {
                        JOptionPane.showMessageDialog(null, ice.getMessage(), "Fehler bei der Eingabe:", JOptionPane.ERROR_MESSAGE);

                }
            }       
        });
        buttonConfig.add(btnDelete);
        btnDelete.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                    System.out.println("button click");
                    for (Unternehmen u : Main.state.company ){

                        System.out.println(u.toString());
                    }
                    System.out.println("Gesamtkosten: " + Main.state.getCost());
                }

            });
        buttonConfig.setMinimumSize(new Dimension(300,75));
        buttonConfig.setMaximumSize(new Dimension(300,75));
        buttonConfig.setPreferredSize(new Dimension(300,75));              

        this.add(buttonConfig);

        //create allPanel
        JPanel allConfig = new JPanel();
        allConfig.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Übersicht"));      
        allConfig.setLayout(new BorderLayout());
        allConfig.add(new JLabel("Gesamtkosten: "),BorderLayout.WEST);
        allConfig.add(lbKosten, BorderLayout.CENTER);
        this.add(allConfig);

    }

    public static void checkConfig() throws WorkConfigException {
        if ( tfdCompany.getText().isEmpty()){
            throw new WorkConfigException("Bitte tragen Sie ein Unternehmen ein!");
        }

        if (tfdCost.getValue().equals(0) ){
            throw new WorkConfigException("Bitte kontrollieren Sie die Eingabe der Kosten!");
        }
        String time = new SimpleDateFormat("dd.MM.yyyy").format(new Date());
        if (tfdDate.getText().equals(time)){
            throw new WorkConfigException("Bitte tragen Sie ein anderes Datum ein!");
        }           
    }

    public static void addCompany(){
        String name = tfdCompany.getText();
        String date = tfdDate.getText();
        String cost = tfdCost.getText();    
        Unternehmen neu = new Unternehmen(name,date,cost);          
        Main.state.addCompany(neu,lbKosten);
    }
}

编辑:我认为问题出在workTable和workState类之间......

0 个答案:

没有答案