Erro na tabela

4 respostas
E

Boa noite Galera, estou com o seguinte problema na tabela

Exception occurred during event dispatching:
java.lang.ClassCastException: javax.swing.table.DefaultTableModel cannot be cast to views.JanelaDevolucao$TableModelLocacao1
	at views.JanelaDevolucao.buscarAction(JanelaDevolucao.java:426)
	at views.JanelaDevolucao.access$3(JanelaDevolucao.java:425)
	at views.JanelaDevolucao$1.focusLost(JanelaDevolucao.java:373)
	at java.awt.AWTEventMulticaster.focusLost(Unknown Source)
	at java.awt.Component.processFocusEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.KeyboardFocusManager.dispatchAndCatchException(Unknown Source)
	at java.awt.KeyboardFocusManager.processCurrentLightweightRequests(Unknown Source)
	at java.awt.KeyboardFocusManager$1.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.Dialog$1.run(Unknown Source)
	at java.awt.Dialog$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.awt.Dialog.show(Unknown Source)
	at java.awt.Component.show(Unknown Source)
	at java.awt.Component.setVisible(Unknown Source)
	at java.awt.Window.setVisible(Unknown Source)
	at java.awt.Dialog.setVisible(Unknown Source)
	at views.MenuOpcoes$7.actionPerformed(MenuOpcoes.java:279)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.AbstractButton.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.menuDragMouseReleased(Unknown Source)
	at javax.swing.JMenuItem.fireMenuDragMouseReleased(Unknown Source)
	at javax.swing.JMenuItem.processMenuDragMouseEvent(Unknown Source)
	at javax.swing.JMenuItem.processMouseEvent(Unknown Source)
	at javax.swing.MenuSelectionManager.processMouseEvent(Unknown Source)
	at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.eventDispatched(Unknown Source)
	at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Unknown Source)
	at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Unknown Source)
	at java.awt.Toolkit.notifyAWTEventListeners(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
Exception occurred during event dispatching:
java.lang.ClassCastException: javax.swing.table.DefaultTableModel cannot be cast to views.JanelaDevolucao$TableModelLocacao1
	at views.JanelaDevolucao.buscarAction(JanelaDevolucao.java:426)
	at views.JanelaDevolucao.access$3(JanelaDevolucao.java:425)
	at views.JanelaDevolucao$1.focusLost(JanelaDevolucao.java:373)
	at java.awt.AWTEventMulticaster.focusLost(Unknown Source)
	at java.awt.Component.processFocusEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.SentEvent.dispatch(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager$DefaultKeyboardFocusManagerSentEvent.dispatch(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.sendMessage(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.SequencedEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.Dialog$1.run(Unknown Source)
	at java.awt.Dialog$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.awt.Dialog.show(Unknown Source)
	at java.awt.Component.show(Unknown Source)
	at java.awt.Component.setVisible(Unknown Source)
	at java.awt.Window.setVisible(Unknown Source)
	at java.awt.Dialog.setVisible(Unknown Source)
	at views.MenuOpcoes$7.actionPerformed(MenuOpcoes.java:279)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.AbstractButton.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.menuDragMouseReleased(Unknown Source)
	at javax.swing.JMenuItem.fireMenuDragMouseReleased(Unknown Source)
	at javax.swing.JMenuItem.processMenuDragMouseEvent(Unknown Source)
	at javax.swing.JMenuItem.processMouseEvent(Unknown Source)
	at javax.swing.MenuSelectionManager.processMouseEvent(Unknown Source)
	at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.eventDispatched(Unknown Source)
	at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Unknown Source)
	at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Unknown Source)
	at java.awt.Toolkit.notifyAWTEventListeners(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

O que pode ser?

Segue o código.

package views;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

import Beans.filmes_locados;
import Beans.locacao_locadora;
import Buscas.BuscaFilmeLocado;

import com.swtdesigner.SwingResourceManager;

@SuppressWarnings("serial")
public class JanelaDevolucao extends JDialog {

	private JScrollPane scrollPane_1;
	private JTable tabela;
	private JButton consulta_filme;
	private JTextField codigo_filme;
	JButton consultar;
	JFormattedTextField data;

	private static EntityManagerFactory f = null;

	class TableModelLocacao1 extends AbstractTableModel {
		private static final long serialVersionUID = 1L;

		private List<filmes_locados> filmes_locados;
		private String[] colunas = { "Código filme Locado", "Código Filme", "Título" };
		private int colSize[] = { 50, 80, 210 };

		public int getColumnCount() {
			return colunas.length;
		}

		public int getRowCount() {
			if (filmes_locados == null) {
				return 0;
			}

			return filmes_locados.size();
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			filmes_locados filme = filmes_locados.get(rowIndex);

			switch (columnIndex) {
			case 0:
				return filme.getCod_filme_locado();
			case 1:
				return filme.getCod_filme();
			case 2:
				return filme.getTitulo();
			default:
				return null;
			}
		}

		public Class<?> getColumnClass(int columnIndex) {
			if (columnIndex == 2) {
				if (filmes_locados != null && !filmes_locados.isEmpty()) {
					return getValueAt(0, columnIndex).getClass();
				}
			}

			return super.getColumnClass(columnIndex);
		}

		@Override
		public String getColumnName(int column) {
			return colunas[column];
		}

		public void setColumnsWidth() {
			for (int i = 0; i < colSize.length; i++) {
				TableColumn tableColumn = tabela.getColumnModel().getColumn(i);
				tableColumn.setPreferredWidth(colSize[i]);
				tableColumn.setMinWidth(colSize[i]);
			}
		}

		public void addItem(filmes_locados filme) {
			if (filmes_locados == null) {
				filmes_locados = new ArrayList<filmes_locados>();
			}

			filmes_locados.add(filme);
		}

		public filmes_locados getItem(int index) {
			return filmes_locados.get(index);
		}

		public void setItems(List<filmes_locados> list) {
			filmes_locados = list;
		}

		public List<filmes_locados> getItems() {
			return filmes_locados;
		}

		public void removeItem(int index) {
			if (filmes_locados != null) {
				filmes_locados.remove(index);
			}
		}

		public void removeItem(Object o) {
			if (filmes_locados != null) {
				filmes_locados.remove(o);
			}
		}

		public void removeAll() {
			filmes_locados = null;
		}
	}

	public JanelaDevolucao(EntityManagerFactory f1) {

		this.setTitle("DEVOLUÇÃO");
		getContentPane().setForeground(new Color(30, 144, 255));
		getContentPane().setLayout(null);
		Container tela = getContentPane();
		setLayout(null);

		f = f1;

		tela.setBackground(new Color(181, 181, 181));
		consultar = new JButton("Consultar");
		consultar.setBounds(0, 0, 0, 0);
		tela.add(consultar);

		//	   	 DESABILITAM-SE ALGUNS BOTÕES 
		consultar.setEnabled(true);

		setSize(733, 407);

		setLocationRelativeTo(null);// CENTRALIZA A JANELA 

		final JLabel label = new JLabel();
		label.setFont(new Font("", Font.BOLD, 12));
		label.setText("Pesquisa Locação");
		label.setBounds(10, 0, 105, 14);
		getContentPane().add(label);

		final JSeparator separator = new JSeparator();
		separator.setBounds(121, 6, 589, 8);
		getContentPane().add(separator);

		final JSeparator separator_1 = new JSeparator();
		separator_1.setOrientation(SwingConstants.VERTICAL);
		separator_1.setBounds(10, 10, 11, 66);
		getContentPane().add(separator_1);

		final JSeparator separator_1_1 = new JSeparator();
		separator_1_1.setOrientation(SwingConstants.VERTICAL);
		separator_1_1.setBounds(709, 6, 11, 76);
		getContentPane().add(separator_1_1);

		final JLabel label_1 = new JLabel();
		label_1.setText("Código do filme");
		label_1.setBounds(22, 36, 73, 14);
		getContentPane().add(label_1);

		final JSeparator separator_3 = new JSeparator();
		separator_3.setBounds(10, 78, 700, 20);
		getContentPane().add(separator_3);

		final JLabel label_2 = new JLabel();
		label_2.setFont(new Font("Dialog", Font.BOLD, 12));
		label_2.setText("Pesquisa Locação");
		label_2.setBounds(25, 0, 105, 14);
		separator_3.add(label_2);

		final JSeparator separator_2_1 = new JSeparator();
		separator_2_1.setBounds(0, 1, 19, 14);
		separator_3.add(separator_2_1);

		final JSeparator separator_2_2 = new JSeparator();
		separator_2_2.setBounds(0, 10, 19, 14);
		separator_3.add(separator_2_2);

		final JSeparator separator_4 = new JSeparator();
		separator_4.setBounds(111, 10, 565, 8);
		separator_3.add(separator_4);

		final JSeparator separator_5 = new JSeparator();
		separator_5.setBounds(109, 10, 591, 8);
		separator_3.add(separator_5);

		final JSeparator separator_2_3 = new JSeparator();
		separator_2_3.setBounds(0, 0, 19, 14);
		separator_3.add(separator_2_3);

		final JSeparator separator_2_4 = new JSeparator();
		separator_2_4.setBounds(0, 10, 19, 14);
		separator_3.add(separator_2_4);

		final JSeparator separator_6 = new JSeparator();
		separator_6.setBounds(113, 10, 565, 8);
		separator_3.add(separator_6);

		final JSeparator separator_7 = new JSeparator();
		separator_7.setBounds(115, 10, 585, 8);
		separator_3.add(separator_7);

		final JSeparator separator_2_5 = new JSeparator();
		separator_2_5.setBounds(0, 10, 19, 14);
		separator_3.add(separator_2_5);

		final JSeparator separator_8 = new JSeparator();
		separator_8.setBounds(113, 10, 587, 8);
		separator_3.add(separator_8);

		final JSeparator separator_2_6 = new JSeparator();
		separator_2_6.setBounds(0, 10, 19, 14);
		separator_3.add(separator_2_6);

		final JSeparator separator_9 = new JSeparator();
		separator_9.setBounds(115, 10, 585, 8);
		separator_3.add(separator_9);

		final JSeparator separator_2_7 = new JSeparator();
		separator_2_7.setBounds(0, 10, 19, 14);
		separator_3.add(separator_2_7);

		final JSeparator separator_10 = new JSeparator();
		separator_10.setBounds(113, 10, 587, 8);
		separator_3.add(separator_10);

		final JSeparator separator_2_8 = new JSeparator();
		separator_2_8.setBounds(0, 10, 19, 14);
		separator_3.add(separator_2_8);

		codigo_filme = new JTextField();
		codigo_filme.setBounds(114, 33, 88, 20);
		getContentPane().add(codigo_filme);

		final JLabel label_3 = new JLabel();
		label_3.setFont(new Font("Dialog", Font.BOLD, 12));
		label_3.setText("Dados locação");
		label_3.setBounds(10, 82, 105, 14);
		getContentPane().add(label_3);

		final JSeparator separator_1_2 = new JSeparator();
		separator_1_2.setOrientation(SwingConstants.VERTICAL);
		separator_1_2.setBounds(10, 93, 11, 66);
		getContentPane().add(separator_1_2);

		final JSeparator separator_3_1 = new JSeparator();
		separator_3_1.setBounds(10, 157, 700, 20);
		getContentPane().add(separator_3_1);

		final JSeparator separator_1_1_1 = new JSeparator();
		separator_1_1_1.setOrientation(SwingConstants.VERTICAL);
		separator_1_1_1.setBounds(709, 88, 11, 71);
		getContentPane().add(separator_1_1_1);

		final JLabel label_4 = new JLabel();
		label_4.setText("Cód. locação");
		label_4.setBounds(23, 111, 88, 14);
		getContentPane().add(label_4);

		final JLabel cod_locacao = new JLabel();
		cod_locacao.setBounds(102, 111, 54, 14);
		getContentPane().add(cod_locacao);

		final JLabel clienteLabel = new JLabel();
		clienteLabel.setText("Cliente");
		clienteLabel.setBounds(51, 131, 54, 14);
		getContentPane().add(clienteLabel);

		final JLabel label_5 = new JLabel();
		label_5.setBounds(102, 131, 100, 14);
		getContentPane().add(label_5);

		final JLabel filmeLabel = new JLabel();
		filmeLabel.setText("Filme");
		filmeLabel.setBounds(243, 111, 59, 14);
		getContentPane().add(filmeLabel);

		final JLabel label_6 = new JLabel();
		label_6.setBounds(308, 111, 88, 14);
		getContentPane().add(label_6);

		final JLabel label_7 = new JLabel();
		label_7.setText("Data Locação");
		label_7.setBounds(240, 131, 73, 14);
		getContentPane().add(label_7);

		final JLabel label_8 = new JLabel();
		label_8.setBounds(308, 131, 88, 14);
		getContentPane().add(label_8);

		final JLabel tituloLabel = new JLabel();
		tituloLabel.setText("Título");
		tituloLabel.setBounds(469, 111, 54, 14);
		getContentPane().add(tituloLabel);

		final JLabel label_9 = new JLabel();
		label_9.setBounds(529, 111, 88, 14);
		getContentPane().add(label_9);

		final JButton devolverButton = new JButton();
		devolverButton.setText("Devolver");
		devolverButton.setBounds(10, 172, 93, 23);
		getContentPane().add(devolverButton);

		final JButton devolverTodasButton = new JButton();
		devolverTodasButton.setText("Devolver Todas");
		devolverTodasButton.setBounds(133, 172, 117, 23);
		getContentPane().add(devolverTodasButton);

		final JButton concluirButton = new JButton();
		concluirButton.setIcon(SwingResourceManager.getIcon(JanelaDevolucao.class, "/picture/OKShield-32.png"));
		concluirButton.setText("Concluir");
		concluirButton.setBounds(487, 343, 117, 23);
		getContentPane().add(concluirButton);

		final JButton sairButton = new JButton();
		sairButton.setIcon(SwingResourceManager.getIcon(JanelaDevolucao.class, "/picture/porta.png"));
		sairButton.setText("Sair");
		sairButton.setBounds(633, 343, 87, 23);
		getContentPane().add(sairButton);
		//tabela

		//	 Ações Botões

		codigo_filme.addFocusListener(new FocusListener() {
			public void focusLost(FocusEvent arg0) {

				if (!codigo_filme.getText().trim().equals("")) {

					EntityManager me = f.createEntityManager();

					locacao_locadora film = new locacao_locadora();

					//
					// <<<<<< Localizando o Registro no Banco = FIND >>>>>
					//
					film = me.find(locacao_locadora.class, Integer.parseInt(codigo_filme.getText().trim()));

					if (film != null) {

						cod_locacao.setText(String.valueOf(film.getCod_loc()));
						label_5.setText(String.valueOf(film.getCod_cli()));
						label_8.setText(film.getData_loc());

						buscarAction();
					}

				} else {
					JOptionPane.showMessageDialog(null, "Título não encontrado!");

					codigo_filme.requestFocus();

				}
			}

			public void focusGained(final FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

		} // fim do new ActionListener
				);

		consulta_filme = new JButton();
		consulta_filme.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent arg0) {

				BuscaFilmeLocado filme = new BuscaFilmeLocado(f);
				filme.setModal(true);
				filme.setVisible(true);

				locacao_locadora film = filme.getFilmeLocadoSelecionado();

				if (film != null) {
					codigo_filme.setText("");

					codigo_filme.setText(String.valueOf(film.getCod_filme()));

					codigo_filme.requestFocus();

				}
			}
		});
		consulta_filme.setIcon(SwingResourceManager.getIcon(JanelaDevolucao.class, "/picture/search_magnifier_32.jpg.png"));
		consulta_filme.setBounds(222, 32, 28, 23);
		getContentPane().add(consulta_filme);

		scrollPane_1 = new JScrollPane();
		scrollPane_1.setBackground(Color.WHITE);
		scrollPane_1.setBounds(51, 215, 427, 105);
		getContentPane().add(scrollPane_1);

		tabela = new JTable();
		scrollPane_1.setViewportView(tabela);
	}

	private void buscarAction() {
		TableModelLocacao1 modelLocacao1 = (TableModelLocacao1) tabela.getModel();

		List<filmes_locados> filmes_locados;

		filmes_locados = buscarFilmePorParametros(Integer.parseInt(codigo_filme.getText()));

		if (filmes_locados != null && !filmes_locados.isEmpty()) {

			for (filmes_locados filmes_locados1 : filmes_locados) {
				modelLocacao1.addItem(filmes_locados1);

			}

			tabela.setRowSelectionInterval(0, 0);
			tabela.requestFocus();
		} else {
			JOptionPane.showMessageDialog(null, "Cai no else!");

		}

		double valor_final = 0;
		// double desconto= Double.parseDuble(desconto.getText());
		//     valor_final= valor_final+valortotal;

	}

	public void focusGained(final FocusEvent arg0) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("unchecked")
	public List<filmes_locados> buscarFilmePorParametros(Integer cod_filme) {

		List<filmes_locados> filme = new ArrayList<filmes_locados>();

		StringBuilder jpql = new StringBuilder("SELECT p FROM filmes_locados p WHERE p.cod_filme IS NOT NULL");//("SELECT p FROM cliente_locadora p WHERE p.cod_cli = ? OR p.nome_cli LIKE ?" );

		if (cod_filme != null && !cod_filme.equals("")) {
			jpql.append(" AND p.cod_filme = " + cod_filme);

		}

		else {
			JOptionPane.showMessageDialog(null, "Filme não locado!");

		}

		try {
			EntityManager em = f.createEntityManager();
			Query query = em.createQuery(jpql.toString());
			filme = query.getResultList();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Cai no erro!");
			e.printStackTrace();
		}

		return filme;

	}

	public static void main(String args[]) {

		JanelaDevolucao app = new JanelaDevolucao(f);

		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}

4 Respostas

M

TableModelLocacao não é filho de DefaultTableModel. Verifique se o pai dela não é AbstractTableModel.

E

cara, desculpa sou meio leigo como faço isso

Marky.Vasconcelos

Voce não setou o TableModel da tabela.

TableModelLocacao modelo = //instancia como quiser
tabela = new JTable(modelo);

Depois disso voce conseguira fazer o cast.

E

cara agora eu dei o set. mas deu esse erro o que pode ser?

Exception occurred during event dispatching:   
java.lang.NullPointerException   
    at views.JanelaDevolucao.buscarAction(JanelaDevolucao.java:466)   
    at views.JanelaDevolucao.access$3(JanelaDevolucao.java:465)   
    at views.JanelaDevolucao$3.actionPerformed(JanelaDevolucao.java:457)   
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)   
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)   
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)   
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)   
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)   
    at java.awt.Component.processMouseEvent(Unknown Source)   
    at javax.swing.JComponent.processMouseEvent(Unknown Source)   
    at java.awt.Component.processEvent(Unknown Source)   
    at java.awt.Container.processEvent(Unknown Source)   
    at java.awt.Component.dispatchEventImpl(Unknown Source)   
    at java.awt.Container.dispatchEventImpl(Unknown Source)   
    at java.awt.Component.dispatchEvent(Unknown Source)   
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)   
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)   
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)   
    at java.awt.Container.dispatchEventImpl(Unknown Source)   
    at java.awt.Window.dispatchEventImpl(Unknown Source)   
    at java.awt.Component.dispatchEvent(Unknown Source)   
    at java.awt.EventQueue.dispatchEvent(Unknown Source)   
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)   
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)   
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)   
    at java.awt.Dialog$1.run(Unknown Source)   
    at java.awt.Dialog$3.run(Unknown Source)   
    at java.security.AccessController.doPrivileged(Native Method)   
    at java.awt.Dialog.show(Unknown Source)   
    at java.awt.Component.show(Unknown Source)   
    at java.awt.Component.setVisible(Unknown Source)   
    at java.awt.Window.setVisible(Unknown Source)   
    at java.awt.Dialog.setVisible(Unknown Source)   
    at views.MenuOpcoes$7.actionPerformed(MenuOpcoes.java:279)   
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)   
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)   
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)   
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)   
    at javax.swing.AbstractButton.doClick(Unknown Source)   
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)   
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)   
    at java.awt.Component.processMouseEvent(Unknown Source)   
    at javax.swing.JComponent.processMouseEvent(Unknown Source)   
    at java.awt.Component.processEvent(Unknown Source)   
    at java.awt.Container.processEvent(Unknown Source)   
    at java.awt.Component.dispatchEventImpl(Unknown Source)   
    at java.awt.Container.dispatchEventImpl(Unknown Source)   
    at java.awt.Component.dispatchEvent(Unknown Source)   
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)   
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)   
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)   
    at java.awt.Container.dispatchEventImpl(Unknown Source)   
    at java.awt.Window.dispatchEventImpl(Unknown Source)   
    at java.awt.Component.dispatchEvent(Unknown Source)   
    at java.awt.EventQueue.dispatchEvent(Unknown Source)   
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)   
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)   
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)   
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)   
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)   
    at java.awt.EventDispatchThread.run(Unknown Source)  

