How  add buttons to Jframe from other class

Hi, a have a little problem;)
I want make a JFrame, but i want to add JButtons from other class. Now I have something like that:
My JFrame class:
package windoow;
import javax.swing.*;
import java.awt.*;
public class MyWindow extends JFrame {
       Butt buttons ;
        public Window(String s) {
                 super(s);
                this.setSize(600, 600);
                this.setVisible(true);
                Buttons();
            public void Buttons(){
                setLayout(null);            
               buttons = new Butt();
               buttons.mywindow =this;
} My class with buttons:
package windoow;
import javax.swing.JButton;
public class Butt {
        MyWindow mywindow;
        JButton b1;
        Butt(){
                  b1 = new JButton("Button");
                  b1.setBounds(100,100,100,20);
                  mywindow.add(b1);  
} and i have NullPointerException.
When i try to make new MyWindow in Butt clas i have something like StackOverFlow.
what should i do?

Your null pointer exception is occuring because, in your Butt() constructor, you are calling mywindow.add(b1), but you don't set mywindow until after you call the constructor in the MyWindow::Buttons() method.
And, if you try to create a new MyWindow() in your Butt() constructor, and, assuming that you are calling the MyWindow::Window(String s) method from the MyWindow() constructor (which is not clear from the code fragment you posted), then Butt() calls new MyWindow() which calls Window(String s) which calls Butt() which calls ... and so on, until the stack overflows.
One possible solution ... pass your MyWindow reference into your Butt() constructor as this, as so:
public void Buttons()
   // stuff deleted
  buttons = new Butt( this );
   // stuff deleted
// AND
Butt( MyWindow mw )
   // stuff deleted
  mywindow = mw;
  mywindow.add( b1 ); // b1 created in stuff deleted section  
}Also, I would call setVisible(true) as the last thing I did during the construction of the frame. This realizes the frame, and from that point forward, most changes to the frame should be made on the event thread.
¦ {Þ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

  • Add/Remove datagridview event from other class

    Hi,
    I have a 'Class A' which have a datagridview object with cellvaluechanged event. i pass this datagridview as byreference to 'Class B'.
    i need to remove/add the cellvaluechanged event of given datagridview object from 'Class B'.
    below is my code
    Thanks and Regards
    Kasim

    Hi Thangam-Kasim,
    If you want to add/remove the event of DataGridView in ClassB.
    You need to add a reference of Class A in Class B.
    I supposed the ClassA is Form1, The Class B is Form2. in Form2, there is a method called SetDataSource() like yours.
    Then you could add/remove the event like below.
    Public Sub SetDataSource(ByVal dt As DataTable, ByRef DGView As DataGridView)
    Dim dgvrow As DataGridViewRow
    If DGView.Rows.Count > 0 Or DGView.DataSource IsNot Nothing Then
    DGView.Rows.Clear()
    End If
    Dim form1 As Form1 = Application.OpenForms.OfType(Of Form1)().FirstOrDefault()
    RemoveHandler DGView.CellValueChanged, AddressOf form1.DataGridView1_CellValueChanged
    AddHandler DGView.CellValueChanged, AddressOf form1.DataGridView1_CellValueChanged
    End Sub
    In Form1, you need to set the DataGridView1_CellValueChanged method to Public.
    If you have any other concern regarding this issue, please feel free to let me know.
    Best regards,
    Youjun Tang
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • How to access variables from other class

        public boolean inIn(Person p)
            if  (name == p.name && natInsceNo == p.natInsceNo && dateOfBirth == p.dateOfBirth)
                 return true;
            else
                 return false;
        }//returns true if Person with same name/natInsceNo/dateOfBirth as phello,
    here am trying to compare the existing object with another object.
    could you please tell me how to access the variables of other class because i meet this error?
    name cannot be resolved!
    thank you!

    public class Person
        protected String name; 
        protected char gender;      //protected attributes are visible in the subclass
        protected int dateOfBirth;
        protected String address;
        protected String natInsceNo;
        protected String phoneNo;
        protected static int counter;//class variable
    //Constractor Starts, (returns a new object, may set an object's initial state)
    public Person(String nme,String addr, char sex, int howOld, String ins,String phone)
        dateOfBirth = howOld;
        gender = sex;
        name = nme;
        address = addr;
        natInsceNo = ins;
        phoneNo = phone;
        counter++;
    public class Store
        //Declaration of variables
        private Person[] list;
        private int count;
        private int maxSize;
    //constructor starts
        public Store(int max)
             list = new Person[max];//size array with parameters
             maxSize = max;
             count = 0;
        }//end of store
    //constructor ends
    //accessor starts  
        public boolean inIn(Person p)
           return (name == p.name && address == p.address && natInsceNo == p.natInsceNo);
        }//returns true if Person with same name/natInsceNo/dateOfBirth as phope it helps now!

  • Delete scroller from other class

    Hi.
    I have FlashSite class where after clicking to button i see page with text and with scroll.
    If i click to 'close' button i want removeChild this page, text and scroll.
    But for scroll i use code from other class. (i insert this class like: import classes.Scroller.*;)
    So, how i can remove this scroller?
    code:
    public function lilyContent():void
                   // create a sample pane
                   addChild(lilyPageScroll);
                   lilyPageScroll.x = 360;
                   lilyPageScroll.y = 400;
                   // then attach a new scroller to the pane
                   var lilyScroller = new Scroller(lilyPageScroll, lilyPageScroll.width, 540, new scrollerFace(),"vertical");
    i use this line:
    var lilyScroller = new Scroller(lilyPageScroll, lilyPageScroll.width, 540, new scrollerFace(),"vertical");
    to add scroller.
    all code for FlashSite class:
    package
         import flash.display.*;
         import flash.text.*;
         import flash.events.*;
         import classes.Scroller.*;
         public class FlashSite extends MovieClip 
              var lilyDeskBack:LilyDescriptionBackground = new LilyDescriptionBackground();
              var lilyPageScroll = new LilyContent();
              //constructor
              public function FlashSite()
                   stop();
                   addEventListener(Event.ENTER_FRAME, flashSiteLoading);
              public function flashSiteLoading(event:Event)
                   var mcBytesLoaded:int=this.root.loaderInfo.bytesLoaded;
                   var mcBytesTotal:int=this.root.loaderInfo.bytesTotal;
                   var mcKLoaded:int=mcBytesLoaded/1024;
                   var mcKTotal:int=mcBytesTotal/1024;
                   flashSiteLoading_txt.text="Loading: "+mcKLoaded+"Kb of "+mcKTotal+"Kb";
                   if (mcBytesLoaded>=mcBytesTotal)
                        removeEventListener(Event.ENTER_FRAME, flashSiteLoading);
                        gotoAndPlay('welcomeSite');
                        lilyBtn.addEventListener(MouseEvent.CLICK, lilyDescription);
                        roseBtn.addEventListener(MouseEvent.CLICK, roseDescription);
                        jasmineBtn.addEventListener(MouseEvent.CLICK, jasmineDescription);
                        irisBtn.addEventListener(MouseEvent.CLICK, irisDescription);
              public function lilyDescription(event:MouseEvent):void
                   trace("Lily description goes here.");
                   roseBtn.visible = false;
                   jasmineBtn.visible = false;
                   irisBtn.visible = false;
                   addChild(lilyDeskBack);
                   lilyDeskBack.x = 350;
                   lilyDeskBack.y = 330;
                   lilyContent();
                   lilyDeskBack.LilyDesckClose_btn.addEventListener(MouseEvent.CLICK, closeLilyDescription);
              public function closeLilyDescription(event:MouseEvent):void
                   trace("close Lily description");
                   removeChild(lilyDeskBack);
                   removeChild(lilyPageScroll);
                   lilyScroller.visible = false;
                   roseBtn.visible = true;
                   jasmineBtn.visible = true;
                   irisBtn.visible = true;
              public function roseDescription(event:MouseEvent):void
                   trace("Rose description goes here.");
              public function jasmineDescription(event:MouseEvent):void
                   trace("Jasmine description goes here.");
              public function irisDescription(event:MouseEvent):void
                   trace("Iris description goes here.");
              public function lilyContent():void
                   // create a sample pane
                   addChild(lilyPageScroll);
                   lilyPageScroll.x = 360;
                   lilyPageScroll.y = 400;
                   // then attach a new scroller to the pane
                   var lilyScroller = new Scroller(lilyPageScroll, lilyPageScroll.width, 540, new scrollerFace(),"vertical");
    Thanks for help

    Make the lilyScroller object a class level object.
    Add to 'closeLilyDescription' method 'removeChild(lilyScroller);'
    package
         import flash.display.*;
         import flash.text.*;
         import flash.events.*;
         import classes.Scroller.*;
         public class FlashSite extends MovieClip 
              var lilyDeskBack:LilyDescriptionBackground = new LilyDescriptionBackground();
              var lilyPageScroll = new LilyContent();
              var lilyScroller;
              //constructor
              public function FlashSite()
                   stop();
                   addEventListener(Event.ENTER_FRAME, flashSiteLoading);
              public function flashSiteLoading(event:Event)
                   var mcBytesLoaded:int=this.root.loaderInfo.bytesLoaded;
                   var mcBytesTotal:int=this.root.loaderInfo.bytesTotal;
                   var mcKLoaded:int=mcBytesLoaded/1024;
                   var mcKTotal:int=mcBytesTotal/1024;
                   flashSiteLoading_txt.text="Loading: "+mcKLoaded+"Kb of "+mcKTotal+"Kb";
                   if (mcBytesLoaded>=mcBytesTotal)
                        removeEventListener(Event.ENTER_FRAME, flashSiteLoading);
                        gotoAndPlay('welcomeSite');
                        lilyBtn.addEventListener(MouseEvent.CLICK, lilyDescription);
                        roseBtn.addEventListener(MouseEvent.CLICK, roseDescription);
                        jasmineBtn.addEventListener(MouseEvent.CLICK, jasmineDescription);
                        irisBtn.addEventListener(MouseEvent.CLICK, irisDescription);
              public function lilyDescription(event:MouseEvent):void
                   trace("Lily description goes here.");
                   roseBtn.visible = false;
                   jasmineBtn.visible = false;
                   irisBtn.visible = false;
                   addChild(lilyDeskBack);
                   lilyDeskBack.x = 350;
                   lilyDeskBack.y = 330;
                   lilyContent();
                   lilyDeskBack.LilyDesckClose_btn.addEventListener(MouseEvent.CLICK, closeLilyDescription);
              public function closeLilyDescription(event:MouseEvent):void
                   trace("close Lily description");
                   removeChild(lilyDeskBack);
                   removeChild(lilyPageScroll);
                   removeChild(lilyScroller);
                   lilyScroller.visible = false;
                   roseBtn.visible = true;
                   jasmineBtn.visible = true;
                   irisBtn.visible = true;
              public function roseDescription(event:MouseEvent):void
                   trace("Rose description goes here.");
              public function jasmineDescription(event:MouseEvent):void
                   trace("Jasmine description goes here.");
              public function irisDescription(event:MouseEvent):void
                   trace("Iris description goes here.");
              public function lilyContent():void
                   // create a sample pane
                   addChild(lilyPageScroll);
                   lilyPageScroll.x = 360;
                   lilyPageScroll.y = 400;
                   // then attach a new scroller to the pane
                   lilyScroller = new Scroller(lilyPageScroll, lilyPageScroll.width, 540, new scrollerFace(),"vertical");

  • Calling a object of class from other class's function with in a package

    Hello Sir,
    I have a package.package have two classes.I want to use object of one class in function of another class of this same package.
    Like that:
    one.java
    package co;
    public class one
    private String aa="Vijay";  //something like
    }main.java:
    package co;
    import java.util.Stack;
    public class main extends Stack
    public void show(one obj)
    push(obj);
    public static void main(String args[])
    main oo=new main();
    }when I compile main class, Its not compile.
    Its give error.can not resolve symbol:
    symbol: class one
    location: class co.main
    public void show(one obj)
                              ^Please help How that compile "Calling a object of class from other class's function with in a package" beacuse I want to use this funda in an application

    kumar.vijaydahiya wrote:
    .It is set in environment variable.path=C:\bea\jdk141_02\bin;.,C:\oraclexe\app\oracle\product\10.2.0\server\bin;. command is:
    c:\Core\co\javac one.javaIts compiled already.
    c:\Core\co\javac main.javaBut it give error.
    Both java classes in co package.Okay, open a command prompt and execute these two commands:
    // to compile both classes:
    javac -cp c:\Core c:\Core\co\*.java
    // to run your main-class:
    java -cp c:\Core co.main

  • .How to instantiate the innerclass from another class with coded eg.

    How to instantiate the innerclass from another class(both for static & non static) please give me an eg with coding.

    It's just a preference, but I like writing factory methods:
    public class Outer {
        public class Inner {}
        public static class StaticInner {}
        public Inner innerInstance() {
            return new Inner();
        public static StaticInner staticInnerInstance() {
            return new StaticInner();
        public static void main(String[] args) {
            Outer.StaticInner si = Outer.staticInnerInstance();
            Outer outer = new Outer();
            Outer.Inner i = outer.innerInstance();
    }Often, for me, the inner class implements an interface, and the factory method
    lets you hide the implementation class:
    public class Outer {
        private class Inner implements Runnable {
            public void run() {}
        public Runnable runnerInstance() {
            return new Inner();
        public static void main(String[] args) {
            Outer outer = new Outer();
            Runnable r = outer.runnerInstance();
    }

  • Making object accesible from other class?

    hi all
    how can i have an object of class to be accesible from other class?
    thanks

    It's not clear what you mean by "object of class". Do you mean fields within an object. Do you mean using an object of one class within the code of another?
    For most named objectes in java, whether field, method or class, there are four levels of access.
    public means accessible from anywhere in the program
    private means accessible only from the same source file.
    protected means only from the class, or a class that extends it.
    or if you don't put any of these keywords it means accessible only from the same package, that is set of source files in a single directory.

  • Method not accessible from other classes

    Hi,
    I ve defined a class and would like to create an instance of it from another class. That works fine, I am also able to access class variables. However the class method "calcul" which is defined as following, is not accessible from other classes:
    class Server {
    static String name;
    public static void calcul (String inputS) {
    int length = inputS.length();
    for (int i = 0 ; i < length; i++) {
    System.out.println(newServer.name.charAt(i)); }
    If I create an instant of the class in the same class, the method is then available for the object.
    I am using JBuilder, so I can see, which methods and variables are available for an object. Thanks for your help

    calcul is a static method, that means you do not need an instance of server to run this method. This method is also public, but your class Server is not, your Server class is package protected. So only classes within the same package has Server can use its method. How to use the calcul method?// somewhere in the same package as the Server class
    Server.calcul( "toto" );

  • How to reject ACK/NACK from other Send Ports to Orchestration

    Hi Everyone,
    I have a below scenario, Please let me your ideas how can we avoid ACK from other send ports (If there is any failures), in my current scenario getting issues or conflicts because of this
    AckRequired.
    Key points in my scenarios
    SendPort_A is pointing to SAP
    SendPort_B is pointing to SQL
    Flow
    Receive MessageA and Transform to MessageB format, send MessageB to SendPort_A which directly bind to orchestration with transmitted property enabled.
    SendPort_B subscribes the same message, output of map with message type as filter condition (we are not expecting any Acknowledge from sendPort_B, because it’s a generic send port for some scenario).
    Problem
    When I see the context properties both send ports has AckRequired
    Property to true (so getting ACK/NACK from both send ports).
    I want acknowledgement only form SendPort_A which is bind to orchestration with transmitted property enabled, but not from the SendPort_B which subscribes just with message type.
    I have tried the below points but no luck
    Created custom pipeline with custom pipeline component to set / overwrite
    AckRequired Property to false (Pipeline component is working fine, but nothing is overwriting in context, getting acknowledge back to orchestration for any failure in SendPort_B)
    #region IComponentUI
    public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
    pInMsg.Context.Write("AckRequired", "http://schemas.microsoft.com/BizTalk/2003/system-properties", false);
    return pInMsg;
    #endregion
    Below images gives some info on the same scenario.
    Appreciate your suggestions.
    Thanks, SMS Vikas K

    Hi Vikas,
    Let me know if I understood correctly.
    SendPort_B subscribes the same message, output of map with message type as filter condition (we are not expecting any Acknowledge from sendPort_B, because it’s a generic send port for some scenario).
    Problem
    You have a send port B which is subscribing to the same message using the message type.
    You can set additional filters to ignore Acknowledgement-
    Ex- Mgtype= "" AND
    AckRequired!=True OR
    AckRequired!=False 
    Thanks,
    Prashant
    Please mark this post accordingly if it answers your query or is helpful.

  • How can i move contacts from other accounts (eg: gmail,hotmail etc) to icloud account.

    Hi All,
    I have setup icloud in my Iphone 6 running 8.1.2. and it is logged in by enabling Contacts in icloud settings. My contact list contains 1718 Contacts. I manually backed up my data into icloud including my contacts without Passbook & Documents data. But when i logged into iCloud.com to see the contacts it is not there.
    I also find when i checked only the Icloud contacts "there are no contacts showing in my contact list". I think this is the reason why there is no contacts in icloud web too. Then how can i move contacts from other accounts (eg: gmail,hotmail etc) to icloud.

    I assume you're using GMail as your primary e-mail account?  Try this.  This is how I am setup.  Not only will it move all your contacts to your phone but any changes made in GMail or on the iPhone will be sync'd with the server.
    http://www.google.com/support/mobile/bin/answer.py?answer=138740
    Note... I think this will delete your existing contacts from your device.  You will want to use iTunes to back them up and get them imported into GMail before you sync your device wtih GMail.

  • How do i import contact from other sim to iphone5

    How do i import contact from other sim to iphone5

    i would suggest backing up with iTunes or iCloud. This link should help.
    http://support.apple.com/kb/ht1766

  • How do I move objects from other package to a newly created one?

    How do I move objects from other package to a newly created one?
    Thanks!

    Dear Anthony Pham ,
    for changing objects from one package to another package
    open that object for example Report
    open the report and than click GOTO select OBJECT DIRECTIORY ENTRY
    than it will open a popup in that popup click the change and give the new package
    Thanks
    Surendra

  • How can I purchase songs from other country?

    How can I purchase songs from other country?

    You cant, because of licensing issues apple wont allow you too purchase songs which they dont have licens too sell in your country.

  • Set JComponent from other class

    Hi, I have problem from set any JComponent (JButton,JTable, etc.)
    in class from other class.
    I have class A. In class I have JButton and JTable. In this class I have function, in that are I create new instance class B. Class B have function, that are set properties any item in class A.
    class A
    function createAnotherClass(){
    B bclass = new B();
    bclass.setItemInClassA(this);
    function setItem(){
    jTable.setBackground(Color.RED);
    etc.
    class B
    function setItemInClassA(A a){
    a.setItem();
    This is only example, but I need your hepl. Thank you

    hahaha, yeah right,
    i think we do not understand your problem.

  • Accesing from other classes to protected void

    Hy! I'm a newbie to java programming. So i am making a program in J2ME, and the problem is :
    I have an abstract class, wich i must extend. That class defines a procedure
    abstract protected void destroyApp(boolean unconditional), so in my extended class this void is protected. But i want to access to this procedure from other class, and do it somehow static.
    I have made a main program class (the extended class), and i want to send from other class a destroy message to it (to call destroyApp procedure).
    Kazhha.

    Your question has nothing to do with Native Methods.
    In the future, for novice questions, use the New To Java Technology forum.
    I have an abstract class, which i must extend. That
    class defines a procedure
    abstract protected void destroyApp(boolean
    unconditional), so in my extended class this void is
    protected.You can make the method public in your class.
    But i want to access to this procedure
    from other class, and do it somehow static.IIRC you cannot make a method static if it is not static in the class you extend.
    I have made a main program class (the extended class),
    and i want to send from other class a destroy message
    to it (to call destroyApp procedure).Huh?

Maybe you are looking for