How do I access a gui component in a different class?

I have a jpanel (mainwindow) in a japplet. mainwindow loads and displays a jpanel form (content1). How do I code a button on conent1 so that mainwindow loads and displays a different jpanel form(content2)? mainwindow, content1, and content2 are all in different classes/packages. Thanks in advance!

Let your JPanel content1 forward its ActionEvents to its parent. For instance, you could define your content1 as follows:
public class Content1 extends JPanel
    private ArrayList<ActionListener> actionListeners;
    private JButton myButton;
          public Content1()
         actionListeners = new ArrayList<ActionListener>();
         myButton = new JButton("Test");
         myButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         forwardAction(e);
          public void addActionListener(ActionListener listener) {
         actionListeners.add(listener);
    protected void forwardAction(ActionEvent e) {
      for (ActionListener l: actionListeners) {
           l.actionPerformed(e);
}Then you could let your mainWindow listen to content1:
// in your main windows' code:
Content1 content1 = new Content1();
content1.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
          swapPane(e);    // create a method swapPane in your mainwindow that handles the switch to content2.
});

Similar Messages

  • Accessing a gui component directly via getSource()

    Hi,
    to access a gui component I can access it by getting its object variable and casting it to its class type, e.g.:
    public class Tab02 extends JFrame {
         JTable tab = new JTable (rows, head);
         JTextField txt = new JTextField ("dimensions");
         JTree tree = new JTree(node.getRoot());
    class DragDropNode implements TreeSelectionListener, MouseMotionListener, MouseListener {
    public void mouseDragged (MouseEvent me ) {
         Cursor dragcurs = new Cursor(13);
    // me.getSource().setCursor (mdragcurs) does not work as getSource delivers an object type
         JTree tmp = (JTree)me.getSource();
    // me.getSource().setCursor (mdragcurs)
         tmp.setCursor (mdragcurs);     // as me.getSource().setCursor (mdragcurs)
         JTable tmp = (JTable )me.getSource();
         tmp.setCursor (mdragcurs);
    How can I cast the object automatically to the genuine type?
    Is there a method that returns the type and I can use this method to cast me.getSource()?
    Thanks

    Navigate to the folder enclosing it and enter Library in the Go to Folder command.
    (70632)

  • How can JSP access a COM+ component

    I have to develop JSP pages that leverage existing Microsoft COM+ components. Can anyone suggest a good resource or provide me with a quick explanation/overview of how I would access a COM+ component from JSP? I'm getting exhausted searching the internet for answers only to come up empty. Thanks...

    I use Neva Object (www.nevaobject.com) for this sort of thing. This is a commercial product. The product does what it says it does and that is as far as I will endorse it.
    Since purchasing it I came accross
    http://staff.develop.com/halloway/code/jawin.html
    This looks like it will do much of the same thing but is more suitable for scriptable COM objects. It is also an open source license.
    I don't have any experience with jawin but the support community looks strong. Checking out their website will be worth your time.
    Good luck
    Bill

  • How to get change a GUI component from another class?

    Hi there,
    I'm currently trying to change a GUI component in my 'Application' class from my 'Dice' class.
    So the Application class sets up some GUI including a JLabel that initially displays "Change".
    The 'Dice' class contains the ActionPerformed() method for when the 'Change' button (made from Application class) is clicked.
    And it returns an 'int' between 1 and 6.
    Now I want to set this number back int he JLabel from the Application class.
    APPLICATION CLASS
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
    import java.awt.event.*;
    public class Application extends JFrame implements ActionListener{
         public JPanel rollDicePanel = new JPanel();
         public JLabel dice = new JLabel("Loser");
         public Container contentPane = getContentPane();
         public JButton button = new JButton("Change");
         public Dice diceClass = new Dice();
         public Application() {}
         public static void main(String[] args)
              Application application = new Application();
              application.addGUIComponents();
         public void addGUIComponents()
              contentPane.setLayout(new BorderLayout());
              rollDicePanel.add(dice);
            button.addActionListener(diceClass);
            contentPane.add(rollDicePanel, BorderLayout.SOUTH);
            contentPane.add(button,BorderLayout.NORTH);
              this.setSize(460, 655);
              this.setVisible(true);
              this.setResizable(false);
         public void changeDice()
              dice.setText("Hello");
         public void actionPerformed(ActionEvent e) {}
    }DICE
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class Dice implements ActionListener
         public Dice() {}
         public void actionPerformed(ActionEvent e)
              //super.actionPerformed(e);
              String event = e.getActionCommand();
              if(event.equals("Change"))
                   System.out.println("Will be about to change the 'dice' label");
                   Application application = new Application();
                   application.dice.setText("Hello");
    }

    It's all about references, baby. The Dice object needs a way to communicate with the Application object, and so Dice needs a reference to Application. There are many ways to pass this. In my example I pass the application object directly to Dice, but a better way would use interfaces and some indirection. Look up the Observer pattern for a better way to do this that scales much better than my brute-force approach.
    import javax.swing.*;
    import java.awt.*;
    public class Application extends JFrame // *** implements ActionListener
        // *** make all of these fields private ***
        private JPanel rollDicePanel = new JPanel();
        private JLabel dice = new JLabel("Loser");
        private Container contentPane = getContentPane();
        private JButton button = new JButton("Change");
        // *** pass a reference to your application ("this")
        // *** to your Dice object:
        private Dice diceClass = new Dice(this);
        public Application()
        public static void main(String[] args)
            Application application = new Application();
            application.addGUIComponents();
        public void addGUIComponents()
            contentPane.setLayout(new BorderLayout());
            rollDicePanel.add(dice);
            button.addActionListener(diceClass);
            contentPane.add(rollDicePanel, BorderLayout.SOUTH);
            contentPane.add(button, BorderLayout.NORTH);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setPreferredSize(new Dimension(460, 655));
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
            setResizable(false);
        // *** I'm not sure what this is supposed to be doing, so I commented it out.
        //public void changeDice()
            //dice.setText("Hello");
        // *** ditto.  I strongly dislike making a GUI class implement ActionListeenr
        //public void actionPerformed(ActionEvent e)
        // *** here's the public method that the Dice object calls
        public void setTextDiceLabel(String text)
            dice.setText(text);
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class Dice implements ActionListener
        // *** have a variable that holds a reference to your application object
        private Application application;
        private boolean hello = true;
        public Dice(Application application)
            // *** get that reference via a constructor parameter (one way to do this)
            this.application = application;
        public void actionPerformed(ActionEvent e)
            String event = e.getActionCommand();
            if (event.equals("Change"))
                System.out.println("Will be about to change the 'dice' label");
                if (hello)
                    // *** call the application's public method
                    application.setTextDiceLabel("Hello");
                else
                    application.setTextDiceLabel("Goodbye");
                hello = !hello;
                //Application application = new Application();
                //application.dice.setText("Hello");
    }

  • Updating a GUI component from a runnable class that doesn't know that GUI

    Hi. I have a problem that I think isn't solvable in an elegant way, but I'd like to get confirmation before I do it the dirty way.
    My application allows the user to save (and load) his work in sessions files. I implemented this by a serializable class "Session" that basically stores all the information that the user created or the settings he made in its member variables.
    Now, I obviously want the session files created by this to be as small as possible and therefore I made sure that no GUI components are stored in this class.
    When the user has made all his settings, he can basically "run" his project, which may last for a long time (minutes, hours or days, depending on what the user wants to do....). Therefore I need to update the GUI with information on the progress/outcome of the running project. This is not just a matter of updating one single GUI component, but of a dynamic number of different internal frames and panels. So I'd need a reference to a GUI component that knows all these subcomponents to run a method that does the update work.
    How do I do that? I cannot pass the reference to that component through the method's argument that "runs" the project, because that needs to be a seperate thread, meaning that the method is just the run() method of that thread which has no arguments (which I cannot modify if I'm not mistaken).
    So the only thing I can think of is passing the reference through the constructor of the runnable class (which in turn must be stored in the session because it contains critical information on the user's work). As a result, all components that need to be incorporated in that updating process would be part of the session and go into the exported file, which is exactly what I wanted to avoid.
    I hope this description makes sense...
    Thanks in advance!

    Thanks for the quick answer! Though to be honest I am not sure how it relates to my question. Which is probably my fault rather than yours :-)
    But sometimes all it takes me to solve my problem is posting it to a forum and reading it through again :)
    Now I wrote a seperate class "Runner" that extends thread and has the gui components as members (passed through its constructor). I create and start() that object in the run method of the original runnable class (which isn't runnable anymore) so I can pass the gui component reference through that run method's argument.
    Not sure if this is elegant, but at least it allows me to avoid saving gui components to the session file :-)
    I am realizing that probably this post shouldn't have gone into the swing forum...

  • HT2954 How can I access my imac mail from a different computer other than Apple?

    How can i access my imac email from a differnt computer other than
    apple?

    If you are using IMAP mail connection (IMAP keeps the  mail on the server), then you just configure the other computer's mail client to access the same account.
    If you are using a POP mail connection, then the mail is generally downloaded to your Mac and deleted from the server.  That is a much more difficult problem to solve.  NOTE:  Google's gmail does have a POP option that does NOT delete the mail on the server, so it is accessible via the gmail web page.
    A lot depends on what you are using for your mail service and whether you are using IMAP or POP protocols.

  • How to interact with a COM component from a Java class

    Hi, could someone give a hint on what API I should explore in order to interact with a COM component from a Java class?
    Thanks in advance
    Luis

    jacob sounds nice...http://danadler.com/jacob/

  • HT1203 How can I access my itunes account from a different computer as I need to cancel a subscription and I am on holiday at the moment

    Hello
    I need to access my itunes account from a different computer as I need to cancel a subscription and I am on holiday at the moment.
    Am I already now logged in to Apple ??
    Thanks

    If the Computor you are on has iTunes library go to tunes as if u were going to buy a song or film and you can log into your account. At the bottom of the iTunes home page you should see the user that is logged on usually a email address. Click on it to log them out and enter your own apple I'd and password. You can then update whatever you want and log back out after

  • How can i access my itunes library in a different computer?

    I have my itunes library in my laptop at home and sometimes during my spare time in the office I want to access my library or download more songs to my library. How can I do that?

    your personal library is not saved to the internet, but your itunes account is, so you can download songs on different computers through your accound
    also, if you want all of your itunes library on both comps, then just copy them using some kind of connection.
    And ipod works best

  • How can I access the oracle/sql server jdbc driver class files from my cust

    I have a war file in which I have custom DataSource i.e mypackage.Datasource class. Its basically the need of my application. In this class we connect to datasource and link some of our programming artifacts .
    I have deployed the oracle jdbc driver and deploy my application as ear with datasources.xml in the meta inf file. Inspite of that my code fails to load the jdbc driver classes.
    Here is the extract of the code :
            Class.forName("oracle.jdbc.OracleDriver").newInstance();
             String url = "jdbc:oracle:thin:@dataserver:1521:orcl";
            Connection conn = DriverManager.getConnection(url, "weblims3", "labware");
            if(conn != null){
              out.println("the connection to the database have been achieved");
            out.println("conn object achived= " + conn);
    Class.forname fails in this case. I can see the ojdbc5.jar the driver jar in usr\sap\CE1\J00\j2ee\cluster\bin\ext\ojdbc5  . I even put the ojdbc.jar in web-inf/lib and application lib but does not help at all. Hope I have explained my problem clearly.
    I deployed the jdbc driver in the name of ojdbc5 .
    I am stuck here. It will be great help if anyone can help me in this. Thanks in advance.

    Bent,
    You can access the database from your Java portlet, just like from any other Java/JSP environment. Yes, you can use JDBC, as well as BC4J.
    The Discussion Forum portlet was built using BC4J, take a look at it's source to see how it was done.
    Also, check out Re: BC4J Java portlet anyone?, it contains a lot of useful information too.
    Peter

  • How do I access a USB server on a "different network segment"?

    I have tried posting this question in the Server community but with no response, however I believe the solution will be achived through Terminal and I believe there will be those versed in the use of Terminal here, so here goes.
    I have a USB Server with four ports attached to my ethernet LAN. If I enter the IP address into Safari it shows me the Server details and the details of any item attached to any of the ports. However I cannot access anything on the server. If I put the IP address into Connect to Server I get an error message. Apparently the Server is on a "different network segment". How can I overcome this?
    Attached pages from Safari.

    MyBook USB=junk.
    If you search the forums you will find many users with problems with this particular brand.
    Does this drive have an external AC power supply brick? If not, Apple recommends the use of a powered USB hub.

  • How to use a flex custom component in an AS3 Class?

    Our software team has been developing flash applications using AS3 (via the FlashDevelop IDE and the free Flex SDK).  Recently, some members of the team started exploring FlexBuilder and Flex (wow... why did we wait so long?).  The problem is that some folks continue to develop using pure Action Script 3 (FlashDevelop) while others are creating custom components in FlexBuilder.
    How do the AS3 developers use the Flex Custom components built in FlexBuilder in their AS3 Applications?

    SwapnilVJ,
    Your suggestions enabled me to make progress, but I'm still having a problem.  Based on you suggestion, I learned how to make a swc using Flex Builder.  I successfully added the swc to the lib resource in my AS3 project (FlashDevelop).  I was able to instantiate one of my new components (code hinting even picked it up from the lib).
    When I run my app, my component is not visible.  I can trace properties of it and the values are correct.  Any thought why I might not be seeing my custom component?
    package trainer.games.board.MatchThree {
    import flash.display.Sprite;
    public class Test extends Sprite{
      private var cp:MatchingGameControlPanel; // <<< this is my swc custom component created in Flex
      public function Test() {
       cp = new MatchingGameControlPanel();
       cp.visible = true;
       addChild(cp);
       trace("width: ",cp.width); // <<< works and displays valid data for the component.

  • PLEASE HELP. How do you access properties files in WEB-INF  and classes directory

    We have a war file that needs to access properties files that are in the WEB-INF directory
    of the war file. We also need to load one of the properties files from the classpath.
    However, when we deploy the application ( an ear which inlcludes an ejbjar and a
    war and the libraries both the ejbjar (with a manifest setting the classpath ) and
    war need ) the properties don't get extracted.
    In some of our servlets we are trying to access those files with the path "WEB-INF/foo.properties"
    and we get a FileNotFoundException. Then we check and see that NO properties files
    have been extracted into their appropriate places ( not even those we throw into
    the WEB-INF/classes directory ).
    PLEASE HELP,
    Christian Hargraves

    The file doesn't have to be extracted from the war. For example, you can place
    test.properties into your app WEB-INF and write a simple JSP to see how it
    works:
    <%
    InputStream in = application.getResourceAsStream("/WEB-INF/test.properties");
    %>
    It will return you a zip inputstream if you deployed your application as a .war.
    Christian Hargraves <[email protected]> wrote:
    I try this, but I get a NullPointerException. The file never actually gets extracted
    from the war. Under tomcat and resin this works great ( that's why I am having all
    of the trouble i am having ), but there are absolutely no properties files in the
    extracted directories for WebLogic deploys. only:
    WEB-INF/some_tmp_dir/WEB-INF/lib
    and then some dynamically generated jor file with all of the classes that would normally
    go in WEB-INF/classes ( all except the properties, of course, which are no where
    to be found. ).
    There has to be some kind of setting I am missing. Please don't make me seperate
    these properties files from the war/ear and then put the path to these properties
    files in the CLASSPATH, changing one step to three steps to deploy!!
    I have found a documented bug where you can't even put the properties files in a
    jar file and that bug will never be fixed for WebLogic 6.1.
    "Dimitri I. Rakitine" <[email protected]> wrote:
    To access files in WEB-INF you can use ServletContext.getResourceXXX("/WEB-INF/filename")
    Christian Hargraves <[email protected]> wrote:
    We have a war file that needs to access properties files that are in theWEB-INF directory
    of the war file. We also need to load one of the properties files fromthe classpath.
    However, when we deploy the application ( an ear which inlcludes an ejbjarand a
    war and the libraries both the ejbjar (with a manifest setting the classpath) and
    war need ) the properties don't get extracted.
    In some of our servlets we are trying to access those files with the path"WEB-INF/foo.properties"
    and we get a FileNotFoundException. Then we check and see that NO propertiesfiles
    have been extracted into their appropriate places ( not even those wethrow into
    the WEB-INF/classes directory ).
    PLEASE HELP,
    Christian Hargraves--
    Dimitri
    Dimitri

  • How to get the command button component id  in bean class?

    Hi All,
    I'm using adf 11g.
    I'm having three command button , which have some similar code to use on fire of these buttons.
    if it possible to catch the component id in bean , i can use the code with little parameter passing .

    Hi,
    You can get the command button from the actionEvent (if you are writing a generic actionListener) and from it, you can get the Text of the command button. Using which, you can process accordingly.
    If the text also same, then set some client attribute for the command button (check out [url http://docs.oracle.com/cd/E21764_01/apirefs.1111/e12419/tagdoc/af_clientAttribute.html]af:clientAttribute), and from the actionListener, get the client attribute using getClientAttribute method and process accordingly.
    -Arun

  • How do I access my Iphoto photos on a different Mac. I saved photos from My Macbook Pro and sold it. Now I want to access those pictures on my desktop Mac. Is this possible?

    I saved photos from my Macbook Pro and sold it. Now I want to access those pictures from the Macbook Pro on my desktop Mac. Is this possible?

    That would depend on where you saved the photos. I assume they are on an external drive or a cd(s) or DVD or USB drive. Hook whatever device they are on to the desktop Mac, open iPhoto and do an Import to Library from the File menu.

Maybe you are looking for