Exception occurred during event dispatching:
java.lang.NullPointerException
	at views.JanelaDevolucao.buscarAction(JanelaDevolucao.java:466)
	at views.JanelaDevolucao.access$3(JanelaDevolucao.java:465)
	at views.JanelaDevolucao$3.actionPerformed(JanelaDevolucao.java:457)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.Dialog$1.run(Unknown Source)
	at java.awt.Dialog$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.awt.Dialog.show(Unknown Source)
	at java.awt.Component.show(Unknown Source)
	at java.awt.Component.setVisible(Unknown Source)
	at java.awt.Window.setVisible(Unknown Source)
	at java.awt.Dialog.setVisible(Unknown Source)
	at views.MenuOpcoes$7.actionPerformed(MenuOpcoes.java:279)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.AbstractButton.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

Segue o código

package views;   
  
import java.awt.Color;   
import java.awt.Container;   
import java.awt.Font;   
import java.awt.event.ActionEvent;   
import java.awt.event.ActionListener;   
import java.awt.event.FocusEvent;   
import java.awt.event.FocusListener;   
import java.text.SimpleDateFormat;   
import java.util.ArrayList;   
import java.util.List;   
  
import javax.persistence.EntityManager;   
import javax.persistence.EntityManagerFactory;   
import javax.persistence.Query;   
import javax.swing.JButton;   
import javax.swing.JDialog;   
import javax.swing.JFormattedTextField;   
import javax.swing.JLabel;   
import javax.swing.JOptionPane;   
import javax.swing.JScrollPane;   
import javax.swing.JSeparator;   
import javax.swing.JTable;   
import javax.swing.JTextField;   
import javax.swing.SwingConstants;   
import javax.swing.table.AbstractTableModel;   
import javax.swing.table.TableColumn;   
  
