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 4 of 4

Thread: Brand new beginner help with code

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Brand new beginner help with code

    Hi everyone. I just starting learning how to code a few days ago. So far so good!! However I have run into one problem I cannot figure out. Maybe it’s a little to advanced for me atm. So here’s my question and hopefully I get all the terminology correct lol

    I am creating a very basic windows application.
    In my main window I have various buttons calling other Jframes and they work fine however in one Jframe I have a JTabbedPane.

    I can’t seem to figure out how to code a jbutton to open the second tab within the Jtabbedpane in another jframe.

    I’ve learned the basic jbutton to call a jframe
    New jframe1().setVisible(true);

    From what I’ve read online I can use JTabbedPane.setSelectedIndex(1); if the button was within the same jframe as the tabbedpane. But the button is in another jframe.

    I’m sorry if this is in the wrong thread! Will repost in the right one if needed.

    Thank you in advance and sorry for being so new lol

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Brand new beginner help with code

    But the button is in another jframe.
    For code in one class to access code in another class, it must have a reference to the instance of the other class.
       TheClass refToClass = new TheClass();
        ...
       refToClass.someMethod();   // call someMethod in the other class using the reference

    Without seeing the code I can not really suggest what the problem is.

    Be sure to wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Brand new beginner help with code

    Norm,

    I appreciate your response and willingness to help. I did a little more research on classes since your response pointed me in that direction. I don't think there is anything wrong with my code because it's basically non-exsistant for my problem lol. I quickly created a test app with the problem i'm trying to solve since my original app is on my stand-alone work computer.

    Jframe 1 (main Frame) Has two buttons. The first goes to Jframe 2 (second frame) and the second button i want to go to the second tab in the second jframe. (i hope that's clear, i fear that i can't explain myself in java yet)

    I'm using Netbeans IDE if that helps

    Here's my code for JFrame 1

    package com.shawnquigley.testproject;
     
    public class MainFrame extends javax.swing.JFrame {
     
        public MainFrame() {
            initComponents();
        }
     
        private void initComponents() {
     
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
     
            jButton1.setText("Second JFrame");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
            getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 280, -1, -1));
     
            jButton2.setText("Tab in TabbedPane");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
            getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 280, -1, -1));
     
            pack();
        }                   
     
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            new SecondFrame().setVisible(true);
        }                                        
     
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
        }                                        
     
        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 ("Windows".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
     
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new MainFrame().setVisible(true);
                }
            });
        }
     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;



    Here's my code for JFrame 2



    package com.shawnquigley.testproject;
     
    public class SecondFrame extends javax.swing.JFrame {
     
        public SecondFrame() {
            initComponents();
        }
     
        private void initComponents() {
     
            jTabbedPane1 = new javax.swing.JTabbedPane();
            FirstTab = new javax.swing.JPanel();
            SecondTab = new javax.swing.JPanel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
     
            javax.swing.GroupLayout FirstTabLayout = new javax.swing.GroupLayout(FirstTab);
            FirstTab.setLayout(FirstTabLayout);
            FirstTabLayout.setHorizontalGroup(
                FirstTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 535, Short.MAX_VALUE)
            );
            FirstTabLayout.setVerticalGroup(
                FirstTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 342, Short.MAX_VALUE)
            );
     
            jTabbedPane1.addTab("First Tab", FirstTab);
     
            javax.swing.GroupLayout SecondTabLayout = new javax.swing.GroupLayout(SecondTab);
            SecondTab.setLayout(SecondTabLayout);
            SecondTabLayout.setHorizontalGroup(
                SecondTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 535, Short.MAX_VALUE)
            );
            SecondTabLayout.setVerticalGroup(
                SecondTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 342, Short.MAX_VALUE)
            );
     
            jTabbedPane1.addTab("Second Tab", SecondTab);
     
            getContentPane().add(jTabbedPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 70, 540, 370));
     
            pack();
        }                       
     
        /**
         * @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 ("Windows".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(SecondFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(SecondFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(SecondFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(SecondFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
     
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new SecondFrame().setVisible(true);
                }
            });
        }
     
        public javax.swing.JPanel FirstTab;
        public javax.swing.JPanel SecondTab;
        public javax.swing.JTabbedPane jTabbedPane1;
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Brand new beginner help with code

    I'm using Netbeans IDE if that helps
    That is a problem because Netbeans uses none standard classes that are not available to non-Netbeans users.
    For example: org.netbeans.lib.awtextra.AbsoluteConstraints

    Also the IDE builds the contents of the classes, making it hard to modify.

    i want to go to the second tab in the second jframe
    To call a method in another class, you need the reference to an instance of the class.
    Where is the reference to the second jframe class?

    To go to a tab in a JTabbedPane, you need to call a method for that class the tells it what component to go to.
    Normally those methods are named with a name starting with set. Read the API doc to find what method to use.
    Then write a method in the second frame class that calls that method.
    Then in the first frame class, call that method using the reference to the instance of the second class.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. hi, beginner. need help with code
    By kittykat0953 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2014, 06:32 AM
  2. Help with beginner code
    By KSB Dad in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 13th, 2013, 07:31 AM
  3. Beginner code
    By Skybear in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 1st, 2012, 07:15 PM
  4. Beginner - Help with my code.
    By eddross in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2010, 09:30 AM