Emergency: "error: non-static variable this cannot be referenced from a static context"

2.7k views Asked by At

I have 4 hours to complete my exercise but I get multiple "cannot be referenced from a static context" errors.

Here are these in full:

MainApp.java:281: error: non-static variable this cannot be referenced from a static context
  list.addMouseListener(this);
                        ^
MainApp.java:289: error: non-static method setBounds(int,int,int,int) cannot be referenced from a static context
  setBounds(300, 300, 350, 150);
  ^
MainApp.java:292: error: non-static method getContentPane() cannot be referenced from a static context
  Container cp = getContentPane();
                 ^
MainApp.java:299: error: non-static variable this cannot be referenced from a static context
  button1.addActionListener(this);
                            ^
MainApp.java:326: error: non-static method pack() cannot be referenced from a static context
  pack();
  ^
MainApp.java:328: error: non-static method setDefaultCloseOperation(int) cannot be referenced from a static context
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  ^
MainApp.java:330: error: non-static method RentShop(String,Catalogs,DefaultListModel) cannot be referenced from a static context
  RentShop("Film",cat,ListModel);
  ^
MainApp.java:343: error: non-static variable this cannot be referenced from a static context
  list2.addMouseListener(this);
                         ^
MainApp.java:351: error: non-static method setBounds(int,int,int,int) cannot be referenced from a static context
  setBounds(300, 300, 350, 150);
  ^
MainApp.java:354: error: non-static method getContentPane() cannot be referenced from a static context
  Container cp2 = getContentPane();
                  ^
MainApp.java:363: error: non-static variable this cannot be referenced from a static context
  button2.addActionListener(this);
                            ^
MainApp.java:391: error: non-static method pack() cannot be referenced from a static context
  pack();
  ^
MainApp.java:393: error: non-static method setDefaultCloseOperation(int) cannot be referenced from a static context
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  ^
MainApp.java:395: error: non-static method RentShop(String,Catalogs,DefaultListModel) cannot be referenced from a static context
  RentShop("Game",cat,ListModel2);
  ^
MainApp.java:407: error: non-static variable this cannot be referenced from a static context
  list3.addMouseListener(this);
                         ^
MainApp.java:414: error: non-static method setBounds(int,int,int,int) cannot be referenced from a static context
  setBounds(300, 300, 350, 150);
  ^
MainApp.java:416: error: non-static method getContentPane() cannot be referenced from a static context
  Container cp3 = getContentPane();
                  ^
MainApp.java:426: error: non-static variable this cannot be referenced from a static context
  button3.addActionListener(this);
                            ^
MainApp.java:447: error: non-static method pack() cannot be referenced from a static context
  pack();
  ^
MainApp.java:449: error: non-static method setDefaultCloseOperation(int) cannot be referenced from a static context
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  ^
MainApp.java:463: error: non-static method add(Component) cannot be referenced from a static context
  add(tabbedPane);
  ^
Note: MainApp.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
21 errors

I know about the error in general, however I think this is a different case. For example, I use "this" in "addMouseListener", in a static function called in a static main, but I still get an error.

My code of MainApp.java, the main class, is:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JList;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import java.util.*;


public class MainApp extends JFrame implements ActionListener, MouseListener
{
 
 private static String ans;
 private static int ans2;
 private static boolean over;
 
 private int li=-1;
 private int li2=-1;
 private int li3=-1;
 
 private static JFrame frame;
 
 private static JButton button1;
 private static JButton button2;
 private static JButton button3;
 
 private static JButton gfbutton;
 private static JButton rbutton;
 
 private static JLabel label;
 private static JLabel label2;
 private static JLabel label3;
 
 private static JTextArea area;
 private static JTextArea area2;
 private static JTextArea area3;
 
 private static JList list;
 private static JList list2;
 private static JList list3;
 
 private static DefaultListModel ListModel;
 private static DefaultListModel ListModel2;
 private static DefaultListModel ListModel3;
 
 public static Catalogs cat = new Catalogs();
 public static String Items;
 public static String RentsList;
 
 
 