import Beans.filmes_locados;   
import Beans.locacao_locadora;   
import Buscas.BuscaFilmeLocado;   
  
import com.swtdesigner.SwingResourceManager;   
  
@SuppressWarnings("serial")   
public class JanelaDevolucao extends JDialog {   
  
    class Tabela_1TableModel extends AbstractTableModel {   
        private static final long serialVersionUID = 1L;   
  
        private List<filmes_locados> filmes_locados;   
        private String[] colunas = { "Código", "Titulo", "Genero", "Valor" };   
        private int colSize[] = { 50, 80, 210 };   
  
        public int getColumnCount() {   
            return colunas.length;   
        }   
  
        public int getRowCount() {   
            if (filmes_locados == null) {   
                return 0;   
            }   
  
            return filmes_locados.size();   
        }   
  
        public Object getValueAt(int rowIndex, int columnIndex) {   
            filmes_locados filme = filmes_locados.get(rowIndex);   
  
            switch (columnIndex) {   
            case 0:   
                return filme.getCod_filme();   
            case 1:   
                return filme.getCod_filme_locado();   
            case 2:   
                return filme.getTitulo();   
            case 3:   
                return filme.getCod_cli();   
            default:   
                return null;   
            }   
        }   
  
        public Class<?> getColumnClass(int columnIndex) {   
            if (columnIndex == 2) {   
                if (filmes_locados != null && !filmes_locados.isEmpty()) {   
                    return getValueAt(0, columnIndex).getClass();   
                }   
            }   
  
            return super.getColumnClass(columnIndex);   
        }   
  
