Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Passing dat from Jbutton to another class

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Passing dat from Jbutton to another class

    Hello everyone
    I've a problem. I would like to send string workData from class MamboUi. jButton1ActionPerformed to class showData.showData where it will be worked

    This is my code

    public class MamboUI extends javax.swing.JFrame {
     
        public String anno;
        public String mese;
        public String workData;
     
        public MamboUI() {
            initComponents();
         }
    [...]
     public void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
            String anno = (String) comboAnno.getSelectedItem();
            String mese = (String) comboMese.getSelectedItem();
            workData = anno + " " +mese;
            showData t = new showData(); //Indirizzo la Classe = classe.java
            t.joe();                     //indirizzo l'entry point
           } 
     
    [...]
    }

    public class showData  extends JPanel{
     
        static final String USERNAME = "";
        static final String PASSWORD = "";
        static final String CONN_STRING = "jdbc:mysql://localhost:3306/mambo";
        String workData ="";
     
     
        public static void joe() {
            MamboUI dataFrom = new MamboUI();
     
            JFrame jf = new JFrame();
            showData t = new showData();
            jf.setTitle("Test");
            jf.setSize(1350,400);
            jf.setVisible(true);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.add(t);
        }
     
        JTable jt;
        public showData () {
                MamboUI dataFrom = new MamboUI(this);
               workdata ???  <-------
                try {
                [....]
     
                DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
                DefaultTableModel NomeGiorni = new DefaultTableModel(colonne, 0);
                Connection conn;
                conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
                Statement stmt = (Statement) conn.createStatement();
                String query = "Select * from alldata where anno = "+anno+" and mese = "+mese+"";
                ResultSet rs = stmt.executeQuery(query);
               [...]
    }

    Thank you in advance
    paps

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Passing dat from Jbutton to another class

    Can you pass the value via an argument to the joe method?

    Note: do not create a instance of a class to call one of its static methods. Just use the name of the class:
    ShowData.joe() // Call static method joe in the ShowData class
    (Also Class names should start with uppercase letters)
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    paps (July 27th, 2020)

  4. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Passing dat from Jbutton to another class

    Adopted this solution (in the meanwhile rewrote the receiving class)

    in main class (MamboUI)

    public void jButton1ActionPerformed(ActionEvent evt) {                                         
     
            Choice();
        }                                        
     
     
        private void Choice()
        {
            String anno = (String) comboAnno.getSelectedItem();
            String mese = (String) comboMese.getSelectedItem();
            workData = anno + " " +mese;
            displayResult t = new displayResult(workData);
            t.joe(workData);
     
        }

    and in the receiving class (displayResult)



    public static void joe(String workData){  
     
            displayResult frame = new displayResult(workData);
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.setSize(1200, 500);
            frame.setLocation(80,130);
            frame.setVisible(true);
        }
        public displayResult(String workData)
        {
            super("Gruppo C - Mambo");
            ....
       }

    it works like a charme

    paps

Similar Threads

  1. Passing Double Value from Class to Class
    By Deployment in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 6th, 2017, 12:15 PM
  2. Passing variable from one class to another
    By kingsta in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 16th, 2013, 09:57 AM
  3. Passing a value to another class
    By Semple in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 05:49 PM
  4. [SOLVED] Class not passing information
    By DiPep in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 26th, 2010, 10:04 AM
  5. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM

Tags for this Thread