 public void lists(JTextArea area, JLabel label, JList list) //For games and films
 {
  boolean flag=false;
  int i=0;
   
  while(i<cat.items.size() && flag==false)
  {
   if( cat.items.get(i).getProname().equals ( list.getSelectedValue() ) )
   {
    area.setText(cat.items.get(i).toString());
    
    String pathimage = "Images/" + cat.items.get(i).getProname() + ".jpg";
    
    label.setIcon(new ImageIcon(pathimage));
    
    flag=true;
   }
   
   i++;
  }
  
  if(cat.items.get(i).getRentnum()>0)
   cat.rentItems.add(cat.items.get(i).RentItem());  
   else 
   JOptionPane.showMessageDialog(null,"Sorry...","There are no available copies left",JOptionPane.INFORMATION_MESSAGE); 
 }
 
 
 
 
 public void mouseClicked(MouseEvent event)
 {      
  
  if (list.getSelectedIndex() != li) 
  {
   lists(area, label, list);
  }
  
  else if (list2.getSelectedIndex() != li2) 
  {
   lists(area2, label2, list2);
  }
  
  else if (list3.getSelectedIndex() != li3) 
  {
   boolean flag=false;
   int i=0;
   
   while(i<cat.rentItems.size() && flag==false) 
   {
    if( cat.rentItems.get(i).getProname().equals ( list.getSelectedValue() ) )
    {
     area.setText(cat.rentItems.get(i).toString());
     
     String pathimage = "Images/" + cat.rentItems.get(i).getProname() + ".jpg";
    
     label.setIcon(new ImageIcon(pathimage));
     
     flag=true;
    }
    
    i++;
   }
  }
  
  li = list.getSelectedIndex();
  li2 = list2.getSelectedIndex();
  li3 = list3.getSelectedIndex();
 }
 
 public void mouseExited(MouseEvent event){}
 public void mouseEntered(MouseEvent event){}
 public void mouseReleased(MouseEvent event){}
 public void mousePressed(MouseEvent event){}
  
 
 public void actionPerformed(ActionEvent e) 
 {
  if  (e.getSource() == button1) {
   cat.items.get(list.getSelectedIndex()).RentItem();
  }
  
  else if  (e.getSource() == button2) {
   cat.items.get(list2.getSelectedIndex()).RentItem();
  }
  
  else if (e.getSource() == button3) 
  {
   boolean flag=false;
   int i = list3.getSelectedIndex();
   
   if (i != -1) 
   {

    while(i<cat.items.size() && flag==false) //Find the product in items via renting name
    {
     if( cat.items.get(i).getProname().equals( cat.rentItems.get(i).getProname() ) )
     {
      cat.items.get(i).setRentnum( cat.items.get(i).getRentnum() + 1 );
      flag=true;
     }
     
     i++;
    }
     
    ListModel3.remove(i);
    cat.rentItems.remove(i);
   }
  }
  
 }
 
 
 
 public void RentShop(String type, Catalogs cat, DefaultListModel ListModel) //Function for renting each product
 {
  for(Products product : cat.items)
  { 
    if ( product.getType().equals(type) ) 
     ListModel.addElement( product.getProname() );   
  }
 }

 
 
 //--------------------------------------------------Main program
 