        @Override   
        public String getColumnName(int column) {   
            return colunas[column];   
        }   
  
        public void setColumnsWidth() {   
            for (int i = 0; i < colSize.length; i++) {   
                TableColumn tableColumn = tabela.getColumnModel().getColumn(i);   
                tableColumn.setPreferredWidth(colSize[i]);   
                tableColumn.setMinWidth(colSize[i]);   
            }   
        }   
  
        public void addItem(filmes_locados filme) {   
            if (filmes_locados == null) {   
                filmes_locados = new ArrayList<filmes_locados>();   
            }   
  
            filmes_locados.add(filme);   
  
            tabela.updateUI();   
        }   
  
        public filmes_locados getItem(int index) {   
            return filmes_locados.get(index);   
        }   
  
        public void setItems(List<filmes_locados> list) {   
            filmes_locados = list;   
  
            tabela.updateUI();   
        }   
  
        public List<filmes_locados> getItems() {   
            return filmes_locados;   
        }   
  
        public void removeItem(int index) {   
            if (filmes_locados != null) {   
                filmes_locados.remove(index);   
            }   
  
            tabela.updateUI();   
        }   
  
        public void removeItem(Object o) {   
            if (filmes_locados != null) {   
                filmes_locados.remove(o);   
            }   
  
            tabela.updateUI();   
        }   
  
