I'm just a beginner and I'm having a problem with implementing an item listener as an inner class.
What I want to happen is that if someone selects the checkbox t1
, the text above it, written in the TextArea txtTop
, becomes bold.
However, Eclipse tells me that there are multiple problems with my listener.
- It wants me to declare t1 and txtTop as final.
- It wants me to not make the listener class private.
- It doesn't find the listener anyway. The line
t1.addItemListener(new cl());
doesn't work.
So yeah, I don't know what to do. Hopefully one of you can help me out! :)
Here is the code:
import java.awt.BorderLayout;
public class WindowBuilderTest extends JFrame
{
private JPanel contentPane;
//Launch the application.
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
WindowBuilderTest frame = new WindowBuilderTest();
frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
// Creating Frame
public WindowBuilderTest()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1280, 720 );
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Create Font
Font headfont = new Font("Serif", Font.PLAIN, 15);
// Role Headlines
final JTextArea txtTop = new JTextArea();
txtTop.setEditable(false);
txtTop.setText("TOP");
txtTop.setBounds(180, 95, 32, 23);
txtTop.setFont(headfont);
contentPane.add(txtTop);
JTextArea txtMid = new JTextArea();
txtMid.setEditable(false);
txtMid.setText("MID");
txtMid.setBounds(252, 95, 32, 23);
contentPane.add(txtMid);
JTextArea txtJng = new JTextArea();
txtJng.setEditable(false);
txtJng.setText("JNG");
txtJng.setBounds(320, 95, 32, 23);
contentPane.add(txtJng);
JTextArea txtAdc = new JTextArea();
txtAdc.setEditable(false);
txtAdc.setText("ADC");
txtAdc.setBounds(389, 95, 32, 23);
contentPane.add(txtAdc);
JTextArea txtSup = new JTextArea();
txtSup.setEditable(false);
txtSup.setText("SUP");
txtSup.setBounds(453, 95, 32, 23);
contentPane.add(txtSup);
// Checkbox 1st row
final JCheckBox t1 = new JCheckBox("");
t1.setBounds(183, 143, 23, 23);
t1.addItemListener(new cl());
contentPane.add(t1);
JCheckBox m1 = new JCheckBox("");
m1.setBounds(255, 143, 23, 23);
contentPane.add(m1);
JCheckBox j1 = new JCheckBox("");
j1.setBounds(322, 143, 23, 23);
contentPane.add(j1);
JCheckBox a1 = new JCheckBox("");
a1.setBounds(393, 143, 23, 23);
contentPane.add(a1);
JCheckBox s1 = new JCheckBox("");
s1.setBounds(457, 143, 23, 23);
contentPane.add(s1);
class cl implements ItemListener
{
@Override
public void itemStateChanged(ItemEvent e)
{
if(t1.isSelected())
{
//Font headfont = txtTop.getFont().deriveFont(Font.BOLD, 40);
//txtTop.setFont(headfont);
System.out.println("HURRA!");
}
else
{
//Font headfont = txtTop.getFont().deriveFont(Font.PLAIN, 40);
//txtTop.setFont(headfont);
System.out.println("JUHU!");
}
}
}
}
}
This works for you:
Following problems you had:
ActionListener listner = new ActionListner{ ...}
. If you have a class like this, you can access the member variable.I renamed your ItemListner to
BoldChanger
. It gets the TextArea to change in the constructor.Btw:
CARRYTANKSUPPORT