 public static void main(String args[]) 
 { 
  
  //First window for importing
  
  JFrame.setDefaultLookAndFeelDecorated(true);
  JDialog.setDefaultLookAndFeelDecorated(true);
  frame = new JFrame("Importing...");
  
  frame.setLayout(new FlowLayout());
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  JButton gfbutton = new JButton("Select File for Games and Films");
  JButton rbutton = new JButton("Select File for Rents");
  
  gfbutton.addActionListener(new ActionListener() {
  
   public void actionPerformed(ActionEvent ae) {
    JFileChooser fi = new JFileChooser();
    int returnValue = fi.showOpenDialog(null);
    
    if (returnValue == fi.APPROVE_OPTION) {
       Items = fi.getSelectedFile().getPath();
    }
    
    if(over==true) frame.dispose();
    over = true;
   }
   
  });
  
  rbutton.addActionListener(new ActionListener(){
  
   public void actionPerformed(ActionEvent ae) 
   {
    JFileChooser fi = new JFileChooser();
    int returnValue = fi.showOpenDialog(null);
   
    if (returnValue == fi.APPROVE_OPTION) {
     RentsList = fi.getSelectedFile().getPath();
////////////////////??????????
    }
   
    if(over==true) 
    {
     frame.dispose();
     MainPro();
    }
    over = true;
   }
  });
 }
 
 
 public static void MainPro(){
  
  frame = new JFrame("Video Club"); //?
  
  frame.setBounds(500,300,400,500);
  frame.add(gfbutton);
  frame.add(rbutton);
  frame.pack();
  frame.setVisible(true);

   
  L_File lf = new L_File(cat,"Items.txt", "RentsList.txt"); //Importing
  lf.loadFile();
  
  
  //Main window, 1st tab
  
  JTabbedPane tabbedPane = new JTabbedPane();
   
  JPanel panel1 = new JPanel();
  
  tabbedPane.addTab("Movies",null,panel1,"Movies");
  
  JList list = new JList();
 
  
  list.addMouseListener(this);
  panel1.add(list);
  
  JTextArea area = new JTextArea("Details");
  panel1.add(area,BorderLayout.SOUTH);
  ListModel = new DefaultListModel();


  setBounds(300, 300, 350, 150);


  Container cp = getContentPane();
  cp.setLayout(new BorderLayout());

  
  panel1.setLayout(new FlowLayout());

  button1= new JButton("Rent");
  button1.addActionListener(this);
  
  panel1.add(button1);
  
  area.setFont(new Font("Serif", Font.ITALIC, 18));
  area.setForeground(Color.BLUE);
  area.setEditable(false);
  
  cp.add(panel1, BorderLayout.LINE_START);

 
  // A JList presents the user with a group of items (DefaultListModel)

  list = new JList(ListModel);
  list.setSelectedIndex(0);

  // A JScrollPane provides a scrollable view of a component

  JScrollPane listScroller = new JScrollPane(list);
  listScroller.setPreferredSize(new Dimension(150, 100));
  cp.add(listScroller, BorderLayout.CENTER);


  label = new JLabel();
  //label.setIcon(new ImageIcon(products.get(0).getImagePath()));
  cp.add(label, BorderLayout.LINE_END);

  pack();

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  RentShop("Film",cat,ListModel);
  
  

  //2nd tab
  
  JPanel panel2 = new JPanel();
  
  tabbedPane.addTab("Games",null,panel2,"Games");
  
  JList list2 = new JList();
 
  
  list2.addMouseListener(this);
  panel2.add(list2,BorderLayout.SOUTH);
  
  JTextArea area2 = new JTextArea("Details");
  panel2.add(area2);
  DefaultListModel ListModel2 = new DefaultListModel();


  setBounds(300, 300, 350, 150);

  
  Container cp2 = getContentPane();
  cp2.setLayout(new BorderLayout());


  panel2.setLayout(new FlowLayout());

  button2= new JButton("Rent");
  
  panel2.add(button2);
  button2.addActionListener(this);
  

  area2.setFont(new Font("Serif", Font.ITALIC, 18));
  area2.setForeground(Color.BLUE);
  area2.setEditable(false);
  

  cp2.add(panel2, BorderLayout.LINE_START);

  
  // A JList presents the user with a group of items (DefaultListModel)

  list2 = new JList(ListModel);
  list2.setSelectedIndex(0);

  // A JScrollPane provides a scrollable view of a component

  JScrollPane listScroller2 = new JScrollPane(list);
  listScroller2.setPreferredSize(new Dimension(150, 100));
  cp2.add(listScroller2, BorderLayout.CENTER);


  label2 = new JLabel();
  //label2.setIcon(new ImageIcon(products.get(0).getImagePath()));
  cp2.add(label2, BorderLayout.LINE_END);


  pack();

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  RentShop("Game",cat,ListModel2);
  
 
 //3rd tab
  
  JPanel panel3 = new JPanel();
  
  tabbedPane.addTab("Rents",null,panel3,"Rents");
  
  JList list3 = new JList();
 
  
  list3.addMouseListener(this);
  panel3.add(list3,BorderLayout.SOUTH);
  
  JTextArea area3 = new JTextArea("Details");
  panel3.add(area3);
  DefaultListModel ListModel3 = new DefaultListModel();
  
  setBounds(300, 300, 350, 150);

  Container cp3 = getContentPane();
  cp3.setLayout(new BorderLayout());


  panel3.setLayout(new FlowLayout());

  button3= new JButton("Remove Rent");
  

  panel3.add(button3);
  button3.addActionListener(this);
  
  area3.setFont(new Font("Serif", Font.ITALIC, 18));
  area3.setForeground(Color.BLUE);
  area3.setEditable(false);
  

  cp3.add(panel3, BorderLayout.LINE_START);


  // A JList presents the user with a group of items (DefaultListModel)

  list3 = new JList(ListModel2);
  list3.setSelectedIndex(0);

  // A JScrollPane provides a scrollable view of a component

  JScrollPane listScroller3 = new JScrollPane(list);
  listScroller3.setPreferredSize(new Dimension(150, 100));
  cp3.add(listScroller3, BorderLayout.CENTER);

  pack();

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
  
 
  //After designing:
  
  for(Rents rent : cat.rentItems)
  {  
   ListModel3.addElement( rent.getProname() );   
  }
 
 
  JOptionPane.showMessageDialog(null,"Total Cost","The total renting cost is " + cat.CalculateTotalCost() + " drachmes",JOptionPane.PLAIN_MESSAGE); 
 
  add(tabbedPane);
  
  
 }
  
}

