Results 1 to 8 of 8
  1. #1
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Using jtextfield on jframe (Swing) to enter search terms in a different class

    I have a swing jFrame set up with a control group which contains 3 radio buttons.
    when one is clicked it sends its label to a jtextfield

    in a different class i have code parsing text by a term

    Java Code:
            Scanner s = null;
    
            try {
                s = new Scanner(new BufferedReader(new FileReader("/Stage_One.txt")));
                s.useDelimiter("\\*");
                String total = (s.next());
                Pattern p = Pattern.compile("\\n[\\n]+");
    
                String[] parts = p.split(total);
                boolean searchArrayResult = false;
                java.io.File targetFile = new java.io.File("/Stage_Two.txt");
                java.io.FileWriter filew = new java.io.FileWriter(targetFile);
                java.io.PrintWriter print = new java.io.PrintWriter(filew, true);
                for (String str : parts) {
                    searchArrayResult = str.contains("THIS IS WHERE I WANT THE RESULT OF THE RADIO BUTTON GROUP");
                    if (searchArrayResult) {
                        
                        print.println(str + "\n");
                        //System.out.println(str + "\n");
                    }
                }
                while (s.hasNext()) {
                }
                 
            } catch (Exception ex) {
            }
        }
    
    }
    I just need to know how to access the jframe variables (textfields, panels and such) so i can use their inputs in code to filter a text file.
    Thanks in advance.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Using jtextfield on jframe (Swing) to enter search terms in a different class

    Pass them into a method or constructor.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Using jtextfield on jframe (Swing) to enter search terms in a different class

    Quote Originally Posted by KevinWorkman View Post
    Pass them into a method or constructor.
    thats the part I cant figure out. I know i need to call in the jframe to this class, but i dont know how to do that. i have been searching google and most results are for jFrame to jFrame.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Using jtextfield on jframe (Swing) to enter search terms in a different class

    You said you are passing the text of a button's label. Since you only have three buttons are you only allowing to filter on three
    items? If so, you don't need to pass to a text field. Just have the action listener for the button use the action command as the desired text (which should be set to the same text as the label). You can assign the text to an instance field of your class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Using jtextfield on jframe (Swing) to enter search terms in a different class

    How are you calling this code from your gui? Can you post an MCVE?

    Step 1: Create a class that contains a single method- that method takes a single String argument and prints it to the console.

    Step 2: Create a GUI that contains a text box and a button. When you press that button, take whatever's in the text box and pass it to the class from step 1.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  6. #6
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Using jtextfield on jframe (Swing) to enter search terms in a different class

    Quote Originally Posted by jim829 View Post
    You said you are passing the text of a button's label. Since you only have three buttons are you only allowing to filter on three
    items? If so, you don't need to pass to a text field. Just have the action listener for the button use the action command as the desired text (which should be set to the same text as the label).
    OK, i think i get what you said, you are cutting out the need for a jTextfield, which is great

    You can assign the text to an instance field of your class.

    IF this is the part where you are telling me how to pass the text, from the choice from the radio buttons, i dont know what you mean by the instance field of the class, or how to do it.

  7. #7
    mrlarue77 is offline Member
    Join Date
    Nov 2014
    Posts
    18
    Rep Power
    0

    Default Re: Using jtextfield on jframe (Swing) to enter search terms in a different class

    Step 1

    Java Code:
    package learnin;
    
    /**
     *
     * @author leighlarue
     */
    public class Learnin {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            String learn = "New text will go here";
             System.out.println(learn);
        }
      }
    step 2

    Java Code:
    package learnin;
    
    /**
     *
     * @author leighlarue
     */
    public class NewJFrame extends javax.swing.JFrame {
    
        /**
         * Creates new form NewJFrame
         */
        public NewJFrame() {
            initComponents();
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jTextField1 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jTextField1.setText("jTextField1");
    
            jButton1.setText("jButton1");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(69, 69, 69)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(124, 124, 124)
                            .addComponent(jButton1)))
                    .addContainerGap(102, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(80, 80, 80)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(jButton1)
                    .addContainerGap(145, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            String newString = jTextField1.getText();// TODO add your handling code here:
        }                                        
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JTextField jTextField1;
                     
    }
    sorry or long code, was generated by netBeans.

    and i cant complete the second part of step 2, as that was my original question, I dont know how to call from the jFrame to the method.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: Using jtextfield on jframe (Swing) to enter search terms in a different class

    In your first step, you shouldn't be just printing out a hard-coded String. Write a method that takes a String as a parameter, then print that String out. That class should not contain a main method.

    In your second step, create an instance of the class from the first step. Then pass in the text from the JTextField.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Replies: 0
    Last Post: 07-16-2012, 08:27 AM
  2. JTextField on JPanel-class within JFrame-class
    By floris in forum AWT / Swing
    Replies: 5
    Last Post: 06-25-2012, 01:54 PM
  3. Replies: 0
    Last Post: 12-02-2011, 05:56 PM
  4. Replies: 3
    Last Post: 11-14-2011, 06:13 AM
  5. Replies: 0
    Last Post: 08-30-2011, 10:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •