Problemy z JTabel

0

Witam jestem nowicjuszem jesli chodzi o programowanie. Mam kilka problemow których nie jestem w stanie rozwiązać. Po pierwsze po odpaleniu programu widoczna jest tylko tabela, dopiero po najechaniu, kliknieciu lub zminimalizowaniu okna pojawiaja sie elemety. Po drugie tabela nie odswieza sie. Po trzecie, w sumie to nie problem ale prosba zeby ktos ogarniajacy mvc spojrzał na kod i powiedzial czy jest on dobrze napisany jesli nie to co jest zle i co mam poprawic.

Model

import java.util.Random;
import javax.swing.table.AbstractTableModel;

public class AplikacjaModel extends AbstractTableModel
{
	private final int countRowTable = 5;
	private final int countColumnTable = 5;
	private Integer[][] data = new Integer[countRowTable][countColumnTable];
	private String[] colName = {"1","2","3","4","5"};
	
	public AplikacjaModel() {
		super();
		setZeroTable();
	}
	public int getColumnCount() {
		return countColumnTable;
	}
	public int getRowCount() {
		return countColumnTable;
	}
	public Object getValueAt(int row, int col) {
		Object object = (Object) data[row][col];
		return object;
	}
	public Integer[][] getIntegerValuesTable() {
		return data;
	}
	public String getStringValuesTable() {
		String str = "";
		for(int i=0; i<countRowTable; i++)
			for(int j=0; j<countColumnTable; j++) {
				str = str + data[i][j] +" ";
			}
		return str;
	}
	public String getColumnName(int col) {
		return colName[col];
	}
	public String[] getColumnNames() { 
		return colName;
	}
	public void setValue(Integer value, int row, int col) {
		data[row][col] = value;
		fireTableDataChanged();	
	}
	public void setRandomTable() {
		Random random = new Random();
		for(int i=0; i<countRowTable; i++)
			for(int j=0; j<countColumnTable; j++) {
				// ograniczenie znaku liczby i zakresu do 10000
				data[i][j] = Math.abs(random.nextInt()) % 10000;
			}
		fireTableDataChanged();
	}
	public void setZeroTable() {
		for(int i=0; i<countRowTable; i++)
			for(int j=0; j<countColumnTable; j++) {
				data[i][j] = new Integer(0);
			}
		fireTableDataChanged();
	}
	public Integer calculateSum() {
		Integer sum = new Integer(0);
		for(int i=0; i<countRowTable; i++)
			for(int j=0; j<countColumnTable; j++) {
				sum = sum + data[i][j];
			}
		return sum;
	}
	public Float calculateAverage() {
		Float avg = new Float(0.0);
		Integer sum = calculateSum();
		if(sum > 0) avg = (sum.floatValue())/(countRowTable*countColumnTable);
		return avg;
	}
		

	public int calculateMax() // oblicza liczbe max
	{
		int maximum = data[0][0];
		int minimum = data[0][0];
		
		for(int i=0; i<data.length; i++)
		{
			for(int j=0; j<data.length; j++)
			if(data[i][j]>maximum) 
			{
				maximum=data[i][j];	
			}
			else if(data[i][j]<minimum) 
			{
				minimum=data[i][j];	
			}	
		}
		return maximum;
	}

		public int calculateMin()  //oblicza liczbe min
		{
			int maximum = data[0][0];
			int minimum = data[0][0];
			
			for(int i=0; i<data.length; i++)
			{
				for(int j=0; j<data.length; j++)
				if(data[i][j]>maximum) 
				{
					maximum=data[i][j];	
				}
				else if(data[i][j]<minimum) 
				{
					minimum=data[i][j];	
				}	
			}
			return minimum;
		}
		

		
}

View

import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.table.TableModel;

public class AplikacjaView extends JFrame
{
	