Any idea will be much appreciated, thank you!

3

There are 3 answers

1
Shaun On BEST ANSWER

You need to create an instance of MainApp in main

Instead of having all your code in your main(), you need to transfer that code to a new private void run() method (or whatever you want to call it).

public static void main(String[] args) {
   new MainApp().run();
}

Now, since you have an instance get rid of all your private static variables in favor of just private

private static String ans;

becomes

private String ans;

etc

Something like... (I have not tested this). Note, I changed MainPro to an instance method as well. Removed all the statics

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JList;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import java.util.*;


public class MainApp extends JFrame implements ActionListener, MouseListener
{

    private String ans;
    private int ans2;
    private boolean over;

    private int li=-1;
    private int li2=-1;
    private int li3=-1;

    private JFrame frame;

    private JButton button1;
    private JButton button2;
    private JButton button3;

    private JButton gfbutton;
    private JButton rbutton;

    private JLabel label;
    private JLabel label2;
    private JLabel label3;

    private JTextArea area;
    private JTextArea area2;
    private JTextArea area3;

    private JList list;
    private JList list2;
    private JList list3;

    private DefaultListModel ListModel;
    private DefaultListModel ListModel2;
    private DefaultListModel ListModel3;

    public static Catalogs cat = new Catalogs();
    public static String Items;
    public static String RentsList;



    public void lists(JTextArea area, JLabel label, JList list) //For games and films
    {
        boolean flag=false;
        int i=0;

        while(i<cat.items.size() && flag==false)
        {
            if( cat.items.get(i).getProname().equals ( list.getSelectedValue() ) )
            {
                area.setText(cat.items.get(i).toString());

                String pathimage = "Images/" + cat.items.get(i).getProname() + ".jpg";

                label.setIcon(new ImageIcon(pathimage));

                flag=true;
            }

            i++;
        }

        if(cat.items.get(i).getRentnum()>0)
            cat.rentItems.add(cat.items.get(i).RentItem());     
         else   
            JOptionPane.showMessageDialog(null,"Sorry...","There are no available copies left",JOptionPane.INFORMATION_MESSAGE); 
    }




    public void mouseClicked(MouseEvent event)
    {                       

        if (list.getSelectedIndex() != li) 
        {
            lists(area, label, list);
        }

        else if (list2.getSelectedIndex() != li2) 
        {
            lists(area2, label2, list2);
        }

        else if (list3.getSelectedIndex() != li3) 
        {
            boolean flag=false;
            int i=0;

            while(i<cat.rentItems.size() && flag==false) 
            {
                if( cat.rentItems.get(i).getProname().equals ( list.getSelectedValue() ) )
                {
                    area.setText(cat.rentItems.get(i).toString());

                    String pathimage = "Images/" + cat.rentItems.get(i).getProname() + ".jpg";

                    label.setIcon(new ImageIcon(pathimage));

                    flag=true;
                }

                i++;
            }
        }

        li = list.getSelectedIndex();
        li2 = list2.getSelectedIndex();
        li3 = list3.getSelectedIndex();
    }

