JTable adding Row

frosty

Mitglied
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
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);
		
	}


	
}
und meine Work
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});
		
	}
			
}
Table
 
Hey
unzwar möchte ich dem Table etwas hinzufügen.

1. Button löst ein ActionEvent aus (das funktioniert auch)
2. die Methode actionPerformed soll dann die Methode addCompany ausführen (funktioniert auch)
3. die Mehtode addCompany soll dann die einzelnen TextFields auslesen, eine neue Company erstellen und in die Table hinzufügen
1. die Company erstellen (klappt auch)
2. die Company in die Table hinzufügen(klappt auch [lasse mir dafür die Anzahl nach jeder Änderung anzeigen])
3. die Table mit den neuen Werten angezeigt (klappt nicht)

Jedoch weiß ich nicht wie ich das lösen soll...
Habe es mit repaint() und tableModel.fireDataChanged(); schon probiert - ohne erfolg...
 
Also ich hab mir gerade aus deinen Codesnippets ein KSKB zusammengebastelt und es funktioniert tadellos. Das Problem was ich sehe ist eventuell, dass du zwei Instanzen von WorkTable verwendest und deshalb der Grund ist, warum die Table nicht aktualisiert wird.
 
So ich hab dir das mal zusammenkopiert und kannst dann selbst schauen wie du "eine und nur eine" Instanz von deinem table Element verwendest. Ich hab auch viele static Sachen verändert (du solltest dir auch nocheinmal durchlesen was static eigentlich macht).

Java:
package table;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class WorkMain {
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(() -> new WorkMain());
	}
	
	public WorkMain() {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(new WorkConfig());
		
		frame.pack();
		frame.setVisible(true);
	}
	
	public class WorkConfig extends JPanel {

		JCheckBox ja, nein;

		final public JTextField tfdCompany = new JTextField();
		final public JFormattedTextField tfdCost = new JFormattedTextField(
				NumberFormat.getCurrencyInstance());
		final public JFormattedTextField tfdDate = new JFormattedTextField(
				new Date());
		final public JLabel lbKosten = new JLabel("0");
		final public 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 WorkTable table = new WorkTable();

		public WorkConfig() {
			createGui();
		}

		private void createGui() {

			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) {
					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);
			this.add(table);

		}

		public 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 void addCompany() {
			String name = tfdCompany.getText();
			String date = tfdDate.getText();
			String cost = tfdCost.getText();

			Unternehmen neu = new Unternehmen(name, date, cost);
			table.addToTable(neu);

		}

		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);

			}

			public void addToTable(Unternehmen u) {
				String name = u.name;
				String cost = u.cost;
				String date = u.date;

				tableModel.addRow(new Object[] { name, date, cost });
			}

		}

		public class Unternehmen {

			public String name, cost, date;

			public Unternehmen(String name, String cost, String date) {
				this.name = name;
				this.cost = cost;
				this.date = date;
			}
		}

		public class WorkConfigException extends Exception {
			private static final long serialVersionUID = 1L;

			public WorkConfigException(String s) {
				super(s);
			}
		}
	}
}
 

Zurück
Oben