	private JLabel lblNumerKolumny, lblNumerWiersza, lblWprowadzLiczbe, lblWybierzObiczenia;
	private JComboBox comboBox, comboBox_1;
	private JTextField textField,textField_1;
	private JTable table;
	private JToolBar toolBar;
	private JButton btnZapisz, btnDodaj, btnWyzeruj, btnWypelnij, btnOblicz;
	private JButton btZapisz, btDodaj, btWyzeruj, btWypelnij, btSuma, btSrednia, btMin, btMax, btInformacje, btPomoc;
	private JMenuBar menuBar;
	private JMenu mnPlik, mnTabela, mnObliczenia, mnPomoc;
	private JScrollPane tableScrollPane;
	
	
 public AplikacjaView()
 {

		setFont(null);
		setForeground(new Color(240, 240, 240));
		setBounds(100, 100, 700, 470);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(null);
		setVisible(true);
		
		toolBar = new JToolBar();
		toolBar.setBounds(0, 0, 682, 25);
		getContentPane().add(toolBar);
		
		 btZapisz = new JButton("Zapisz"); 
		toolBar.add(btZapisz);
		
		btDodaj = new JButton("Dodaj");
		toolBar.add(btDodaj);
		
		btWyzeruj = new JButton("Wyzeruj");
		toolBar.add(btWyzeruj);
		
		btWypelnij = new JButton("Wypelnij");
		toolBar.add(btWypelnij);
		
		btSuma = new JButton("Suma");
		toolBar.add(btSuma);
		
		btSrednia = new JButton("Srednia");
		toolBar.add(btSrednia);
		
		btMin = new JButton("Min");
		toolBar.add(btMin);
		
		btMax = new JButton("Max");
		toolBar.add(btMax);
		
		btPomoc = new JButton("Pomoc");
		toolBar.add(btPomoc);
		
		btInformacje = new JButton("Informacje");
		toolBar.add(btInformacje);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 25, 682, 372);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		lblWprowadzLiczbe = new JLabel("Wprowadz liczbe");
		lblWprowadzLiczbe.setBounds(12, 13, 104, 16);
		panel.add(lblWprowadzLiczbe);
		
		textField = new JTextField();
		textField.setBounds(119, 10, 116, 22);
		panel.add(textField);
		textField.setColumns(10);
		
		lblNumerWiersza = new JLabel("Numer wiersza");
		lblNumerWiersza.setBounds(260, 13, 93, 16);
		panel.add(lblNumerWiersza);
		
		lblNumerKolumny = new JLabel("Numer kolumny");
		lblNumerKolumny.setBounds(441, 16, 93, 16);
		panel.add(lblNumerKolumny);	
		
		table = new JTable(new AplikacjaModel());
		table.setBounds(10, 63, 520, 103);
		panel.add(table);
		
		tableScrollPane = new JScrollPane(table);
		tableScrollPane.setBounds(10, 63, 520, 103);
		panel.add(tableScrollPane);
		
		btnDodaj = new JButton("Dodaj");
		btnDodaj.setBounds(557, 58, 97, 25);
		panel.add(btnDodaj);
		
		btnWypelnij = new JButton("Wypelnij");
		btnWypelnij.setBounds(557, 96, 97, 25);
		panel.add(btnWypelnij);
		
		btnWyzeruj = new JButton("Wyzeruj");
		btnWyzeruj.setBounds(557, 134, 97, 25);
		panel.add(btnWyzeruj);
		
		btnZapisz = new JButton("Zapisz");
		btnZapisz.setBounds(557, 172, 97, 25);
		panel.add(btnZapisz);
		
		lblWybierzObiczenia = new JLabel("Wybierz obiczenia: ");
		lblWybierzObiczenia.setBounds(12, 176, 116, 16);
		panel.add(lblWybierzObiczenia);	
		
		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setBounds(76, 242, 522, 102);
		panel.add(textField_1);
		
		btnOblicz = new JButton("Oblicz");
		btnOblicz.setBounds(241, 172, 97, 25);
		panel.add(btnOblicz);
		
		String[] selections = { "Suma", "srednia", "Minimum", "Maximum" };
		JList list = new JList(selections);
		list.setBounds(136, 170, 64, 79);
		panel.add(list);
		
		comboBox = new JComboBox();
		comboBox.setBounds(365, 10, 42, 22);
		comboBox.addItem("1");
		comboBox.addItem("2");
		comboBox.addItem("3");
		comboBox.addItem("4");
		comboBox.addItem("5");
		panel.add(comboBox);
		
		comboBox_1 = new JComboBox();
		comboBox_1.setBounds(546, 13, 42, 22);
		comboBox_1.addItem("1");
		comboBox_1.addItem("2");
		comboBox_1.addItem("3");
		comboBox_1.addItem("4");
		comboBox_1.addItem("5");
		panel.add(comboBox_1);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnPlik = new JMenu("Plik");
		menuBar.add(mnPlik);
		
		mnTabela = new JMenu("Tabela");
		menuBar.add(mnTabela);

		
		mnObliczenia = new JMenu("Obliczenia");
		menuBar.add(mnObliczenia);
		
