Easy question: Give access to a class in another class.

Stupid title, but dont know how to express myself :P Sorry for that.
To the problem. Never had this problem before, and I know its a really easy solution to this.
I have my Main class, which creates a MyView class. Inside this class, I make to new classes(or instances of classes i've already made), MyPanel and MyToolsPanel. Now I want to add buttons inside the MyToolsPanel class, and add actionlisteners to these buttons inside MyPanel.
What I'v always done to grant access to MyToolsPanel inside of MyTools, is to simply add a
//this is the MyPanel class
MyToolsPanel mtp;
public void setMtp(MyToolsPanel mtp){
this.mtp = mtp;
}and then in the MyView class which create these to classes, I put a command: mp.setMtp(mtp);
When I run this it doesnt work.. Why? Should be easy to solve, since it obviously is a stupid problem that I just dont see. :P Feeling kind of stupid to ask this question, but this is how it is...
EDIT: I might have to implement actionListener inside the MyToolsPanel class?
Edited by: Stianbl on Sep 28, 2008 4:59 PM

one way:
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
* Subclasses JPanel, can send text out via the getText() method
* can hook into button press via addActionListener
* @author Pete
public class PanelCommSender extends JPanel
  private JTextField sendingField = new JTextField(12);
  private JButton sendButton = new JButton("Send");
  public PanelCommSender()
    add(sendingField);
    add(sendButton);
  public void addActionListener(ActionListener al)
    // attach this listener to the button
    sendButton.addActionListener(al);
   * call this to get the text currently in the textfield
   * @return String text
  public String getText()
    return sendingField.getText();
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
* Subclasses JPanel, receives text from another class
* @author Pete
public class PanelCommReceiver extends JPanel
  private JTextField showResultsField = new JTextField(12);
  public PanelCommReceiver()
    showResultsField.setEditable(false);
    add(new JLabel("Results from other panel: "));
    add(showResultsField);
   * call this to set text of textField
   * @param text
  public void setText(String text)
    showResultsField.setText(text);
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class PanelCommControl
  private static void createAndShowUI()
    // create new instances of the receiving and sending panels:
    final PanelCommReceiver receivePanel = new PanelCommReceiver();
    final PanelCommSender sendPanel = new PanelCommSender();
    // let the communicate w/ each other
    sendPanel.addActionListener(new ActionListener()
      public void actionPerformed(ActionEvent e)
        receivePanel.setText(sendPanel.getText());
    // place the receiving JPanel into a JFrame
    JFrame frame = new JFrame("Receiving Panel");
    frame.getContentPane().add(receivePanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(300, 300)); // make it bigger so it can be seen
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    // place the sending JPanel into a JDialog
    JDialog dialog = new JDialog(frame, "Sending Panel", false);
    dialog.getContentPane().add(sendPanel);
    dialog.pack();
    dialog.setLocationRelativeTo(null);
    dialog.setVisible(true);
  public static void main(String[] args)
    java.awt.EventQueue.invokeLater(new Runnable()
      public void run()
        // run the whole show in a thread-safe manner
        createAndShowUI();
}

Similar Messages

  • Question about access non-public class from other package.

    Hi, everyone!
    Suppose class A and class B are in the same java file of package pkg1
    -- A.java. So, A is a public class and B is a non-public class.
    If I want to access class B from another class class C and class
    C is in package pkg2. When compiling, an error occurs indicating
    that class B is not visible to class C.
    So, If I defined serveral classes in one java file and I want to
    access every class from other package. How should I do?
    (I think in one java file, there should be only one public class and
    only the public class can be accessed from other package.)
    Thanks in advance,
    George

    So, If I defined serveral classes in one java file and
    I want to
    access every class from other package. How should I
    do? As you already seem to know, there is at most one public class allowed per source file (at least, with javac and most popular compilers). So if you want more than one public class, you will need to use more than one file...

  • Why eclipse gives me warning if I access in inner class a parent  field ?

    I have a class
    class A{
    private Map fValues;
    A(){
    fValues= new HashMap();
    prrotected class B {
    B(){
    fValues.get("String");
    Above class is only for an example to expalin my problem.
    Inner class B wants to access fValues from class A .
    The eclipse gives me a warning
    Access to enclosing method fValues from the type A is emulated by a synthetic accessor method. Increasing its visibility will improve your performance
         I order to get rid of this warning I must make fValues public is this the only solution ?
    miro

    miro_connect wrote:
    I have a class
    class A{
    private Map fValues;
    A(){
    fValues= new HashMap();
    prrotected class B {
    B(){
    fValues.get("String");
    Above class is only for an example to expalin my problem.
    Inner class B wants to access fValues from class A .
    The eclipse gives me a warning
    Access to enclosing method fValues from the type A is emulated by a synthetic accessor method. Increasing its visibility will improve your performance
         I order to get rid of this warning I must make fValues public is this the only solution ?
    miroWhat happens if you remove the modifier? (That is uses default)

  • Question about view/controller/nib class design

    Assume you need to make an application with, let's say, 15 different views in total. There are two extreme design choices you can use to implement the app:
    1) Every single view has its own view controller and a nib file. Thus you end up with 15 controller classes and 15 nib files (and possibly a bunch of view classes if any of your views needs to be somehow specialized).
    2) You have only one controller which manages all the views, and one nib file from which they are loaded.
    AFAIK Apple and many books recommend going purely with option #1. However, going with this often results in needless complexity, large amounts of classes (and nib files) to be managed and complicated class dependencies, especially if some of the views (and thus their controllers) interact with each other or share something (something which would be greatly simplified if all these related views were handled by one single controller class).
    Option #2 also usually ends up being very complex. The major problem is that the single controller will often end up being enormous, handling tons of different (and usually unrelated) things (which is just outright bad design). This is seldom a good design, unless your application consists of only a few views which are closely related to each other (and thus it makes sense for one single controller class to handle them).
    (Option #2 also breaks the strictest interpretation of the MVC pattern, but that's not really something I'm concerned about. I'm concerned about simple design, not about following a programming pattern to the letter.)
    A design somewhere in between the two extremes often seems to be the best approach. However, since I don't have decades of Cocoa programming experience, I would like to hear some opinions about this subject matter from people with more experience on that subject. (I do have object-oriented programming experience, but I have only relatively recently started programming for the iPhone and thus Cocoa and its design patterns are relatively new to me, so I'm still learning.)

    Somehow I get the feeling that my question was slightly misunderstood.
    I was not asking "which one of these two designs do you think is better, option #1 or option #2?" I already said in my original post that option #2 is bad design (unless your application consists of just one or two views). That's not the issue.
    The issue is that from my own experience trying to adhere very strictly to the "every single view must have its own view controller and nib file" often results in needless complexity. Of course this is not always the case, but sometimes you end up having controller classes which perform very similar, if not even the exact same actions, resulting in code repetition. (An OO'ish solution to this problem would be to have a common base class for these view controllers where the common functionality has been grouped, but this often just adds to the overall complexity of the class hierarchy rather than alleviating it.)
    As an example, let's assume that you have a set of help screens (for example one help screen for each major feature of the app) and a view where you can select which help view to show. Every one of these views has, for example, a button to immediately exit the help system. If you had one single controller class managing these views, this becomes simpler: The controller can switch between any of the views and the buttons of each view (most of them doing the same things) can call back actions on this controller (eg. to return to the help selection or to exit the help screen completely). These help screens don't necessarily have any functionality of their own, so it's questionable what do they would need view controllers of their own. These view controllers would basically be empty because there's nothing special for them to do.
    View controllers might make it easy to use the navigation controller class, but the navigation controller is suitable mainly for utility apps but often not for things like games. (And if you need animated transitions between views, that can be implemented using the UIView animation features.)
    I also have hard time seeing the advantages of adhering strictly to the MVC pattern. The MVC pattern is useful in things like web servers, where MVC adds flexibility. The controller acts as a mediator between the database and the user interface, and it does so in such an abstract way that either one can be easily changed (eg. the "view", which normally outputs HTML, could be easily changed to a different "view" which outputs a PDF or even plain text, all this without having to touch the controller or the model at all). However, I'm not seeing the advantages of the MVC pattern in an iPhone app. It provides a type of class design, but why is it better than some other class design? It's not like the input and output formats of the app need to be changed on the fly (which is one advantage of a well-designed program using the MVC pattern).

  • Need to Give Access to my iMac to Work on this iMac Over Internet

    I do eCommerce from home using my iMac on Mac OS Lion.
    I want to hire somebody you can work from is home and manage all the task I do every day running this online store.
    What are the SAFE options to give access to my iMac?
    - I want to give complet access to my iMac
    - Parallels is instal on my iMac. I use Parallels Desktop to run Windows 7 and Quick Books accounting software. This is the main reason I want to give access to my iMac because most task are done online using email and my online store platform.
    My goal is to have access to all my informations when ever I need and supervise this employes.
    I tried ScreenSharing, I don't think it can do the job. It is ok to connect to a another computer to fix something, but to use everyday, it looks slow.
    How about Lion Server? Can it do the job?
    What those thise involve? Buy another mac and installing my accounting software on it?
    What are the cost to have a small server? The smallest.
    Can I set this uo myself?
    I found Open VPN on the internet, would it be a good option?
    New to VPN, is it easy to setup my computer?
    Is it safe?
    Anything I should know about creating this kind of network?
    One last thing, I have this old PowerBook G4, do you thing this employe can connect to my imac using this laptop? If yes, does I need a special vesrion of Mac OS to make this work?
    Thank you for your help.
    BigBlaze
    PS: if QuickBooks was a online platform, thing will have been much more easy

    You are going to need a server of some type, web server, that you or your emplyees can connect to,  concurrently, over a VPN connection.
    I really don't thing this is the forum for this type of thing.
    Good luck and best wishes.

  • Pls. give access to App Builder APIs!

    Dev. guys,
    a request. In a future version, can you pls. give us access (or at least publish how tos) to the App. Bldr. APIs ? We would like to extend the forms & reports functionality(just look at the questions raised by others). This is more so when we want to add custom event handlers, custom biz rules, adding code to do/display stuff before/after (currently there are issues with displaying content before and after the form due to page template etc).
    If it is not possible to open up a part of the App Bldr APIs, can you at least tell us how we can reuse portal components like LOVs etc in our custom forms ? Currently, this is not possible. Not to sound like I am whining, but in a 3 - 6mo. project, we can't custom code forms (specially when the Portal can do a much better job).

    Hi,
    You need to be an Admin to give access to the users as co-admin, and this can be done from the classic portal.
    Go to settings in the portal, click on Administrator, and click on Add, you will get a pop up window to Specify a co-administrator for subscriptions.
    Also check this link, Arvind has explained the authentication part while publishing the app via VS:
    https://social.msdn.microsoft.com/Forums/en-US/ba68d8fa-115d-4b18-b12a-96398f22b764/user-credential-verification-failed?forum=WindowsAzureAD
    Hope this helps.
    Regards,
    Azam khan

  • I have what is hopefully a quick and easy question. I kno...

    I have what is hopefully a quick and easy question. I know almost nothing about this stuff so go easy on me . All I need to do is find out if my DHCP is enabled. I'm having problems with my Xbox 360 and one of the possible problems is this DHCP thing. However I have no idea how to find the settings for my router on my computer. If it helps I have a Wirless model BEFW11S4. Thanks in advance for any help.

    You need to access your router to check if the DHCP is on.  To access the router open your browser and type in http://192.168.1.1 into the address field and hit enter.  That should open the routers log on screen and by default the user is left blank (some routers it is admin) and the password is abmin.  If you changed your routers password as you should for security reasons then use that password.  That will bring you to your routers user interface and on the main set up page should be your DHCP.
    Richard Aichner (Ikester)

  • My java program runs fine even if i don't specify access specifier of class

    Hi,
    My java program runs fine even if i don't specify access specifier of class as public .
    Then why do they say that atleast one class should be specified as public.
    please help.

    public access specifier is the default access
    specifier
    so if you dont give the access specifier before the
    class name it is not wrong.I think that you are wrong. The default specifier is package or "package-private".
    See here:
    http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
    Message was edited by:
    petes1234

  • Easy question hopefully.....

    I have an easy question but am having a mental block. How can I assign the value of 0.05 to restock? What I need to do is return a 5% restocking value added onto the item. Make sense?
    //Type.java
    public class Type extends Office // Start of public subclass Itemtype that stores office items color.
    private String color; //get string name
    private float restock; //get float value of restock
    private float item; //get float value of total items
    public Type(String color,float restock, float item)
    {  // constructor to initialize the fields
    this.color = color;
    this.restock = restock;
    this.item = item;
    public float getRestock()
    return (this.item * this.restock) + (this.item);
    } //end public subclass Type

    Don't crosspost. Too late this time, but don't do it again.

  • Easy Questions . . .Please Answer

    Ok so i have the new Airport Express 802.11n jobbie and i have just managed to set it up with my BT homehub
    the green light is on and its working (i think)
    Ok so the reasons i bought it was.
    1) love the wireless printing idea
    2) love the wireless music idea
    3) to plug an Ethernet Voip phone into (the home hub is in another room)
    4) i got it for £52.95
    ok so i have connected it in client mode (i think) where all the computers (theres is 3 of them in our house) connect to the homehub and so does the airport.
    What i want to know is how do i stop the other 2 computers using the airtunes and the wireless printing features. Basically i want the airport to myself for my home office which it was bought for and don't want everyone else stealing the features.
    Thanks!

    +What i want to know is how do i stop the other 2 computers using the airtunes and the wireless printing features.+
    Easy Questions....But not easy answers
    Since you have configured the AirPort Express in the home office as a client on the network, it's going to be visible and accessible from any other devices on the network. There's no way to change this or prevent access to the office AirPort Express.
    If...you configure the office AirPort Express to "create a wireless network", and set it up with a different wireless network name and password, you could create a "private" network for yourself.
    Here, I am assuming that the stereo is connected to the office AirPort Express, is that correct?
    The downside to this approach is that you would not have internet access on this network.
    If...you connected the AirPort Express to the HomeHub using an ethernet connection, you might be able to access the internet from your "private" network. I say might because I don't know enough about the HomeHub to know whether this would be allowed. I think you should assume that it can't to keep expectations at a realistic level.

  • How to give access to my application for my users

    I have created a report in APEX and i want to give access to users(not all) so that they can run the report.

    I created an access control page and i have created two end users to view the application. After that i try to login from end users credentials it is saying
    Access denied by Application security check but if i login in my account and click the run application and give end users credentials it is working.
    Thanks

  • Easy Question: Illegal Start of Expression

    This is a ridiculously easy question... but I am having trouble with it...
    Anyway, here is the line of code that is giving me trouble:
    jButtons = {{jButton1, jButton5, jButton9, jButton13},
    {jButton2, jButton6, jButton10, jButton14},
    {jButton3, jButton7, jButton11, jButton15},
    {jButton4, jButton8, jButton12, jButton16}};
            That's it. jButton1 through jButton16 are all jButton objects (for a GUI). jButtons is an array (4 by 4) of jButton. All are global variables, the buttons are all initilized (in fact, that was the problem I had before, and why I need to put this here: otherwise I get a null pointer exception).
    Surprisingly, such a simple line of code causes TONS of errors to occur. To save space, {...} * 2 means that the exception occurs twice in a row, errors are separated by comma's.
    { Illegal Start of Expression, {Not a statement, ; required} * 2} * 4, Empty statement
    A similar statement (int[] test = {{1,2,3},{4,5,6}};) works perfectly fine.
    Please help, doing this will reduce the size of my code to about a third of the size of the code. And then I can laugh in the faces of those people who say that I write long, and in-efficient code! MWHAHAHAHAHAHA!!
    However, I will keep at it, and Murphy's Law states I will find a solution 10 seconds after posting. If I do, I will edit this post, and tell you guys the answer ;)
    [Edit]In case you are wondering... all my other code is correct. Here is the adjacent 3 methods:
    private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
        ButtonClick(3,3);
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton10;
        private javax.swing.JButton jButton11;
        private javax.swing.JButton jButton12;
        private javax.swing.JButton jButton13;
        private javax.swing.JButton jButton14;
        private javax.swing.JButton jButton15;
        private javax.swing.JButton jButton16;
        private javax.swing.JButton jButton17;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JButton jButton5;
        private javax.swing.JButton jButton6;
        private javax.swing.JButton jButton7;
        private javax.swing.JButton jButton8;
        private javax.swing.JButton jButton9;
        private javax.swing.JLabel jLabel1;
        // End of variables declaration
         * @param args the command line arguments
        public static void main(String args[])
            jButtons = {{jButton1, jButton5, jButton9, jButton13},
    {jButton2, jButton6, jButton10, jButton14},
    {jButton3, jButton7, jButton11, jButton15},
    {jButton4, jButton8, jButton12, jButton16}};
            int[][] test = {{1,2,3},{4,5,6}};
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new GameWindow().setVisible(true);               
        String[] row1 = {"1", "5", "9", "13"};
        String[] row2 = {"2", "6", "10", "14"};
        String[] row3 = {"3", "7", "11", "15"};
        String[] row4 = {"4", "8", "12", ""};
        String[][] labels = {row1, row2, row3, row4};
        int blankX = 3;
        int blankY = 3;
        static javax.swing.JButton[][] jButtons;
        private void DisableAll()
            for (int looperX = 0; looperX < 4; looperX++)
                for (int looperY = 0; looperY < 4; looperY++)
                    jButtons[looperX][looperY].setEnabled(false); 
        Edited by: circularSquare on Oct 13, 2008 5:49 PM
    Edited by: circularSquare on Oct 13, 2008 5:52 PM

    You can only initialise an array like that when you declare it at the same time. Otherwise you have to do as suggested above.
    int[] numbers = {1,2,3,4}; //ok
    int[] numbers;
    numbers = {1,2,3,4}; // not ok

  • Why do I need to log into google to use feedly? And, when I do, it wants me to give access to my to my account.

    Is this safe, why should I have to sign in with my google account? And why do I need to give access to my account?

    What does that mean. I'm just asking how to use one of the products that I was encouraged to use by a Mozilla email. I was worried about the issue of safety and privacy of having to sign in with another companies account (Google) and give access to that account.

  • Need to give access to a room to multiple users at a time

    Hi ,
      My situation is like this . I need to create a room and give access to multiple users to that room at the same time . The user is not very comfortable with the interface and it's usability considering his education level but then he got to use the functionality as this is the solution suggested to the client . I am thinking of adding adding the room to portal favourites so that thay don't have to be taught the whole navigation path to go to the room and access the documents in that room . But let's say there are 200 users , and if adding that room to their portal favourites is part of my job , the only way I see to do that is login with each of their user id's and add the same . Is there a way I can do this with some batch job or something for all the users at the same time ?
    I am aware that in the page for user management you can upload the user data and in that data you also can upload the user mapping information . This saves you the effort of individually mapping every user with the systems in the portal , by logging individually with their user id's . Can this be done from there or a similar kind of interface in KM&C ?
    Regards
    Deepak Singh

    Hi Deepak,
    The favorites are placed in the "userhome" repository. Take a look at KM Content -> userhome -> for each username -> Favorites, there you'll find links with the favorites. You can simply copy & paste those to the other users, so you don't have to login with each one.
    Regards,
    Pascal

  • Easy question - how to increase number of recent files in menu?

    This is for CS5 if it matters.
    I'm an infrequent Illustrator user (at best) but I have what I hope is an easy question: I want to increase number of recent files showing in the menu.
    I can't find a preferece to set such as in PS and almost every other application on the face of the earth. I've tried to reset the preferences file (start Illustrator with ctrl-alt-shift held down) but I'm stuck at 4 files, which I assume is the anemic default option.
    Is there an easy way to do this that Adobe has hidded somewhere? As expected, it can't be found in the help, I can't find it on the forums, or even on the net, but I do know it can be done because I can see other installs showing lots of files in the list.
    thanks for any help.

    This might be because of a new feature that you can turn off for each object, and alltogether. When you create a new document at the bottom, make sure 'Align New Objects to Pixel Grid' is unchecked.  After you have created a new file, you can still go into the 'TRANSFORM' palette (SHIFT+8), use the little right top menu, and make sure 'Align New Objects to Pixel Grid' is unchecked. And if you want to get fancy with each object; you can select an object, and look at the 'TRANSFORM' palette at the bottom is a place to uncheck the 'Align to Pixel Grid' at your disposall.
    Before I found this, it ruined my week.
    http://www.kenwells.com

Maybe you are looking for