Reputation: 21180
(only found out by 'accidentally' pausing my mouse on the button and seeing that it doesn't compile)
Original question
I have a very specific issue:
I have two different classes:
Class 1 implements a JPanel within a JFrame.
Class 2 implements a JFrame only.
In Class 2, the following code works perfectly:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class frameTest extends JFrame {
private JPanel contentPane;
private JTextField txtGeefLiefde;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frameTest frame = new frameTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public frameTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnTest = new JButton("press");
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testFunction();
}
});
btnTest.setBounds(162, 188, 89, 23);
contentPane.add(btnTest);
}
public void testFunction()
{
JTextPane textPane = new JTextPane();
textPane.setBounds(162, 231, 89, 20);
textPane.setText(":)");
contentPane.add(textPane);
}
}
Now I want to implement the exact same functionality in Class 1 as well.
I tried the following:
import java.awt.event.MouseAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
public class frontpanel extends JFrame {
private JPanel panel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frontpanel frame = new frontpanel();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public frontpanel() {
JButton btnTest = new JButton("press");
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testFunction();
}
});
btnTest.setBounds(162, 188, 89, 23);
panel.add(btnTest);
}
public void testFunction()
{
JTextPane textPane = new JTextPane();
textPane.setBounds(162, 231, 89, 20);
textPane.setText(":)");
panel.add(textPane);
}
}
where panel
is the JPanel in the JFrame.
I have been stuck on this for hours. I just can't seem to get ANY ActionListener
to work. What is causing the problem?
All help greatly appreciated!
Upvotes: 0
Views: 3673
Reputation: 2610
Giving the slighly modified code below to fit my test case of your code, adding panel
initialition and adding it to Test
frame, that you mentionned was a copy/paste error, with a dimension for ease of testing, the code works well on my side.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
public class Test extends JFrame {
private JPanel panel = new JPanel();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JButton btnTest = new JButton("press");
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testFunction();
}
});
btnTest.setBounds(162, 188, 89, 23);
panel.add(btnTest);
this.setSize(new Dimension(300, 300));
this.add(panel);
}
public void testFunction()
{
JTextPane textPane = new JTextPane();
textPane.setBounds(162, 231, 89, 20);
textPane.setText(":)");
panel.add(textPane);
}
}
Upvotes: 2
Reputation: 14413
The problem is that you never initialized panel
and also you have to add to the jframe
public FrontPanel() {
JButton btnTest = new JButton("press");
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testFunction();
}
});
// btnTest.setBounds(162, 188, 89, 23); you should avoid using this
panel = new JPanel();
panel.add(btnTest);
this.add(panel);
this.pack();
}
}
By the way and not less important,
1)Follow Java Code Conventions class names starts with UpperCase.
2) Don't extend JFrame
instead of that use composition against inheritance if you are not overriding JFrame
behaviour.
Example:
public class FrontPanel{
private JFrame frame;
private JPanel panel;
}
3) Don't use setBounds
instead of that delegate this job to LayoutManager
. Using Layout Managers
Upvotes: 2