Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Fubarable

Thread: jTabbedPane1StateChanged update the jLabel text from another class

  1. #1
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default jTabbedPane1StateChanged update the jLabel text from another class

    Hello im stock again in my project and i need help.

    im trying to update the text of jLabel1 from NewJPanel1.java using jLabel1.setText using the jTabbedPane1StateChanged from NewJFrame.java see my code i hope its easy to understand the problem.

    //Code from NewJFrame.java
    Java Code:
     private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {                                          
    int currentTab = jTabbedPane1.getSelectedIndex();
    switch(currentTab){
        case 0:
            currtab = 0;
            new NewJPanel1("Tab1");
            break;
        case 1:
            currtab = 1;
            new NewJPanel1("Tab2");
            break;
        case 2:
            currtab = 2;
            new NewJPanel1("Tab3");
            break;
        case 3:
            currtab = 3;
            new NewJPanel1("Tab4");
            break;
        case 4:
            currtab = 4;
            new NewJPanel1("Tab5");
            break;
    }
        }
    //Code from NewJPanel1
    Java Code:
    public class NewJPanel1 extends javax.swing.JPanel {
    
        public NewJPanel1(String curTabText){ 
            System.out.println(curTabText); //<- this is working it prints the text
          jLabel1.setText(curTabText); //<- this one cause the errors
        }
           public NewJPanel1() {
            initComponents();
            
           }
        @SuppressWarnings("unchecked")
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at test.NewJPanel1.<init>(NewJPanel1.java:8)
    at test.NewJFrame.jTabbedPane1StateChanged(NewJFrame. java:137)
    at test.NewJFrame.access$100(NewJFrame.java:18)
    at test.NewJFrame$2.stateChanged(NewJFrame.java:59)
    at javax.swing.JTabbedPane.fireStateChanged(JTabbedPa ne.java:400)
    at javax.swing.JTabbedPane$ModelListener.stateChanged (JTabbedPane.java:253)
    at javax.swing.DefaultSingleSelectionModel.fireStateC hanged(DefaultSingleSelectionModel.java:116)
    at javax.swing.DefaultSingleSelectionModel.setSelecte dIndex(DefaultSingleSelectionModel.java:50)
    at javax.swing.JTabbedPane.setSelectedIndexImpl(JTabb edPane.java:599)
    at javax.swing.JTabbedPane.setSelectedIndex(JTabbedPa ne.java:574)
    at javax.swing.JTabbedPane.insertTab(JTabbedPane.java :710)
    at javax.swing.JTabbedPane.addTab(JTabbedPane.java:77 7)
    at test.NewJFrame.initComponents(NewJFrame.java:64)
    at test.NewJFrame.<init>(NewJFrame.java:25)
    at test.NewJFrame$3.run(NewJFrame.java:166)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    thanks..

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Jhovarie View Post

    //Code from NewJFrame.java
    Java Code:
     private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {                                          
    int currentTab = jTabbedPane1.getSelectedIndex();
    switch(currentTab){
        case 0:
            currtab = 0;
            new NewJPanel1("Tab1");
            break;
        case 1:
            currtab = 1;
            new NewJPanel1("Tab2");
            break;
        case 2:
            currtab = 2;
            new NewJPanel1("Tab3");
            break;
        //.... etc...
    Not sure what you're trying to do here as you don't seem to do anything with the NewJPanel1 that you create. Also, what's with all the switch/case statements? Wouldn't it be easier to simply write:

    Java Code:
    currtab = jTabbedPane1.getSelectedIndex();
    new NewJPanel1("Tab" + (currtab + 1));

    And regarding:
    //Code from NewJPanel1
    Java Code:
          jLabel1.setText(curTabText); //<- this one cause the errors
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at test.NewJPanel1.<init>(NewJPanel1.java:8)
    This error is telling you that jLabel1 is obviously null at this location. So look in the rest of your code to figure out why.

  3. #3
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    I add jLabel1 = new JLabel(); before the jLabel1.setText(curTabText); error message is gone but jLabel1 doest not update the text.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    You can't just randomly create a new component.

    Yes the NPE is gone but you didn't add the label to the GUI so you are setting the text of an undisplayed component.

    Your ChangeListener code needs access to the label that you added to the GUI. Presumably you have code somewhere the create a label and a tabbed pane and added both of those components to the GUI. That is the label you need to reference.

  5. #5
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    can any one help me from this code? i create new short sample but still not working see my codes.

    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import java.awt.event.*;
    import javax.swing.JLabel;
    import javax.swing.JTabbedPane;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    public class Frame1 extends JFrame{
        private JFrame m2;
        private JButton btn;
        private JTabbedPane tab;
        public Frame1(){
            setTitle("Frame1");
            setLayout(new java.awt.BorderLayout());
      
            tab = new JTabbedPane();
            tab.addTab("Tab1",new JLabel("Tab1"));
            tab.addTab("Tab2",new JLabel("Tab2"));
            tab.addTab("Tab3",new JLabel("Tab3"));
            add(tab,BorderLayout.CENTER);
            tab.addChangeListener(new ChangeListener(){
                    public void stateChanged(ChangeEvent evt){
                        int currtab = tab.getSelectedIndex();
                        System.out.println("Frame1: "+currtab); //<- it works
                       new myPanel(currtab); 
                        //new myPanel(new JLabel(),currtab);
                    }
            });
            
            m2 = new Frame2(); 
            m2.setBounds(300,20,400,300);
            
         btn = new JButton("Show Frame2");
         add(btn,BorderLayout.SOUTH);
          btn.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
           m2.setVisible(true);
          }
          }
         ); 
           
          setVisible(true);
          setBounds(250,10,500,400);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        public static void main(String[]args){
            new Frame1();
        }
    }
    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Frame2 extends JFrame{
        private JPanel pan;
        public Frame2(){
            setAlwaysOnTop(true);
            setLayout(new java.awt.BorderLayout());
            pan = new myPanel();
            add(pan,BorderLayout.CENTER); 
        }
    }
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class myPanel extends JPanel{
        private JLabel label = new JLabel("Test jLabel changed");
      //private JLabel label;  //<- not working
        /*
         * this constructor still not working
        public myPanel(JLabel l,int i){
            label = l;
            System.out.println("myPanel: "+i);
            //label = new JLabel(""+i); //<- not working
            label.setText(String.valueOf(i)); //<- not working
        }
         */
       public myPanel(int i){
            System.out.println("myPanel: "+i);
            //label = new JLabel(""+i); //<- not working
            label.setText(String.valueOf(i)); //<- not working
        }
            
        public myPanel(){
           setBackground(Color.ORANGE);
           setLayout(new java.awt.BorderLayout());
           add(label,BorderLayout.NORTH);
        }
    }
    im trying to update the text of label using label.setText from myPanel.java using stateChanged of tabPane in Frame1.java
    Last edited by Jhovarie; 08-19-2011 at 05:38 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    What is your goal with this code? Your problem is likely one of misplaced reference -- you can only change the state of an object if you have a reference to that object. You cannot change the state of a JLabel unless you have a reference to it, and your code above appears to lack appropriate references where needed.

    I suggest that you put down GUI coding for a bit and go through a basic intro text on Java cover to cover to get a better handle on this and other core concepts. You won't regret doing this, trust me.
    Jhovarie likes this.

  7. #7
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What is your goal with this code?
    my problem is to update the text of the label from myPane.java but now its ok was solve here with i done.

    //code from my Frame.java
    Java Code:
    final myPanel pn = new myPanel();
            tab.addChangeListener(new ChangeListener(){
                    public void stateChanged(ChangeEvent evt){
                        int currtab = tab.getSelectedIndex();
                        System.out.println("Frame1: "+currtab); //<- it works 
                        pn.label.setText(" Tab: "+currtab); //<- it is now working.
                    }
            });

    //code from myPanel.java
    Java Code:
    public static JLabel label = new JLabel("Test jLabel changed");

Similar Threads

  1. Problem about JLabel not update in JPanel
    By fantasyme in forum Java Applets
    Replies: 11
    Last Post: 10-16-2011, 08:00 AM
  2. update text output (eg. jLabel) during calculations
    By kosmo76 in forum AWT / Swing
    Replies: 2
    Last Post: 04-06-2011, 11:30 PM
  3. update a jlabel
    By niba10 in forum AWT / Swing
    Replies: 3
    Last Post: 04-04-2011, 08:43 PM
  4. Update JLabel during loop automatically
    By carnado2008 in forum AWT / Swing
    Replies: 4
    Last Post: 01-22-2011, 03:55 PM
  5. Jlabel update problem
    By fantasyme in forum AWT / Swing
    Replies: 3
    Last Post: 04-14-2010, 06:10 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
  •