Search code examples
jscrollpanejtextpane

textPane cannot be resolved


I am writing a program in eclipse that generates reports based on inputs from a user. I have a JTextPane in a JScrollPane to allow the user to enter a description of the text, then add it to the system by pressing a button, but I am getting the error "textPaneDescription cannot be resolved".

The code(excluding imports)

public class GUI{

private JFrame frame;
private JTextField textFieldHoursWorked;
private JTextField textFieldParking;
private JTextField textFieldCongestion;
private JTextField textFieldDays;
private JTextField textFieldSmallTools;
private JTextField textFieldHireCharges;
private JTextField textFieldMaterials;
private JTextField textFieldSubContracted;
private JTextField textFieldSupplyOnlyWorks;
private JTextField textFieldConsultants;
private int counter = 0;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                GUI window = new GUI();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public GUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    final ArrayList<Operative> tierOneArray = new ArrayList();
    final ArrayList<Operative> tierTwoArray = new ArrayList();
    final ArrayList<Operative> semiSkilledArray = new ArrayList();
    final ArrayList<Operative> supervisionArray = new ArrayList();
    frame = new JFrame();
    frame.setBounds(100, 100, 780, 555);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JScrollPane scrollPaneHistory = new JScrollPane();
    scrollPaneHistory.setBounds(353, 0, 411, 516);
    frame.getContentPane().add(scrollPaneHistory);

    final JTextArea textAreaHistory = new JTextArea();
    textAreaHistory.setEditable(false);
    scrollPaneHistory.setViewportView(textAreaHistory);

    final JComboBox comboBoxOperativeType = new JComboBox();
    comboBoxOperativeType.setModel(new DefaultComboBoxModel(new String[] {"Skilled Operative (Tier 1)", "Skilled Operative (Tier 2)", "Semi-skilled Operative", "Supervision"}));
    comboBoxOperativeType.setBounds(139, 52, 182, 20);
    frame.getContentPane().add(comboBoxOperativeType);

    JLabel lblOperativeType = new JLabel("Operative Type");
    lblOperativeType.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblOperativeType.setBounds(35, 52, 99, 20);
    frame.getContentPane().add(lblOperativeType);

    JLabel lblHoursWorked = new JLabel("Hours Worked");
    lblHoursWorked.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblHoursWorked.setBounds(40, 114, 89, 20);
    frame.getContentPane().add(lblHoursWorked);

    textFieldHoursWorked = new JTextField();
    textFieldHoursWorked.setBounds(139, 114, 182, 20);
    frame.getContentPane().add(textFieldHoursWorked);
    textFieldHoursWorked.setColumns(10);

    final JComboBox comboBoxWorkingHours = new JComboBox();
    comboBoxWorkingHours.setModel(new DefaultComboBoxModel(new String[] {"In", "Out"}));
    comboBoxWorkingHours.setBounds(139, 145, 182, 20);
    frame.getContentPane().add(comboBoxWorkingHours);

    JLabel lblNewLabelWorkingHours = new JLabel("In/Out of Hours");
    lblNewLabelWorkingHours.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblNewLabelWorkingHours.setBounds(35, 145, 99, 20);
    frame.getContentPane().add(lblNewLabelWorkingHours);

    final JRadioButton rdbtnEmergency = new JRadioButton("Emergency?");
    rdbtnEmergency.setFont(new Font("Tahoma", Font.PLAIN, 14));
    rdbtnEmergency.setForeground(new Color(0, 0, 0));
    rdbtnEmergency.setBounds(135, 233, 20, 23);
    frame.getContentPane().add(rdbtnEmergency);

    JLabel lblParking = new JLabel("Parking");
    lblParking.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblParking.setBounds(84, 176, 45, 18);
    frame.getContentPane().add(lblParking);

    textFieldParking = new JTextField();
    textFieldParking.setColumns(10);
    textFieldParking.setBounds(139, 176, 182, 20);
    frame.getContentPane().add(textFieldParking);

