Como criar um evento onde meu botão está em outra classe?
Aqui crio um botão.
import javax.swing.JPanel;
import javax.swing.JButton;
public class Panel extends JPanel {
private JButton jButton = null;
public Panel() {
super();
initialize();
}
private void initialize() {
this.setLayout(null);
this.setSize(60, 60);
this.add(getJButton(), null);
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("OK");
jButton.setBounds(new java.awt.Rectangle(0,0,60,60));
}
return jButton;
}
}
agora estou adicionando o botão em outra classe:
import javax.swing.JPanel;
public class PanelAction2 extends JPanel {
Panel panel = new Panel();
public PanelAction2() {
super();
initialize();
}
private void initialize() {
this.setSize(300, 200);
this.add(panel, null);
}
}
Mantu
#2
import javax.swing.JPanel;
/* * * * * * * * * * * * * * * * * */
import seu.pacote.que.contem.a.sua.classe.Panel;
/* * * * * * * * * * * * * * * * * */
public class PanelAction2 extends JPanel {
Panel panel = new Panel();
public PanelAction2() {
super();
initialize();
}
private void initialize() {
/* * * * * * * * * * * * * * * * * */
/*Instancie um objeto da SUA classe panel*/
Panel seuPanel = new Panel();
/* * * * * * * * * * * * * * * * * */
this.setSize(300, 200);
this.add(panel, null);
/* * * * * * * * * * * * * * * * * */
JButton btn = seuPanel.getJButton();
btn.addActionListener(
new AlgumaClasseQueImplementaActionListener()
);
this.add(btn.getJButton());
/* * * * * * * * * * * * * * * * * */
}
}
Beleza assim?
Não funcionou…
Desta maneira que você me explicou não deu…
Obrigado pela ajuda.
até+…
Mantu
#4
Tem como vc postar o código?
classe do botao:
import javax.swing.JPanel;
import javax.swing.JButton;
public class Panel extends JPanel {
private JButton jButton = null;
public Panel() {
super();
initialize();
}
private void initialize() {
this.setLayout(null);
this.setSize(60, 60);
this.add(getJButton(), null);
this.setVisible(true);
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("OK");
jButton.setBounds(new java.awt.Rectangle(0,0,60,60));
}
return jButton;
}
}
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PanelAction2 extends JFrame {
Panel panel = new Panel();
public static void main(String[] args) {
PanelAction2 action2 = new PanelAction2();
}
public PanelAction2() {
super();
initialize();
}
private void initialize() {
this.setSize(300, 200);
this.setTitle("teste");
this.add(panel, null);
this.setVisible(true);
// JButton button = new panel.getJButton();
}
}
até+…
Mantu
#6
Veja se é isso que vc tava querendo. Aqui funcionou:
package botaoEEventoEmClassesDiferentes;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class Panel extends JPanel {
private JButton jButton = null;
public Panel() {
super();
initialize();
}
private void initialize() {
this.setLayout(null);
this.setBounds(0, 0, 170, 70);
this.add(getJButton(), null);
this.setVisible(true);
setBorder(
BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(
Color.BLUE,
Color.GREEN
),
"Seu Painel",
TitledBorder.LEFT,
TitledBorder.BOTTOM
)
);
setBackground(Color.WHITE);
}
/*Eu naum tinha percebido antes, mas se esse método é private daí naum rola.
* Vou deixá-lo protected*/
protected JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("OK");
jButton.setBounds(new java.awt.Rectangle(0,0,60,60));
}
return jButton;
}
}
package botaoEEventoEmClassesDiferentes;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class PanelAction2 extends JFrame {
private class Handler implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(
((Component)e.getSource()).getParent(),
"Testando evento do Botão"
);
}
}
Panel panel = new Panel();
public static void main(String[] args) {
PanelAction2 action2 = new PanelAction2();
action2.show();
}
public PanelAction2() {
super();
initialize();
}
private void initialize() {
setSize(300, 200);
setTitle("teste");
getContentPane().setLayout(null);
JButton button = panel.getJButton();
button.addActionListener(new Handler());
button.setBounds(0, 130, 70, 30);
getContentPane().add(button);
getContentPane().add(panel);
}
}