3

When i'm trying to call a method from JPanel2 in my JPanel4, it wont show any changes in the JFrame after the press of a button, but when i tried putting the button in JPanel2, everything worked fine. So my question is, as i stated in the title - Did i create a duplicate JPanel? I'm fairly new to java, so any help is appreciated.

Main

package tellimine;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;


public class Tellimine extends JFrame{

public Tellimine(){
      super("Tellimine");
}

public void looFrame() throws Exception{

   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   this.setPreferredSize(new Dimension(800, 310));
   this.setLayout(new GridBagLayout());
   GridBagConstraints c = new GridBagConstraints();


    JPanel2 panel2 = new JPanel2();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(0,0,0,0);
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 1;
    this.add(panel2, c);


    JPanel4 panel4 = new JPanel4();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 0;
    c.weighty = 1.0;
    c.anchor = GridBagConstraints.PAGE_END;
    c.gridx = 0;
    c.gridwidth = 2;
    c.gridy = 3;
    this.add(panel4, c);

    this.setVisible(true);
    this.pack();

}

public static void main(String[] args) throws Exception{

    Class.forName("cubrid.jdbc.driver.CUBRIDDriver");

   final Tellimine raam = new Tellimine();
    EventQueue.invokeLater(new Runnable() {
        @Override
    public void run() {
            try {
                raam.looFrame();
            } catch (Exception ex) {
                Logger.getLogger(Tellimine.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
         });
   }
}

JPanel2

package tellimine;

import java.awt.GridLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class JPanel2 extends JPanel{

private Connection conn;
Integer suurus;
JLabel tellimus, tk_nr;
JFormattedTextField tellija, kuupaev;
Statement stmt = null;
ResultSet rs = null;

public JPanel2() throws SQLException{
    super();
 panel2();
}


    public static Connection connect() {

    Connection conn = null;

    try{    
        conn = DriverManager.getConnection("jdbc:cubrid:localhost:33000:Access::","dba","qwerty");
    }
    catch ( Exception e ) {

       System.err.println("SQLException : " + e.getMessage());
    }
    return conn;
}

public void panel2() throws SQLException{

    conn=connect();
    stmt = conn.createStatement();
    rs = stmt.executeQuery("SELECT * FROM yldandmed");
    rs.next();
    tellimus = new JLabel();
    tk_nr = new JLabel();
    tellija = new JFormattedTextField();
    kuupaev = new JFormattedTextField();

    yldandmed();

    this.setLayout(new GridLayout(3,5));
    this.add(new JLabel("Tellimuse ID"));
    this.add(tellimus);
    this.add(new JLabel("Tellija:"));
    this.add(tellija);
    this.add(new JLabel(""));
    this.add(new JLabel("Sissekandekuupäev"));
    this.add(kuupaev);
    this.add(new JLabel("Töökäsu nr:"));
    this.add(tk_nr);
    this.add(new JLabel(""));
    this.add(new JLabel("Tellimise Detailid"));
    this.add(new JLabel(""));
    this.add(new JLabel(""));
    this.add(new JLabel(""));

}

public void yldandmed() throws SQLException{

    tellimus.setText(rs.getString("tel_id"));
    System.out.println(rs.getString("tellija"));
    tellija.setText(rs.getString("tellija"));
    kuupaev.setText(rs.getString("kuupaev"));
    tk_nr.setText(rs.getString("tk_nr"));
  }
}

JPanel4

package tellimine;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JPanel;


public class JPanel4 extends JPanel{

JPanel2 paneel = new JPanel2();

    public JPanel4() throws Exception{
    super();
 panel4();
}

    private void panel4() throws SQLException{

        JButton uuenda = new JButton("<");
        this.add(uuenda);
        uuendanupp uuendus = new uuendanupp();
        uuenda.addActionListener(uuendus);

        JButton lisa = new JButton(">");
        this.add(lisa);
        lisanupp lisamine = new lisanupp();
        lisa.addActionListener(lisamine);       
    }



    private class uuendanupp implements ActionListener{

        public void actionPerformed(ActionEvent e){

            System.out.println("<");

        }

}
    private class lisanupp implements ActionListener{

        public void actionPerformed(ActionEvent e){
            try {   
                paneel.rs.next();
                paneel.yldandmed();
                System.out.println(">");    
            } catch (SQLException ex) {
                Logger.getLogger(JPanel4.class.getName()).log(Level.SEVERE, null, ex);
            }

        }        
   }
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138

1 Answers1

4
public class JPanel4 extends JPanel{

JPanel2 paneel = new JPanel2();

Here you are creating new instance of JPanel2. You should instead pass the JPanel2 instance created in the main method to JPanel4 via the constructor or via a settor method.

Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19
  • Would you be so kind to show me how to pass it using constructor? I've never done it before. And thanks for the fast answer! – user1773940 Oct 25 '12 at 13:14
  • 2
    public JPanel4() should be changes to public JPanel4(JPanel2 panel2) and when creating JPanel4 you should call it as JPanel4(jpanel2). Inside JPanel2 have a member jpanel2 that is initialized in the constructor and use that where you need instance of jpanel2. – Ashwinee K Jha Oct 25 '12 at 13:24
  • I'm sure it's a valid explanation, but as a beginner i don't really understand. Maybe you could show how to do it in my code? Hope it's not too much to ask for and sorry for bothering You again. – user1773940 Oct 26 '12 at 06:49
  • 2
    @user1773940, what AKJ means is this http://pastebin.com/9bJKFMX0. Let me know if you have any question. – esengineer Oct 26 '12 at 07:37