        public void removeAll() {   
            filmes_locados = null;   
  
            tabela.updateUI();   
        }   
    }   
  
    private JTable tabela_1;   
    private JScrollPane scrollPane_2;   
    private JButton pagamento;   
    private JButton consulta_filme;   
    private JTextField valor_total;   
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");   
    JLabel rotulo1, rotulo2, rotulo3, rotulo4, rotulo5, rotulo9, rotulo10, rotulo11;   
    JLabel rotulo7, rotulo8;   
    JButton cadastrar, fechar, alterar, excluir, consultar, limpar;   
    JTextField cod_locacao, cod_cli, cod_filme, data_locacao, titulo;   
    JTextField data_devolucao, valorf, quantidade;   
    JFormattedTextField data;   
    private JScrollPane scrollPane;   
    private JTable tabela;   
    private JButton visualizarButton;   
    private JLabel nomeLabel;   
    private JLabel codigoLabel;   
    private JTextField nomeField;   
    private JTextField codigoField;   
    private JTextField codigo_filme;   
  
    double valortotal = 0;   
  
    private static EntityManagerFactory f = null;   
  
    public JanelaDevolucao(EntityManagerFactory f1) {   
  
        this.setTitle("DEVOLUÇÃO");   
        getContentPane().setForeground(new Color(30, 144, 255));   
        getContentPane().setLayout(null);   
        Container tela = getContentPane();   
        setLayout(null);   
  
        f = f1;   
  
        tela.setBackground(new Color(181, 181, 181));   
        consultar = new JButton("Consultar");   
        consultar.setBounds(0, 0, 0, 0);   
        tela.add(consultar);   
  
        //       DESABILITAM-SE ALGUNS BOTÕES   
        consultar.setEnabled(true);   
  
        setSize(733, 407);   
  
        setLocationRelativeTo(null);// CENTRALIZA A JANELA   
  
        final JLabel label = new JLabel();   
        label.setFont(new Font("", Font.BOLD, 12));   
        label.setText("Pesquisa Locação");   
        label.setBounds(10, 0, 105, 14);   
        getContentPane().add(label);   
  
        final JSeparator separator = new JSeparator();   
        separator.setBounds(121, 6, 589, 8);   
        getContentPane().add(separator);   
  
        final JSeparator separator_1 = new JSeparator();   
        separator_1.setOrientation(SwingConstants.VERTICAL);   
        separator_1.setBounds(10, 10, 11, 66);   
        getContentPane().add(separator_1);   
  
        final JSeparator separator_1_1 = new JSeparator();   
        separator_1_1.setOrientation(SwingConstants.VERTICAL);   
        separator_1_1.setBounds(709, 6, 11, 76);   
        getContentPane().add(separator_1_1);   
  
        final JLabel label_1 = new JLabel();   
        label_1.setText("Código do filme");   
        label_1.setBounds(22, 36, 73, 14);   
        getContentPane().add(label_1);   
  
        final JSeparator separator_3 = new JSeparator();   
        separator_3.setBounds(10, 78, 700, 20);   
        getContentPane().add(separator_3);   
  
        final JLabel label_2 = new JLabel();   
        label_2.setFont(new Font("Dialog", Font.BOLD, 12));   
        label_2.setText("Pesquisa Locação");   
        label_2.setBounds(25, 0, 105, 14);   
        separator_3.add(label_2);   
  
        final JSeparator separator_2_1 = new JSeparator();   
        separator_2_1.setBounds(0, 1, 19, 14);   
        separator_3.add(separator_2_1);   
  
        final JSeparator separator_2_2 = new JSeparator();   
        separator_2_2.setBounds(0, 10, 19, 14);   
        separator_3.add(separator_2_2);   
  
        final JSeparator separator_4 = new JSeparator();   
        separator_4.setBounds(111, 10, 565, 8);   
        separator_3.add(separator_4);   
  
        final JSeparator separator_5 = new JSeparator();   
        separator_5.setBounds(109, 10, 591, 8);   
        separator_3.add(separator_5);   
  
        final JSeparator separator_2_3 = new JSeparator();   
        separator_2_3.setBounds(0, 0, 19, 14);   
        separator_3.add(separator_2_3);   
  
        final JSeparator separator_2_4 = new JSeparator();   
        separator_2_4.setBounds(0, 10, 19, 14);   
        separator_3.add(separator_2_4);   
  
        final JSeparator separator_6 = new JSeparator();   
        separator_6.setBounds(113, 10, 565, 8);   
        separator_3.add(separator_6);   
  
        final JSeparator separator_7 = new JSeparator();   
        separator_7.setBounds(115, 10, 585, 8);   
        separator_3.add(separator_7);   
  
        final JSeparator separator_2_5 = new JSeparator();   
        separator_2_5.setBounds(0, 10, 19, 14);   
        separator_3.add(separator_2_5);   
  
        final JSeparator separator_8 = new JSeparator();   
        separator_8.setBounds(113, 10, 587, 8);   
        separator_3.add(separator_8);   
  
        final JSeparator separator_2_6 = new JSeparator();   
        separator_2_6.setBounds(0, 10, 19, 14);   
        separator_3.add(separator_2_6);   
  
        final JSeparator separator_9 = new JSeparator();   
        separator_9.setBounds(115, 10, 585, 8);   
        separator_3.add(separator_9);   
  
        final JSeparator separator_2_7 = new JSeparator();   
        separator_2_7.setBounds(0, 10, 19, 14);   
        separator_3.add(separator_2_7);   
  
        final JSeparator separator_10 = new JSeparator();   
        separator_10.setBounds(113, 10, 587, 8);   
        separator_3.add(separator_10);   
  
        final JSeparator separator_2_8 = new JSeparator();   
        separator_2_8.setBounds(0, 10, 19, 14);   
        separator_3.add(separator_2_8);   
  
        codigo_filme = new JTextField();   
        codigo_filme.setBounds(114, 33, 88, 20);   
        getContentPane().add(codigo_filme);   
  
        final JLabel label_3 = new JLabel();   
        label_3.setFont(new Font("Dialog", Font.BOLD, 12));   
        label_3.setText("Dados locação");   
        label_3.setBounds(10, 82, 105, 14);   
        getContentPane().add(label_3);   
  
        final JSeparator separator_1_2 = new JSeparator();   
        separator_1_2.setOrientation(SwingConstants.VERTICAL);   
        separator_1_2.setBounds(10, 93, 11, 66);   
        getContentPane().add(separator_1_2);   
  
        final JSeparator separator_3_1 = new JSeparator();   
        separator_3_1.setBounds(10, 157, 700, 20);   
        getContentPane().add(separator_3_1);   
  
        final JSeparator separator_1_1_1 = new JSeparator();   
        separator_1_1_1.setOrientation(SwingConstants.VERTICAL);   
        separator_1_1_1.setBounds(709, 88, 11, 71);   
        getContentPane().add(separator_1_1_1);   
  
        final JLabel label_4 = new JLabel();   
        label_4.setText("Cód. locação");   
        label_4.setBounds(23, 111, 88, 14);   
        getContentPane().add(label_4);   
  
        final JLabel cod_locacao = new JLabel();   
        cod_locacao.setBounds(102, 111, 54, 14);   
        getContentPane().add(cod_locacao);   
  
        final JLabel clienteLabel = new JLabel();   
        clienteLabel.setText("Cliente");   
        clienteLabel.setBounds(51, 131, 54, 14);   
        getContentPane().add(clienteLabel);   
  
        final JLabel label_5 = new JLabel();   
        label_5.setBounds(102, 131, 100, 14);   
        getContentPane().add(label_5);   
  
        final JLabel filmeLabel = new JLabel();   
        filmeLabel.setText("Filme");   
        filmeLabel.setBounds(243, 111, 59, 14);   
        getContentPane().add(filmeLabel);   
  
        final JLabel label_6 = new JLabel();   
        label_6.setBounds(308, 111, 88, 14);   
        getContentPane().add(label_6);   
  
        final JLabel label_7 = new JLabel();   
        label_7.setText("Data Locação");   
        label_7.setBounds(240, 131, 73, 14);   
        getContentPane().add(label_7);   
  
        final JLabel label_8 = new JLabel();   
        label_8.setBounds(308, 131, 88, 14);   
        getContentPane().add(label_8);   
  
        final JLabel tituloLabel = new JLabel();   
        tituloLabel.setText("Título");   
        tituloLabel.setBounds(469, 111, 54, 14);   
        getContentPane().add(tituloLabel);   
  
        final JLabel label_9 = new JLabel();   
        label_9.setBounds(529, 111, 88, 14);   
        getContentPane().add(label_9);   
  
        final JButton devolverButton = new JButton();   
        devolverButton.setText("Devolver");   
        devolverButton.setBounds(10, 172, 93, 23);   
        getContentPane().add(devolverButton);   
  
        final JButton devolverTodasButton = new JButton();   
        devolverTodasButton.setText("Devolver Todas");   
        devolverTodasButton.setBounds(133, 172, 117, 23);   
        getContentPane().add(devolverTodasButton);   
  
        final JButton concluirButton = new JButton();   
        concluirButton.setIcon(SwingResourceManager.getIcon(JanelaDevolucao.class, "/picture/OKShield-32.png"));   
        concluirButton.setText("Concluir");   
        concluirButton.setBounds(487, 343, 117, 23);   
        getContentPane().add(concluirButton);   
  
        final JButton sairButton = new JButton();   
        sairButton.setIcon(SwingResourceManager.getIcon(JanelaDevolucao.class, "/picture/porta.png"));   
        sairButton.setText("Sair");   
        sairButton.setBounds(633, 343, 87, 23);   
        getContentPane().add(sairButton);   
        //tabela   
  
        //   Ações Botões   
  
        codigo_filme.addFocusListener(new FocusListener() {   
            public void focusLost(FocusEvent arg0) {   
  
                if (!codigo_filme.getText().trim().equals("")) {   
  
                    EntityManager me = f.createEntityManager();   
  
                    locacao_locadora film = new locacao_locadora();   
  
                    //   
                    // <<<<<< Localizando o Registro no Banco = FIND >>>>>   
                    //   
                    film = me.find(locacao_locadora.class, Integer.parseInt(codigo_filme.getText().trim()));   
  
                    if (film != null) {   
  
                        cod_locacao.setText(String.valueOf(film.getCod_loc()));   
                        label_5.setText(String.valueOf(film.getCod_cli()));   
                        label_8.setText(film.getData_loc());   
  
                    }   
  
                } else {   
                    JOptionPane.showMessageDialog(null, "Título não encontrado!");   
  
                    codigo_filme.requestFocus();   
  
                }   
            }   
  
            public void focusGained(final FocusEvent arg0) {   
                // TODO Auto-generated method stub   
  
            }   
  
        } // fim do new ActionListener   
                );   
  
        consulta_filme = new JButton();   
        consulta_filme.addActionListener(new ActionListener() {   
            public void actionPerformed(final ActionEvent arg0) {   
  
                BuscaFilmeLocado filme = new BuscaFilmeLocado(f);   
                filme.setModal(true);   
                filme.setVisible(true);   
  
                locacao_locadora film = filme.getFilmeLocadoSelecionado();   
  
                if (film != null) {   
                    codigo_filme.setText("");   
  
                    codigo_filme.setText(String.valueOf(film.getCod_filme()));   
  
                    codigo_filme.requestFocus();   
  
                }   
            }   
        });   
        consulta_filme.setIcon(SwingResourceManager.getIcon(JanelaDevolucao.class, "/picture/search_magnifier_32.jpg.png"));   
        consulta_filme.setBounds(215, 33, 28, 23);   
        getContentPane().add(consulta_filme);   
  
        scrollPane_2 = new JScrollPane();   
        scrollPane_2.setBackground(Color.WHITE);   
        scrollPane_2.setBounds(51, 223, 427, 105);   
        getContentPane().add(scrollPane_2);   
  
        tabela_1 = new JTable();   
        tabela_1.setModel(new Tabela_1TableModel());   
        scrollPane_2.setViewportView(tabela_1);   
        scrollPane_2.setViewportView(tabela_1);   
  
        visualizarButton = new JButton();   
        visualizarButton.setIcon(SwingResourceManager.getIcon(JanelaLocacao.class, "/picture/OKShield-32.png"));   
        visualizarButton.addActionListener(new ActionListener() {   
            public void actionPerformed(final ActionEvent e) {   
                buscarAction();   
            }   
        });   
        visualizarButton.setBounds(246, 32, 31, 23);   
        getContentPane().add(visualizarButton);   
  
    }   
  
