0

I had a big java file with all my code and it worked but now I am splitting it up I have run into an error I can't fix.

Here is the gui.java code:

public class gui extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

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

    /**
     * Create the frame.
     * @throws IOException 
     * @throws ClassNotFoundException 
     * @throws SQLException 
     */

    public gui() throws IOException, ClassNotFoundException, SQLException {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 655, 420);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(1, 1));
        setContentPane(contentPane);

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        contentPane.add(tabbedPane, BorderLayout.CENTER);

        setResizable(false);

        //JOptionPane.showMessageDialog(contentPane, "Connecting to database. Ensure that there is no other process connecting to the database and this file is in the root directory of the DED project.");


        Class.forName("org.h2.Driver");

        String path = System.getProperty("user.dir");
        String fullPath = path +"\\files\\db\\dedserver;AUTO_SERVER=TRUE";

        Connection conn = DriverManager.getConnection("jdbc:h2://"+fullPath, "sa", "");
        Statement stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

         ImageBuilder builder = new ImageBuilder();

        //home

        JPanel home = new JPanel(new GridLayout());
        Component hLabel = functions.makeTextPanel("<html><h1>DED GUI</h1><small>Made by Shivam Paw</small><br /> <br />To use this simple management tool navigate along the tabs above.<br /><br/><br />Report any bugs to shivampaw on the CoCDevForums.</html>");
        home.add(hLabel);
        tabbedPane.addTab("Home",home);
        tabbedPane.setMnemonicAt(0,KeyEvent.VK_1);      


        ignPanel.main(tabbedPane,conn,stat);

        serverPanel.main(tabbedPane, conn, stat);   

        playerList.main(tabbedPane, conn, stat);

        api.main(tabbedPane, conn, stat, builder);



    }

}

And here is the bit of code where I am getting an error.

api.java:

public static void main(JTabbedPane tabbedPane, Connection conn, Statement stat, ImageBuilder builder) throws SQLException, IOException {  


                JPanel editBase = new JPanel();
                Component labe = functions.makeTextPanel("<html><center>You can edit the level of buildings on a players base using this tool. Credit to Sid3way for making the tool.<br /><br/>To get started enter a players ID below.</center></html>");
                editBase.add(labe);
                tabbedPane.addTab("Edit Base",editBase);


            JPanel editidp = new JPanel();
            editBase.add(editidp);
            JTextField editID = new JTextField(3);
            JButton editbtn = new JButton("Submit");
            editidp.add(editID);
            editidp.add(editbtn);

            editbtn.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                String eid = editID.getText();

                                String query = "SELECT  baseJson FROM homes WHERE id='"+eid+"'";
                                String msg;
                                ResultSet rs;
                                try {
                                        rs = stat.executeQuery(query);
                                        rs.next();
                                        String jsonlol = rs.getString("basejson");
                                        System.out.println(jsonlol);
                                        msg = "Success!";
                                } catch (SQLException e) {
                                        msg = "Error fetching players Json. Make sure you entered a valid ID.";
                                        JOptionPane.showMessageDialog(editidp, msg);
                                }

                                if(msg == "Success!"){

                                         JPanel desktop = new JPanel();
                                         desktop.setLayout(new BoxLayout(desktop, BoxLayout.Y_AXIS));
/*ERROR HERE*/                           setContentPane(desktop);
                                         tabbedPane.setSize(new Dimension(654,420));
.....

Thre error is on the second last line. setContentPane(dekstop);

The error I am getting is:

Cannot make a static reference to the non-static method setContentPane(Container) from the type JFrame

I don't know how to fix this because when I had it all in one file it was working fine.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Shiv
  • 33
  • 1
  • 4
  • Instance (non-static) methods work on objects that are of a particular type (the class). setContentPane is non-static you cannot call from a static method. – Viraj Nalawade May 28 '15 at 09:54
  • In Java you compare `String`s using `String::equals`, not using the `==` operator. – Oli May 28 '15 at 09:56
  • Does the class that contains your `main` method extend `JFrame`? – Oli May 28 '15 at 09:58
  • Wait, why do you have a `public static void main` which takes arguments other than `String[]`? That signature should only be used for the entry point of your program. See https://docs.oracle.com/javase/tutorial/getStarted/application/ – Oli May 28 '15 at 10:01
  • @Oli isn't it String.equals ? – Marged May 28 '15 at 10:01
  • @Marged `String` does not have a static method `equals`, which is what your notation states. I was referring to the instance method using the `Class::method` notation. For clarification OP, you compare strings like this: `if (msg.equals("Success!")) ...` – Oli May 28 '15 at 10:03
  • 1
    I'm just getting confused here... – Shiv May 28 '15 at 10:05
  • Did you try looking [here](https://www.google.co.il/search?client=opera&q=Cannot+make+a+static+reference+to+the+non-static+method&sourceid=opera&ie=UTF-8&oe=UTF-8)? – user1803551 May 28 '15 at 10:06
  • @user1803551 yes but i'm not sure how to do that... – Shiv May 28 '15 at 10:09
  • @user1803551 post as answer ;) – Shiv May 28 '15 at 10:12
  • @user1803551 didn't work :( it keeps restarting the program – Shiv May 28 '15 at 10:18
  • @Oli and yours looked like a c++ definition – Marged May 28 '15 at 10:24
  • @Shiv `JFrame`s have the method `setContentPane`. When you have a `JFrame` instance, you can call that method using that instance. eg: `frame.setContentPane(desktop);`. – Oli May 28 '15 at 10:30
  • "*post as answer*" Not until you post an [MCVE](http://stackoverflow.com/help/mcve). – user1803551 May 28 '15 at 19:11
  • @Oli `::` is indeed C++, what you want is `#` - `Class#method`. – user1803551 May 28 '15 at 19:18

0 Answers0