    public void mouseExited(MouseEvent event){}
    public void mouseEntered(MouseEvent event){}
    public void mouseReleased(MouseEvent event){}
    public void mousePressed(MouseEvent event){}


    public void actionPerformed(ActionEvent e) 
    {
        if  (e.getSource() == button1) {
            cat.items.get(list.getSelectedIndex()).RentItem();
        }

        else if  (e.getSource() == button2) {
            cat.items.get(list2.getSelectedIndex()).RentItem();
        }

        else if (e.getSource() == button3) 
        {
            boolean flag=false;
            int i = list3.getSelectedIndex();

            if (i != -1) 
            {

                while(i<cat.items.size() && flag==false) //Find the product in items via renting name
                {
                    if( cat.items.get(i).getProname().equals( cat.rentItems.get(i).getProname() ) )
                    {
                        cat.items.get(i).setRentnum( cat.items.get(i).getRentnum() + 1 );
                        flag=true;
                    }

                    i++;
                }

                ListModel3.remove(i);
                cat.rentItems.remove(i);
            }
        }

    }



    public void RentShop(String type, Catalogs cat, DefaultListModel ListModel) //Function for renting each product
    {
        for(Products product : cat.items)
        {   
                if ( product.getType().equals(type) ) 
                    ListModel.addElement( product.getProname() );           
        }
    }


