Hallo zusammen,
habe ein Problem mit meiner JTable und den hinzufügen eines Objektes...
Unzwar möchte ich aus meiner einen Klasse WorkConfig die Daten aus dem JTextField auslesen und an die Klasse WorkTable weitergeben und hinzufügen
Hier meine WorkConfig
und meine Work
Table
habe ein Problem mit meiner JTable und den hinzufügen eines Objektes...
Unzwar möchte ich aus meiner einen Klasse WorkConfig die Daten aus dem JTextField auslesen und an die Klasse WorkTable weitergeben und hinzufügen
Hier meine WorkConfig
Java:
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 JLabel lbKosten = new JLabel("0");
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 static WorkState workState = new WorkState();
public static WorkTable table = new WorkTable();
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 {
checkConfig();
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");
}
});
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);
workState.addCompany(neu);
table.addToTable(neu);
}
}
Java:
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() {
table.setEnabled(false);
JScrollPane pane = new JScrollPane(table);
setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Unternehmen"));
setLayout(new BorderLayout());
add(pane, BorderLayout.CENTER);
// TODO Auto-generated method stub
}
public void addToTable(Unternehmen u){
String name = u.name;
String cost = u.cost;
String date = u.date;
tableModel.addRow(new Object[]{name,date,cost});
}
}