    private void buscarAction() {   
  
        Tabela_1TableModel modelLocacao1 = (Tabela_1TableModel) tabela.getModel();   
  
        List<filmes_locados> filmes_locados;   
        JOptionPane.showMessageDialog(null, "ENTREI!");   
  
        filmes_locados = buscarFilmePorParametros(Integer.parseInt(codigo_filme.getText()));   
  
        if (filmes_locados != null && !filmes_locados.isEmpty()) {   
  
            for (filmes_locados filmes_locados1 : filmes_locados) {   
                modelLocacao1.addItem(filmes_locados1);   
  
            }   
  
            tabela.setRowSelectionInterval(0, 0);   
            tabela.requestFocus();   
        } else {   
            JOptionPane.showMessageDialog(null, "Cai no else!");   
  
        }   
  
        double valor_final = 0;   
        // double desconto= Double.parseDuble(desconto.getText());   
        //     valor_final= valor_final+valortotal;   
  
    }   
  
    public void focusGained(final FocusEvent arg0) {   
        // TODO Auto-generated method stub   
  
    }   
  
    @SuppressWarnings("unchecked")   
    public List<filmes_locados> buscarFilmePorParametros(Integer cod_filme) {   
  
        List<filmes_locados> filme = new ArrayList<filmes_locados>();   
  
        StringBuilder jpql = new StringBuilder("SELECT p FROM filmes_locados p WHERE p.cod_filme IS NOT NULL");//("SELECT p FROM cliente_locadora p WHERE p.cod_cli = ? OR p.nome_cli LIKE ?" );   
  
        if (cod_filme != null && !cod_filme.equals("")) {   
            jpql.append(" AND p.cod_filme = " + cod_filme);   
  
        }   
  
        else {   
            JOptionPane.showMessageDialog(null, "Filme não locado!");   
  
        }   
  
        try {   
            EntityManager em = f.createEntityManager();   
            Query query = em.createQuery(jpql.toString());   
            filme = query.getResultList();   
        } catch (Exception e) {   
            JOptionPane.showMessageDialog(null, "Cai no erro!");   
            e.printStackTrace();   
        }   
  
        return filme;   
  
    }   
  
}
Criado 7 de dezembro de 2009
Ultima resposta 8 de dez. de 2009
Respostas 4
Participantes 3