    JLabel lblCongestion = new JLabel("Congestion");
    lblCongestion.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblCongestion.setBounds(60, 205, 69, 18);
    frame.getContentPane().add(lblCongestion);

    textFieldCongestion = new JTextField();
    textFieldCongestion.setColumns(10);
    textFieldCongestion.setBounds(139, 206, 182, 20);
    frame.getContentPane().add(textFieldCongestion);

    JLabel lblEmergency = new JLabel("Emergency?");
    lblEmergency.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblEmergency.setBounds(51, 235, 83, 18);
    frame.getContentPane().add(lblEmergency);

    JButton btnAddOne = new JButton("Add");
    btnAddOne.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            counter ++;
            Labourer t1Operative = new Labourer("Tier 1", 29.75, 39.67, 39.67, 52.89, 45.00, 85.00);
            Labourer t2Operative = new Labourer("Tier 2", 32.98, 43.97, 43.97, 58.63, 45.00, 85.00);
            Labourer semiSkilledOperative = new Labourer("Semi Skilled", 25.50, 34.00, 34.00, 45.33, 45.00, 85.00);
            Labourer supervisionOperative = new Labourer("Tier 2", 34.00, 45.33, 45.33, 60.44, 65.00, 100.00);
            if(comboBoxOperativeType.getSelectedIndex() == 0)
            {
                tierOneArray.add(new Operative(textPaneDescription.getText(), (String)comboBoxOperativeType.getSelectedItem(), textFieldDays.getText(), textFieldHoursWorked.getText(), (String)comboBoxWorkingHours.getSelectedItem(),textFieldParking.getText(), textFieldCongestion.getText(), rdbtnEmergency.isSelected(), t1Operative));
            }
            else if(comboBoxOperativeType.getSelectedIndex() == 1)
            {
                tierTwoArray.add(new Operative(textPaneDescription.getText(), (String)comboBoxOperativeType.getSelectedItem(), textFieldDays.getText(), textFieldHoursWorked.getText(), (String)comboBoxWorkingHours.getSelectedItem(),textFieldParking.getText(), textFieldCongestion.getText(), rdbtnEmergency.isSelected(), t2Operative));
            }
            else if(comboBoxOperativeType.getSelectedIndex() == 2)
            {
                semiSkilledArray.add(new Operative(textPaneDescription.getText(), (String)comboBoxOperativeType.getSelectedItem(), textFieldDays.getText(), textFieldHoursWorked.getText(), (String)comboBoxWorkingHours.getSelectedItem(),textFieldParking.getText(), textFieldCongestion.getText(), rdbtnEmergency.isSelected(), semiSkilledOperative));
            }
            else if(comboBoxOperativeType.getSelectedIndex() == 3)
            {
                supervisionArray.add(new Operative(textPaneDescription.getText(), (String)comboBoxOperativeType.getSelectedItem(), textFieldDays.getText(), textFieldHoursWorked.getText(), (String)comboBoxWorkingHours.getSelectedItem(),textFieldParking.getText(), textFieldCongestion.getText(), rdbtnEmergency.isSelected(), supervisionOperative));
            }
            textAreaHistory.append(counter + ": " + (String)comboBoxOperativeType.getSelectedItem() + " / " + textFieldDays.getText()+ "day(s) / " + textFieldHoursWorked.getText() + "hours(s) / " + (String)comboBoxWorkingHours.getSelectedItem() + " / £" + textFieldParking.getText() + " / £" + textFieldCongestion.getText() + " / " + rdbtnEmergency.isSelected() + "\n");
        }
    });
    btnAddOne.setBounds(231, 235, 89, 23);
    frame.getContentPane().add(btnAddOne);

    JButton btnDone = new JButton("Done");
    btnDone.setBounds(231, 480, 89, 23);
    btnDone.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Logic logic = new Logic(tierOneArray, tierTwoArray, semiSkilledArray, supervisionArray);
        }
    });     
    frame.getContentPane().add(btnDone);

    JLabel labelDays = new JLabel("Days Worked");
    labelDays.setFont(new Font("Tahoma", Font.PLAIN, 14));
    labelDays.setBounds(45, 83, 89, 20);
    frame.getContentPane().add(labelDays);

    textFieldDays = new JTextField();
    textFieldDays.setColumns(10);
    textFieldDays.setBounds(139, 83, 182, 20);
    frame.getContentPane().add(textFieldDays);

    JSeparator separator = new JSeparator();
    separator.setBounds(0, 270, 352, 2);
    frame.getContentPane().add(separator);

    JLabel lblSmallTools = new JLabel("Small Tools and Incidental Costs");
    lblSmallTools.setBounds(8, 308, 219, 14);
    frame.getContentPane().add(lblSmallTools);

    textFieldSmallTools = new JTextField();
    lblSmallTools.setLabelFor(textFieldSmallTools);
    textFieldSmallTools.setBounds(199, 305, 141, 20);
    frame.getContentPane().add(textFieldSmallTools);
    textFieldSmallTools.setColumns(10);

    JLabel lblHireCharges = new JLabel("Hire Charges");
    lblHireCharges.setBounds(118, 283, 128, 14);
    frame.getContentPane().add(lblHireCharges);

    textFieldHireCharges = new JTextField();
    textFieldHireCharges.setColumns(10);
    textFieldHireCharges.setBounds(199, 280, 141, 20);
    frame.getContentPane().add(textFieldHireCharges);

    JLabel lblMaterials = new JLabel("Material Purchase Costs");
    lblMaterials.setBounds(51, 333, 181, 14);
    frame.getContentPane().add(lblMaterials);

    textFieldMaterials = new JTextField();
    textFieldMaterials.setColumns(10);
    textFieldMaterials.setBounds(199, 333, 141, 20);
    frame.getContentPane().add(textFieldMaterials);

    JLabel lblSubcontracted = new JLabel("Specialist Subcontraced Works");
    lblSubcontracted.setBounds(10, 361, 212, 14);
    frame.getContentPane().add(lblSubcontracted);

    textFieldSubContracted = new JTextField();
    textFieldSubContracted.setColumns(10);
    textFieldSubContracted.setBounds(199, 358, 141, 20);
    frame.getContentPane().add(textFieldSubContracted);

    JLabel lblSupplyOnlyWorks = new JLabel("Supply Only Works");
    lblSupplyOnlyWorks.setBounds(80, 389, 116, 17);
    frame.getContentPane().add(lblSupplyOnlyWorks);

    textFieldSupplyOnlyWorks = new JTextField();
    textFieldSupplyOnlyWorks.setColumns(10);
    textFieldSupplyOnlyWorks.setBounds(199, 387, 141, 20);
    frame.getContentPane().add(textFieldSupplyOnlyWorks);

    JLabel lblConsultants = new JLabel("Consultants and Designers");
    lblConsultants.setBounds(35, 417, 206, 14);
    frame.getContentPane().add(lblConsultants);

    textFieldConsultants = new JTextField();
    textFieldConsultants.setColumns(10);
    textFieldConsultants.setBounds(199, 414, 141, 20);
    frame.getContentPane().add(textFieldConsultants);

    JButton btnAddTwo = new JButton("Add");
    btnAddTwo.setBounds(232, 446, 89, 23);
    frame.getContentPane().add(btnAddTwo);

    JLabel lblDescription = new JLabel("Description");
    lblDescription.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblDescription.setBounds(60, 23, 69, 20);
    frame.getContentPane().add(lblDescription);

    JScrollPane scrollPaneDescription = new JScrollPane();
    scrollPaneDescription.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPaneDescription.setBounds(139, 23, 182, 20);
    frame.getContentPane().add(scrollPaneDescription);

    JTextPane textPaneDescription = new JTextPane();
    scrollPaneDescription.setViewportView(textPaneDescription);
}

}


Solution

  • It looks like you are accessing the variable textPaneDescription before you actually declared it.

    You access the textPaneDescription when adding an action listener.

    tierOneArray.add(new Operative(textPaneDescription.getText() ... );

    While not only till later in the code you declare the variable, thus it can not be resolved.