    private void run() {
        //First window for importing

        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        frame = new JFrame("Importing...");

        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton gfbutton = new JButton("Select File for Games and Films");
        JButton rbutton = new JButton("Select File for Rents");

        gfbutton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                JFileChooser fi = new JFileChooser();
                int returnValue = fi.showOpenDialog(null);

                if (returnValue == fi.APPROVE_OPTION) {
                   Items = fi.getSelectedFile().getPath();
                }

                if(over==true) frame.dispose();
                over = true;
            }

        });

        rbutton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent ae) 
            {
                JFileChooser fi = new JFileChooser();
                int returnValue = fi.showOpenDialog(null);

                if (returnValue == fi.APPROVE_OPTION) {
                    RentsList = fi.getSelectedFile().getPath();
////////////////////??????????
                }

                if(over==true) 
                {
                    frame.dispose();
                    MainPro();
                }
                over = true;
            }
        });
    }


    //--------------------------------------------------Main program

    public static void main(String args[]) 
    {   
        new MainApp().run();
    }


    /**
      * I have no idea what you're doing here but this should also not be static
      */
    private void MainPro(){

        frame = new JFrame("Video Club"); //?

        frame.setBounds(500,300,400,500);
        frame.add(gfbutton);
        frame.add(rbutton);
        frame.pack();
        frame.setVisible(true);


        L_File lf = new L_File(cat,"Items.txt", "RentsList.txt"); //Importing
        lf.loadFile();


        //Main window, 1st tab

        JTabbedPane tabbedPane = new JTabbedPane();

        JPanel panel1 = new JPanel();

        tabbedPane.addTab("Movies",null,panel1,"Movies");

        JList list = new JList();


        list.addMouseListener(this);
        panel1.add(list);

        JTextArea area = new JTextArea("Details");
        panel1.add(area,BorderLayout.SOUTH);
        ListModel = new DefaultListModel();


        setBounds(300, 300, 350, 150);


        Container cp = getContentPane();
        cp.setLayout(new BorderLayout());


        panel1.setLayout(new FlowLayout());

        button1= new JButton("Rent");
        button1.addActionListener(this);

        panel1.add(button1);

        area.setFont(new Font("Serif", Font.ITALIC, 18));
        area.setForeground(Color.BLUE);
        area.setEditable(false);

        cp.add(panel1, BorderLayout.LINE_START);


        // A JList presents the user with a group of items (DefaultListModel)

        list = new JList(ListModel);
        list.setSelectedIndex(0);

        // A JScrollPane provides a scrollable view of a component

        JScrollPane listScroller = new JScrollPane(list);
        listScroller.setPreferredSize(new Dimension(150, 100));
        cp.add(listScroller, BorderLayout.CENTER);


        label = new JLabel();
        //label.setIcon(new ImageIcon(products.get(0).getImagePath()));
        cp.add(label, BorderLayout.LINE_END);

        pack();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        RentShop("Film",cat,ListModel);



        //2nd tab

        JPanel panel2 = new JPanel();

        tabbedPane.addTab("Games",null,panel2,"Games");

        JList list2 = new JList();


        list2.addMouseListener(this);
        panel2.add(list2,BorderLayout.SOUTH);

        JTextArea area2 = new JTextArea("Details");
        panel2.add(area2);
        DefaultListModel ListModel2 = new DefaultListModel();


        setBounds(300, 300, 350, 150);


        Container cp2 = getContentPane();
        cp2.setLayout(new BorderLayout());


        panel2.setLayout(new FlowLayout());

        button2= new JButton("Rent");

        panel2.add(button2);
        button2.addActionListener(this);


        area2.setFont(new Font("Serif", Font.ITALIC, 18));
        area2.setForeground(Color.BLUE);
        area2.setEditable(false);


        cp2.add(panel2, BorderLayout.LINE_START);


        // A JList presents the user with a group of items (DefaultListModel)

        list2 = new JList(ListModel);
        list2.setSelectedIndex(0);

        // A JScrollPane provides a scrollable view of a component

        JScrollPane listScroller2 = new JScrollPane(list);
        listScroller2.setPreferredSize(new Dimension(150, 100));
        cp2.add(listScroller2, BorderLayout.CENTER);


        label2 = new JLabel();
        //label2.setIcon(new ImageIcon(products.get(0).getImagePath()));
        cp2.add(label2, BorderLayout.LINE_END);


        pack();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        RentShop("Game",cat,ListModel2);


    //3rd tab

        JPanel panel3 = new JPanel();

        tabbedPane.addTab("Rents",null,panel3,"Rents");

        JList list3 = new JList();


        list3.addMouseListener(this);
        panel3.add(list3,BorderLayout.SOUTH);

        JTextArea area3 = new JTextArea("Details");
        panel3.add(area3);
        DefaultListModel ListModel3 = new DefaultListModel();

        setBounds(300, 300, 350, 150);

        Container cp3 = getContentPane();
        cp3.setLayout(new BorderLayout());


        panel3.setLayout(new FlowLayout());

        button3= new JButton("Remove Rent");


        panel3.add(button3);
        button3.addActionListener(this);

        area3.setFont(new Font("Serif", Font.ITALIC, 18));
        area3.setForeground(Color.BLUE);
        area3.setEditable(false);


        cp3.add(panel3, BorderLayout.LINE_START);


        // A JList presents the user with a group of items (DefaultListModel)

        list3 = new JList(ListModel2);
        list3.setSelectedIndex(0);

        // A JScrollPane provides a scrollable view of a component

        JScrollPane listScroller3 = new JScrollPane(list);
        listScroller3.setPreferredSize(new Dimension(150, 100));
        cp3.add(listScroller3, BorderLayout.CENTER);

        pack();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        //After designing:

        for(Rents rent : cat.rentItems)
        {    
            ListModel3.addElement( rent.getProname() );         
        }


        JOptionPane.showMessageDialog(null,"Total Cost","The total renting cost is " + cat.CalculateTotalCost() + " drachmes",JOptionPane.PLAIN_MESSAGE); 

        add(tabbedPane);


    }

}
0
vinothM On

You can not access "this" in any static method because when the method is initialized there is not instance of the class is ready and it is not fully constructed so access to this from any static method is invalid. If you still need to access the object of that class you need to create the instance and pass it as an object instead of this

0
Gowrav On

static methods are not bound to any object. "this" keyowrd represents the object to which its bound.

You cannot use a non-static from a static method

Here is an post from eariler: Why can't we use 'this' keyword in a static method