Bom dia pessoal gostaria de uma ajuda com uma tabela aki, eu to aprendendo ainda sobre tabelas e nao sei oq acontece aki por q minha tela nao esta sendo visualizada uma amiga me passou os codigos para olhar mais nao sei oq deu errado se alguem puder agradeço
public class TreinandoTabela extends JFrame
{
public JButton
pesquisar, clear;
public JLabel
pesquisa;
public JTextField
tf_pesquisa,camp2;
public JTable
jt_agenda;
public JScrollPane
sp_agenda;
public DefaultTableModel
modelo;
public int
posicao;
public TreinandoTabela ()
{ setBounds(0,0,671,423);
setUndecorated(true);
setResizable(false);
getContentPane().setLayout(null);
setTitle("minha primeira tabela");
setLocationRelativeTo(null);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
WindowGUI();
addWindowListener
(
new WindowAdapter()
{
public void windowClosing
(
WindowEvent e
)
{
dispose();
}
}
);
}
public void WindowGUI()
{
getContentPane().setLayout(null);
pesquisa = new JLabel("Pesquisa: ");
pesquisa.setBounds(10,15,100,30);
getContentPane().add(pesquisa);
tf_pesquisa = new JTextField();
tf_pesquisa.setBounds(70,20,170,20);
tf_pesquisa.setBorder(new EtchedBorder(null, Color.BLACK));
tf_pesquisa.requestFocus();
getContentPane().add(tf_pesquisa);
jt_agenda = new JTable();
jt_agenda.setEnabled(false);
sp_agenda = new JScrollPane(jt_agenda);
sp_agenda.setBounds(8,65,645,306);
sp_agenda.setBorder(new EtchedBorder(null, Color.BLACK));
getContentPane().add(sp_agenda);
tf_pesquisa.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
String[][]
dados =
{
{"*****************","*****************","************","************"},
{"A","B","","C"},
{"A","B","","C"},
{"A","B","","C"},
{"A","B","","C"}
};
String[]
columnNames =
{
"Nome","Celular","Residencial","Email",
};
modelo = new DefaultTableModel(dados,columnNames);
jt_agenda.setModel(modelo);
pesquisar = new JButton(io_ie_pesquisar);
pesquisar.setBounds(322,16,130,30);
pesquisar.setText("Pesquisar");
pesquisar.setBorder(new EtchedBorder(null, Color.BLACK));
getContentPane().add(pesquisar);
clear = new JButton(io_ie_clear);
clear.setBounds(478,16,100,30);
clear.setText("Clear");
clear.setBorder(new EtchedBorder(null, Color.BLACK));
getContentPane().add(clear);
pesquisar.requestFocus();
tf_pesquisa.addKeyListener
(
new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if (
e.getKeyCode() == KeyEvent.VK_ESCAPE
)
{
dispose();
}
}
}
);
}
Action
io_ie_pesquisar = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
if (
tf_pesquisa.getText().trim().equals("help")
)
else if (
tf_pesquisa.getText().equals("")
)
{
JOptionPane.showMessageDialog(null, "O campo \"Pesquisa\" está vazio","Mensagem de Erro",2);
tf_pesquisa.requestFocus();
return;
}
else
{
for (
posicao = 0
;
posicao < jt_agenda.getModel().getRowCount()
;
posicao++
)
{
if (
tf_pesquisa.getText().trim().toUpperCase().equals(jt_agenda.getValueAt(posicao,0).toString())
||
tf_pesquisa.getText().trim().toUpperCase().equals(jt_agenda.getValueAt(posicao,1).toString())
||
tf_pesquisa.getText().trim().toUpperCase().equals(jt_agenda.getValueAt(posicao,2).toString())
||
tf_pesquisa.getText().trim().equals(jt_agenda.getValueAt(posicao,3).toString())
)
{
jt_agenda.setSelectionForeground(Color.BLUE);
Habilita o ScrollPane automático para a tabela.
scrollToVisible(jt_agenda, jt_agenda.getSelectedRow(), 0);
jt_agenda.repaint();
pesquisar.setEnabled(false);
tf_pesquisa.setEditable(false);
clear.requestFocus();
break;
}
}
if (
posicao >= jt_agenda.getRowCount()
)
{
JOptionPane.showMessageDialog(null, "\""+tf_pesquisa.getText()+"\" não está cadastrado");
tf_pesquisa.requestFocus();
}
}
}
};
Action
io_ie_clear = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
if (
tf_pesquisa.getText().equals("")
)
{
JOptionPane.showMessageDialog(null, "O campo \"Pesquisa\" está vazio","Mensagem de Erro",2);
tf_pesquisa.requestFocus();
}
else
{
tf_pesquisa.setText("");
tf_pesquisa.requestFocus();
}
jt_agenda.setSelectionForeground(Color.RED);
scrollToVisible(jt_agenda,0,0);
pesquisar.setEnabled(true);
tf_pesquisa.setEditable(true);
}
};
private
void scrollToVisible(JTable table, int rowIndex, int vColIndex)
{
if (
!(table.getParent()instanceof JViewport)
)
{
return;
}
JViewport
viewport = (JViewport)table.getParent();
Rectangle
rect = table.getCellRect(rowIndex, vColIndex, true);
Point
point = viewport.getViewPosition();
rect.setLocation(rect.x-point.x, rect.y-point.y);
viewport.scrollRectToVisible(rect);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
TreinandoTabela app = new TreinandoTabela();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
O codigo e meio grande pessoal ??
