Interface gráfica.
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
public class InterfacePedidos extends JFrame {
protected static JFormattedTextField tPlaca;
protected static JTextField tPed,tFuncionario,tnFunc;
public static int linha;
JLabel rPlaca;
protected static JTextArea area;
MaskFormatter placaValida;
Connection con;
JPanel meio,componentes0;
String n;
Selecionar s;
public InterfacePedidos() {
super("Pedidos ");
con = new Conexao().getConection();
setContentPane(new Fundo());
Container tela = getContentPane();
tela.setLayout(null);
JPanel titulo = new JPanel();
Font fonte = new Font("Rockwell Condensed",Font.PLAIN,30);
JLabel pedidos = new JLabel("Pedidos");
pedidos.setFont(fonte);
titulo.add(pedidos);
titulo.setOpaque(false);
titulo.setBounds(10,10,200,50);
try {
con.setAutoCommit(false);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
JPanel componentes = new JPanel(new FlowLayout(FlowLayout.LEFT));
componentes0 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel codPed = new JLabel("Código do Pedido: ");
tPed = new JTextField(10);
tPed.setDocument(new TamanhoMaximo(12));
tPed.setDocument(new SoNumericos());
tPed.setEditable(false);
try {
rPlaca = new JLabel("Placa: ");
placaValida = new MaskFormatter("UUU-####");
tPlaca = new JFormattedTextField(placaValida);
tPlaca.setColumns(10);
placaValida.setPlaceholderCharacter('_');
placaValida.setValidCharacters("ABCDEFGHIJKLMNOPRSTUWVXYZ0123456789");
tPlaca.setBorder(new LineBorder(Color.black));
} catch (ParseException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
JCheckBox caixa = new JCheckBox("Código Editável");
caixa.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
Component comp = componentes0.getComponent(2);
if(((JCheckBox) comp).isSelected()) {
tPed.setEditable(true);
}
else
tPed.setEditable(false);
}
});
JButton codigoE = new JButton("Código Editável");
codigoE.setBackground(Color.blue);
codigoE.setForeground(Color.white);
codigoE.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
n = JOptionPane.showInputDialog("Digite o codigo: ");
boolean passou = true;
Selecionar.selecionarCodigoPedido();
try {
if(n != "") {
Integer numero = Integer.parseInt(n);
for(int i = 0; i < Selecionar.codigo1.size(); i++) {
System.out.println("Array: " + Selecionar.codigo1.get(i));
System.out.println("n: ");
if(numero == Selecionar.codigo1.get(i)) {
JOptionPane.showMessageDialog(null,"Já existe esse código");
passou = false;
break;
}
}
if(passou == true) {
tPed.setText(n );
tPlaca.setText("");
tFuncionario.setText("");
tnFunc.setText("");
area.setText("");
}
}
}
catch(java.lang.NumberFormatException e2) {
JOptionPane.showMessageDialog(null,"Valor nulo!");
}
}
});
JLabel cFuncionario = new JLabel("Código do Funcionário: ");
tFuncionario = new JTextField(10);
tFuncionario.setDocument(new TamanhoMaximo(5));
tFuncionario.setDocument(new SoNumericos());
tFuncionario.setBorder(new LineBorder(Color.black));
JLabel nomeFunc = new JLabel("Nome do Funcionário: ");
tnFunc = new JTextField(10);
tnFunc.setEditable(false);
JLabel obs = new JLabel("Observação: ");
area = new JTextArea(10,20);
area.setDocument(new TamanhoMaximo(50));
JScrollPane painelRolagem = new JScrollPane(area);
painelRolagem.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
area.setLineWrap(true);
area.setWrapStyleWord(true);
componentes0.add(codPed);
componentes0.add(tPed);
componentes0.add(caixa);
componentes0.add(codigoE);
componentes0.setBounds(10,60,480,30);
componentes0.setOpaque(false);
tela.add(componentes0);
componentes.add(rPlaca);
componentes.add(tPlaca);
componentes.add(cFuncionario);
componentes.add(tFuncionario);
componentes.add(nomeFunc);
componentes.add(tnFunc);
componentes.add(obs);
componentes.add(painelRolagem);
componentes.setOpaque(false);
componentes.setBounds(10,86,260,280);
JPanel inferior = new JPanel();
JButton botaoAnt = new JButton("<<");
botaoAnt.setBackground(Color.blue);
botaoAnt.setForeground(Color.white);
JButton botao = new JButton("Inserir ");
botao.setBackground(Color.black);
botao.setForeground(Color.white);
JButton botao2 = new JButton("Alterar ");
botao2.setBackground(Color.black);
botao2.setForeground(Color.white);
JButton botao3 = new JButton("Apagar ");
botao3.setBackground(Color.black);
botao3.setForeground(Color.white);
JButton botao4 = new JButton("Selecionar");
botao4.setBackground(Color.black);
botao4.setForeground(Color.white);
JButton botao5 = new JButton("Limpar");
botao5.setBackground(Color.white);
botao5.setForeground(Color.black);
JButton botaoAvan = new JButton(">>");
botaoAvan.setBackground(Color.blue);
botaoAvan.setForeground(Color.WHITE);
inferior.add(botaoAnt);
inferior.add(botao);
inferior.add(botao2);
inferior.add(botao3);
inferior.add(botao4);
inferior.add(botao5);
inferior.add(botaoAvan);
final Retroceder retro = new Retroceder(con);
final Avancar avan = new Avancar(con);
botaoAnt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
linha = retro.retrocederPedido();
}
});
botao.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Inserir(tPed.getText(),tPlaca.getText(),tFuncionario.getText(),area.getText());
try {
con.commit();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
linha = 0;
}
});
botao2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Alterar(tPed.getText(),tPlaca.getText(),tFuncionario.getText(),area.getText());
try {
con.commit();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
linha = 0;
}
});
botao3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String botoes[] = {"sim","não","Cancelar"};
int resultado = JOptionPane.showOptionDialog(null, "Você quer mesmo apagar o registro?!", "Alerta", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null, botoes, botoes[0]);
if(resultado == 0) {
new Deletar(tPed.getText());
try {
con.commit();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
linha = 0;
}
}
});
botao4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Selecionar(tPed.getText(),tPlaca.getText(),tFuncionario.getText(),area.getText());
}
});
botao5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPed.setText("");
tPlaca.setText("");
tFuncionario.setText("");
area.setText("");
tnFunc.setText("");
linha = 0;
}
});
botaoAvan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
linha = avan.avancarPedido();
}
});
inferior.setOpaque(false);
inferior.setBounds(10,380,530,35);
tela.add(titulo);
tela.add(componentes);
tela.add(inferior);
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new InterfacePedidos();
}
}
Classe Avançar - Avançar cursor
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
public class InterfacePedidos extends JFrame {
protected static JFormattedTextField tPlaca;
protected static JTextField tPed,tFuncionario,tnFunc;
public static int linha;
JLabel rPlaca;
protected static JTextArea area;
MaskFormatter placaValida;
Connection con;
JPanel meio,componentes0;
String n;
Selecionar s;
public InterfacePedidos() {
super("Pedidos ");
con = new Conexao().getConection();
setContentPane(new Fundo());
Container tela = getContentPane();
tela.setLayout(null);
JPanel titulo = new JPanel();
Font fonte = new Font("Rockwell Condensed",Font.PLAIN,30);
JLabel pedidos = new JLabel("Pedidos");
pedidos.setFont(fonte);
titulo.add(pedidos);
titulo.setOpaque(false);
titulo.setBounds(10,10,200,50);
try {
con.setAutoCommit(false);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
JPanel componentes = new JPanel(new FlowLayout(FlowLayout.LEFT));
componentes0 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel codPed = new JLabel("Código do Pedido: ");
tPed = new JTextField(10);
tPed.setDocument(new TamanhoMaximo(12));
tPed.setDocument(new SoNumericos());
tPed.setEditable(false);
try {
rPlaca = new JLabel("Placa: ");
placaValida = new MaskFormatter("UUU-####");
tPlaca = new JFormattedTextField(placaValida);
tPlaca.setColumns(10);
placaValida.setPlaceholderCharacter('_');
placaValida.setValidCharacters("ABCDEFGHIJKLMNOPRSTUWVXYZ0123456789");
tPlaca.setBorder(new LineBorder(Color.black));
} catch (ParseException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
JCheckBox caixa = new JCheckBox("Código Editável");
caixa.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
Component comp = componentes0.getComponent(2);
if(((JCheckBox) comp).isSelected()) {
tPed.setEditable(true);
}
else
tPed.setEditable(false);
}
});
JButton codigoE = new JButton("Código Editável");
codigoE.setBackground(Color.blue);
codigoE.setForeground(Color.white);
codigoE.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
n = JOptionPane.showInputDialog("Digite o codigo: ");
boolean passou = true;
Selecionar.selecionarCodigoPedido();
try {
if(n != "") {
Integer numero = Integer.parseInt(n);
for(int i = 0; i < Selecionar.codigo1.size(); i++) {
System.out.println("Array: " + Selecionar.codigo1.get(i));
System.out.println("n: ");
if(numero == Selecionar.codigo1.get(i)) {
JOptionPane.showMessageDialog(null,"Já existe esse código");
passou = false;
break;
}
}
if(passou == true) {
tPed.setText(n );
tPlaca.setText("");
tFuncionario.setText("");
tnFunc.setText("");
area.setText("");
}
}
}
catch(java.lang.NumberFormatException e2) {
JOptionPane.showMessageDialog(null,"Valor nulo!");
}
}
});
JLabel cFuncionario = new JLabel("Código do Funcionário: ");
tFuncionario = new JTextField(10);
tFuncionario.setDocument(new TamanhoMaximo(5));
tFuncionario.setDocument(new SoNumericos());
tFuncionario.setBorder(new LineBorder(Color.black));
JLabel nomeFunc = new JLabel("Nome do Funcionário: ");
tnFunc = new JTextField(10);
tnFunc.setEditable(false);
JLabel obs = new JLabel("Observação: ");
area = new JTextArea(10,20);
area.setDocument(new TamanhoMaximo(50));
JScrollPane painelRolagem = new JScrollPane(area);
painelRolagem.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
area.setLineWrap(true);
area.setWrapStyleWord(true);
componentes0.add(codPed);
componentes0.add(tPed);
componentes0.add(caixa);
componentes0.add(codigoE);
componentes0.setBounds(10,60,480,30);
componentes0.setOpaque(false);
tela.add(componentes0);
componentes.add(rPlaca);
componentes.add(tPlaca);
componentes.add(cFuncionario);
componentes.add(tFuncionario);
componentes.add(nomeFunc);
componentes.add(tnFunc);
componentes.add(obs);
componentes.add(painelRolagem);
componentes.setOpaque(false);
componentes.setBounds(10,86,260,280);
JPanel inferior = new JPanel();
JButton botaoAnt = new JButton("<<");
botaoAnt.setBackground(Color.blue);
botaoAnt.setForeground(Color.white);
JButton botao = new JButton("Inserir ");
botao.setBackground(Color.black);
botao.setForeground(Color.white);
JButton botao2 = new JButton("Alterar ");
botao2.setBackground(Color.black);
botao2.setForeground(Color.white);
JButton botao3 = new JButton("Apagar ");
botao3.setBackground(Color.black);
botao3.setForeground(Color.white);
JButton botao4 = new JButton("Selecionar");
botao4.setBackground(Color.black);
botao4.setForeground(Color.white);
JButton botao5 = new JButton("Limpar");
botao5.setBackground(Color.white);
botao5.setForeground(Color.black);
JButton botaoAvan = new JButton(">>");
botaoAvan.setBackground(Color.blue);
botaoAvan.setForeground(Color.WHITE);
inferior.add(botaoAnt);
inferior.add(botao);
inferior.add(botao2);
inferior.add(botao3);
inferior.add(botao4);
inferior.add(botao5);
inferior.add(botaoAvan);
final Retroceder retro = new Retroceder(con);
final Avancar avan = new Avancar(con);
botaoAnt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
linha = retro.retrocederPedido();
}
});
botao.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Inserir(tPed.getText(),tPlaca.getText(),tFuncionario.getText(),area.getText());
try {
con.commit();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
linha = 0;
}
});
botao2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Alterar(tPed.getText(),tPlaca.getText(),tFuncionario.getText(),area.getText());
try {
con.commit();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
linha = 0;
}
});
botao3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String botoes[] = {"sim","não","Cancelar"};
int resultado = JOptionPane.showOptionDialog(null, "Você quer mesmo apagar o registro?!", "Alerta", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null, botoes, botoes[0]);
if(resultado == 0) {
new Deletar(tPed.getText());
try {
con.commit();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
linha = 0;
}
}
});
botao4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Selecionar(tPed.getText(),tPlaca.getText(),tFuncionario.getText(),area.getText());
}
});
botao5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPed.setText("");
tPlaca.setText("");
tFuncionario.setText("");
area.setText("");
tnFunc.setText("");
linha = 0;
}
});
botaoAvan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
linha = avan.avancarPedido();
}
});
inferior.setOpaque(false);
inferior.setBounds(10,380,530,35);
tela.add(titulo);
tela.add(componentes);
tela.add(inferior);
setSize(800,600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
new InterfacePedidos();
}
}
Classe Retroceder: Retroceder o cursor.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import com.microsoft.sqlserver.jdbc.SQLServerResultSet;
public class Retroceder {
private ResultSet resultado;
Statement comando;
int rows;
public Retroceder(Connection con) {
try {
comando = con.createStatement(SQLServerResultSet.TYPE_SS_SCROLL_DYNAMIC,SQLServerResultSet.CONCUR_READ_ONLY);
resultado = comando.executeQuery("Select * from tab03PEDIDO tpedido inner join tab01funcionario tfunc on tpedido.cod_pedido = tfunc.cód_func");
resultado.afterLast();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int retrocederPedido() {
try {
if(!resultado.wasNull()) {
resultado.first();
resultado.relative(InterfacePedidos.linha);
resultado.previous();
resultado.setFetchSize(InterfacePedidos.linha);
InterfacePedidos.tPed.setText(String.valueOf(resultado.getInt("cod_Pedido")));
InterfacePedidos.tPlaca.setText(resultado.getString("placa"));
InterfacePedidos.tFuncionario.setText(String.valueOf(resultado.getInt("cod_func")));
InterfacePedidos.tnFunc.setText(resultado.getString("nome_func"));
InterfacePedidos.area.setText(resultado.getString("obs"));
rows = InterfacePedidos.linha -1;
System.out.println("Linha: " + resultado.getInt("cod_Pedido"));
}
else{
System.out.println("Erro no retrocesso");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null,"Registro Inválido");
}
return rows;
}
public void retrocederItemPed() {
}
public void fechar() {
try {
comando.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
abraço,