Skip to main content

JButton ActionListener problem

1 reply [Last post]
middaycrisis
Offline
Joined: 2007-11-12
Points: 0

Hi there!
I'm experiencing problem with my code. I have a background image of JFrame, and custom images of JButtons. When trying to add ActionListener to the JButtons, it seems that it doesn't run the needed code. Really desperate to find the problem. Code:

<br />
package gui;</p>
<p>import java.awt.event.*;</p>
<p>import javax.swing.*;</p>
<p>import java.util.*;<br />
import java.awt.*;<br />
import java.util.*;</p>
<p>public class MainFrame extends JFrame {<br />
 private JPanel panel;<br />
 private JButton newParticipant, viewLists, addConference;<br />
 private JScrollPane scrollPane;<br />
 private ImageIcon icon, icon2, icon3, icon4;<br />
 private Image image;<br />
 private Controller controller = new Controller();<br />
// public ConferenceDialog confDialog;</p>
<p>public MainFrame(){</p>
<p>//confDialog = new ConferenceDialog();<br />
icon = new ImageIcon("src/gui/bg.jpg");<br />
icon2 = new ImageIcon("src/gui/main_03.jpg");<br />
icon3 = new ImageIcon ("src/gui/main_05.jpg");<br />
icon4 = new ImageIcon ("src/gui/main_06.jpg");</p>
<p>	JPanel panel = new JPanel()<br />
	{</p>
<p>		protected void paintComponent(Graphics g)<br />
		{</p>
<p>			g.drawImage(icon.getImage(), 0, 0, this);<br />
			super.paintComponent(g);<br />
		}<br />
	};</p>
<p>	panel.setOpaque( false );<br />
	panel.setSize(500, 500) ;<br />
	scrollPane = new JScrollPane( panel );<br />
	getContentPane().add( scrollPane );</p>
<p>	panel.setLayout(null); </p>
<p>	    JButton newParticipant = new JButton(icon2);<br />
	    newParticipant.setMargin(new Insets(1, 1, 1, 1));<br />
	    newParticipant.setBounds(120, 170, 256, 85);<br />
	    newParticipant.addActionListener(controller);<br />
	    panel.add(newParticipant);</p>
<p>	    JButton viewLists = new JButton(icon3);<br />
	    viewLists.setMargin(new Insets(1, 1, 1, 1));<br />
	    viewLists.setBounds(120, 80, 256, 85);<br />
	    viewLists.addActionListener(controller);<br />
	    panel.add(viewLists);</p>
<p>	    JButton addConference = new JButton(icon4);<br />
	    addConference.setMargin(new Insets(1, 1, 1, 1));<br />
	    addConference.setBounds(120, 260, 256, 85);<br />
	    addConference.addActionListener(controller);<br />
	    panel.add(addConference);</p>
<p>}</p>
<p>private class Controller implements ActionListener {</p>
<p>	public void actionPerformed(ActionEvent e) {</p>
<p>		if (e.getSource() == addConference){<br />
			System.out.println("Test");</p>
<p>		}<br />
		if (e.getSource() == viewLists){<br />
			System.out.println("Check");<br />
		}</p>
<p>}<br />
}}</p>
<p>

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jtobia
Offline
Joined: 2007-04-23
Points: 0

in these row:

[code]
...
JPanel panel = new JPanel() {
...
JButton newParticipant = new JButton(icon2);
...
JButton viewLists = new JButton(icon3);
...
JButton addConference = new JButton(icon4);
...
[/code]

you define a local variables that hide the Instance variables of MainFrame.

rewrite these row in this mode:

[code]
...
panel = new JPanel() {
...
newParticipant = new JButton(icon2);
...
viewLists = new JButton(icon3);
...
addConference = new JButton(icon4);
...
[/code]