		mnPomoc = new JMenu("Pomoc");
		menuBar.add(mnPomoc); 
 }
 

		 public void setSuma(int suma)
		 {
			 textField_1.setText(Integer.toString(suma));
		 }
		 
		 public void setSrednia(float srednia)
		 {
			 textField_1.setText(Float.toString(srednia));
		 }
		 
		 public void setMin(int min)
		 {
			 textField_1.setText(Integer.toString(min));
		 }
		 
		 public void setMax(int max)
		 {
			 textField_1.setText(Integer.toString(max));
		 }

		 public JButton getBtPomoc()
		 {
			 return btPomoc;
		 }
		 public JButton getBtInformacje()
		 {
			 return btInformacje;
		 }
		 public JButton getBtMax()
		 {
			 return btMax;
		 }
		 public JButton getBtMin()
		 {
			 return btMin;
		 }
		 public JButton getBtSrednia()
		 {
			 return btSrednia;
		 }
		 public JButton getBtZapsisz()
		 {
			 return btZapisz;
		 }
		 public JButton getBtSuma()
		 {
			 return btSuma;
		 }
		 public JButton getBtWypelnij()
		 {
			 return btWypelnij;
		 }
		 public JButton getBtWyzeruj()
		 {
			 return btWyzeruj;
		 }
		 public JButton getBtDodaj()
		 {
			 return btnDodaj;
		 }
		 public JButton getBtZapisz()
		 {
			 return btZapisz;
		 }
		 public JButton getBtnOblicz()
		 {
			 return btnOblicz;
		 }
		 public JButton getBtnWypelnij()
		 {
			 return btnWypelnij;
		 }
		 public JButton getBtnWyzeruj()
		 {
			 return btnWyzeruj;
		 }
		 public JButton getBtnDodaj()
		 {
			 return btnDodaj;
		 }
		 public JButton getBtnZapisz()
		 {
			 return btnZapisz;
		 }
		  
		 void addCalculateListener(ActionListener listenForCalcButton)
		 {
			 btnZapisz.addActionListener(listenForCalcButton);
			 btnDodaj.addActionListener(listenForCalcButton);
			 btnWyzeruj.addActionListener(listenForCalcButton);
			 btnWypelnij.addActionListener(listenForCalcButton);
			 btnOblicz.addActionListener(listenForCalcButton);
			 btSuma.addActionListener(listenForCalcButton);
			 btZapisz.addActionListener(listenForCalcButton);
			 btDodaj.addActionListener(listenForCalcButton);
			 btWyzeruj.addActionListener(listenForCalcButton);
			 btWypelnij.addActionListener(listenForCalcButton);
			 btSrednia.addActionListener(listenForCalcButton);
			 btMin.addActionListener(listenForCalcButton);
			 btMax.addActionListener(listenForCalcButton);
			 btInformacje.addActionListener(listenForCalcButton);
			 btPomoc.addActionListener(listenForCalcButton);
			 
		 }
		 
	 
void displayErrorMessage(String errorMessage)
	 {
	
		 JOptionPane.showMessageDialog( null, this, errorMessage, 0);
	 }

}

Controller


{
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class AplikacjaController
{
	private AplikacjaModel theModel;
	private AplikacjaView theView;
	
	public AplikacjaController(AplikacjaModel theModel, AplikacjaView theView)
	{
		this.theModel = theModel;
		this.theView = theView;
		
		this.theView.addCalculateListener(new CalculateListener());
	}
	
	class CalculateListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			Object zrodlo = e.getSource();
			
			// bt
			
			if(zrodlo==theView.getBtSuma())
			{
				theView.setSuma(theModel.calculateSum());
			}
			else if(zrodlo==theView.getBtMin())
			{
				theView.setMin(theModel.calculateMin());
			}
			else if(zrodlo==theView.getBtMax())
			{
				theView.setMax(theModel.calculateMax());
			}
			else if(zrodlo==theView.getBtSrednia())
			{
				theView.setSrednia(theModel.calculateAverage());
			}
			else if(zrodlo==theView.getBtWypelnij())
			{
				theModel.setRandomTable();
			}
			else if(zrodlo==theView.getBtWyzeruj())
			{
				theModel.setZeroTable();
			}
			else if(zrodlo==theView.getBtZapisz())
			{
				theModel.setZeroTable();
			}
			
			// btn
			
			else if(zrodlo==theView.getBtnWypelnij())
			{
				theModel.setRandomTable();
			}
			else if(zrodlo==theView.getBtnWyzeruj())
			{
				theModel.setZeroTable();
			}

			
		}
	}
}

Test



public class AplikacjaTest {

	public static void main(String[] args)
	{
		
		AplikacjaView theView = new AplikacjaView();
		AplikacjaModel theModel = new AplikacjaModel();
		AplikacjaController theController = new AplikacjaController(theModel, theView);
		
		

	}

}

0
  1. Za wcześnie wywołujesz setVisible(true), powinno być dopiero po dodaniu wszystkich komponentów.
  2. Nie widzę nigdzie wywołania instrukcji odświeżających tablicę,metoda fireTableDataChanged jest wywoływana np. w metodzie setValue ale metoda setValue nie jest nigdzie wywoływana.

1 użytkowników online, w tym zalogowanych: 0, gości: 1