PaintComponent: inner class / method?

Hi,
I am doing a lot of painting in my paintComponent method and would like to ask whether it's possible to use e.g. inner classes or methods within paintComponent? I think I am confronted with the problem that my paintComponent method gets too unstructured :-/
Thanks for your help!

I am doing a lot of painting in my paintComponent
method and would like to ask whether it's possible to
use e.g. inner classes or methods within
paintComponent? I think I am confronted with the
problem that my paintComponent method gets too
unstructured :-/paintComponent is a method... so, you can call other methods from it. I'm not sure what you mean by "use inner classes". You can define an inner class and instantiate an object of that class from within paintComponent just like you can from any other method. Your question seems a bit weird, or I'm not understanding it.
Is your paintComponent too slow or just too long? By all means, use methods to logically separate different things you are doing, but this only add organization, not speed.

Similar Messages

  • How to call inner class method in one java file from another java file?

    hello guyz, i m tryin to access an inner class method defined in one class from another class... i m posting the code too wit error. plz help me out.
    // test1.java
    public class test1
         public test1()
              test t = new test();
         public class test
              test()
              public int geti()
                   int i=10;
                   return i;
    // test2.java
    class test2
         public static void main(String[] args)
              test1 t1 = new test1();
              System.out.println(t1.t.i);
    i m getting error as
    test2.java:7: cannot resolve symbol
    symbol : variable t
    location: class test1
              System.out.println(t1.t.geti());
    ^

    There are various ways to define and use nested classes. Here is a common pattern. The inner class is private but implements an interface visible to the client. The enclosing class provides a factory method to create instances of the inner class.
    interface I {
        void method();
    class Outer {
        private String name;
        public Outer(String name) {
            this.name = name;
        public I createInner() {
            return new Inner();
        private class Inner implements I {
            public void method() {
                System.out.format("Enclosing object's name is %s%n", name);
    public class Demo {
        public static void main(String[] args) {
            Outer outer = new Outer("Otto");
            I junior = outer.createInner();
            junior.method();
    }

  • BUG: ojc compiler doesnt handle @Override on anonymous inner class methods

    Hi,
    jdev 10.1.3.3.0.4157, XP SP2, jdk 5 u13:
    for the following code sample:
    Object o = new Object()
        @Override
        public String toString2()
            return "some string";
    };With ojc set as a compiler (the default), ojc doesnt catch the error about overriding toString with toString2, changing the compiler to javac achieves the required and correct behavior.

    Thanks Frank, I logged a couple more not so serious issues a while back but didnt get any feedback on them. The subject of the posts didn't have a 'BUG' prefix though. Should I edit them so to give them some attention, or were they silently logged :) ?

  • Method local Inner class

    Why method local class can access only the final varaible of the method?
    I found somewhere that this is because the non-final local variables go out of scope after method completion, but i wonder final local variables also reside on the stack.
    class MyOuter2 {
    private String x = "Outer2";
    void doStuff() {
    String z = "local variable";
    class MyInner {
    public void seeOuter() {
    System.out.println("Outer x is " + x);
    System.out.println("Local variable z is " + z); // Won't Compile!
    } // close inner class method
    } // close inner class definition
    } // close outer class method doStuff()
    }

    pxNet wrote:
    and so you are now saying it accesses the original variable, not a copy of it?No, I'm not saying that at all; see reply #1. The situation is quite the opposite, the inner class accesses a copy of the variable. Direct access to the local variable is most definitely not allowed. But that doesn't have anything to do with parameter-passing semantics.
    I'll try to clarify with another demonstration of the problem. Assume that direct access to non-final local variables was actually allowed, and that the following example would compile (it won't, of course): class Outer {
        Inner method() {
            Object o = "foo";
            return new Inner() {
                public void modifyLocalVariable() {
                    o = "bar";
        interface Inner {
            void modifyLocalVariable();
        public static void main(String[] args) {
            Inner inner = new Outer().method();
            inner.modifyLocalVariable(); // what local variable is this modifying?
                                         // the local variable in method() has gone
                                         // out of scope!
    }Because of the way anonymous inner and local classes are implemented, they get a copy of the local variables (per the JLS). Mind you, there isn't any parameter passing going on at all in this example.
    ~

  • Don't understand Inner Classes and how to use it

    Hi
    As you guess i 'am a newbie!
    I don't understand Inner Classes, particulary members(methods & fields) that an Inner method is able to manipulate.
    So I know that the methods of an Inner class (respectively Outer Class) instance can access members (private or public) of an instance of the Outer Class (respectively Inner Class).
    I tried to answer to a quizz : http://java.sun.com/developer/onlineTraining/new2java/supplements/quizzes/January03.html
    In the following class definition, which variables are inaccessible within the method of the inner class?
    class Test1 {
       public static int a = 1;
       private static int b = 2;
       public int c = 3;
       private int d = 4;
       public static class Inner {
         int e = 5;
         public void aMethod(int f) {
           int g = 6;
           // What can't be accessed here?
    }A.      b, c, d
    B.      c, d
    C.      b, c, d, f
    D.      None of them
    In my opinion members (public or private) of the Outer Class can be accessed by methods of the Inner Class; e.g: a, b, c,d thus for me the answer is D.
    Obviously i'm wrong, but why?

    Hi
    As you guess i 'am a newbie!
    I don't understand Inner Classes, particulary
    members(methods & fields) that an Inner method is
    able to manipulate.
    So I know that the methods of an Inner class
    (respectively Outer Class) instance can access
    members (private or public) of an instance of the
    Outer Class (respectively Inner Class).
    I tried to answer to a quizz :
    http://java.sun.com/developer/onlineTraining/new2java/
    supplements/quizzes/January03.html
    In the following class definition, which variables
    are inaccessible within the method of the inner
    class?
    class Test1 {
    public static int a = 1;
    private static int b = 2;
    public int c = 3;
    private int d = 4;
    public static class Inner {
    int e = 5;
    public void aMethod(int f) {
    int g = 6;
    // What can't be accessed here?
    }A.      b, c, d
    B.      c, d
    C.      b, c, d, f
    D.      None of them
    In my opinion members (public or private) of the
    Outer Class can be accessed by methods of the Inner
    Class; e.g: a, b, c,d thus for me the answer is
    D.
    Obviously i'm wrong, but why?Inner class method can access all the private members of the class. But the inner class is static. So the non-static members cannot be accessed directly (i.e. c and d).
    ***Annie***

  • Getting the name of outer class in an inner class

    Hi,
    I have a private inner class, something like this:
    public class OuterClass extends AnotherClass {
    public OuterClass() {
    supre();
    private class innerClass1 extends SomeotherClass {
    protected void someMethod() {
    // how to get the name of outer class, that is, "OuterClass"
    Can someone please tell me how to get the name of the outer class inside the inner class method?
    Thanks in advance..
    Prasanna

    getClass().getEnclosingClass().getName()But then, you already know it, don't you?

  • How to reference an inner class with @link?

    Hi,
    Let's assume we have a class Test with an inner class namned InnerTest. If I want to put a @link to a method in InnerTest in another java-file, what do I write?
    To reference a method in Test, I write:
    {@link com.company.Test#method}
    My first thought when referencing the inner class' method was to just write:
    {@link com.company.Test.InnerTest#method}
    but this doesn't seem to be the correct way.
    Any help is greatly appreciated! Thank you in advance.

    You're calling the method in the inner class the right way.
    You're probably experiencing bugs with links.
    I don't know what version you're using, but 1.4.0 and
    1.4.1 have some severe bugs. Please see another
    thread in this forum entitled:
    "Some {@link} tags do not seem to generate hyperlinks"
    -Doug Kramer
    Javadoc team

  • Examples For 4 types of Inner Classes in java..

    Hi all,
    It would be gr8 help if u poeple provide me some examples in Java APIs for 4 types of Inner Classes.
    Normal Inner Class,Static Inner Class,Method Local Inner Class and Anonymous Inner Class.
    Thank you ..

    bprathibha wrote:
    ok... not to read..
    I was asked this question in an interview.
    I was unable to answer this question.I guess you know how to write a class?
    Normal Inner Class,Static Inner Class,Method Local Inner Class and Anonymous Inner Class.A normal inner class is a class that is written inside another class. A static inner class is the same thing as the normal inner class except that it's declared to be static. A method local class is a class that is declared within a method and it's only known within that method. An anonymous class is where you create a subclass and instantiate it at the same time. The construction if often used in UI programming. E.g.
    public void addListeners() {
        button.addActionListener(new ActionListener() { //Anonymous class
            public void actionPerformed(ActionEvent e) {
    }Kaj

  • Debugging Inner Class using jdb

    Hi
    I am using JDK 1.3.1 on solaris and observe that the java debugger does not stop at "Inner class" methods. Is there any way to make jdb (from command line) stop inside the "inner class"?

    I had the following code (similar to the example you gave) :
    package test;
    public class Outer {
      public Outer() {
        Inner i = new Inner();
      static public void main(String[] args) {
        Outer o = new Outer();
      public class Inner {
        public Inner() {
          System.out.println("Inner");
          Runnable Runner = new Runnable() {              
         public void run() {
           System.out.println("Runner");
          Runner.run();  
    }Which compiled into :
    Outer.class
    Outer$Inner.class
    Outer$1.class
    And to stop at the line System.out.println("Runner"); I could use stop at test.Outer$1:14 or stop in test.Outer$1.run(). So it seems that you need to look at the compiled .class name and use that.

  • Accessing inner Classes.. how?

    I'm reading the Java API and there is an Interface class named DocumentEvent that has an inner class named DocumentEvent.ElementChange..
    Since all is well with adding a DocumentListener (as per the API) for DocumentEvents, how is it possible to access the inner class' method? Do I need to implement another interface i.e. ElementChange and then write out all the methods?? What I really want is to use a method from DocumentEvent - getChange( Element elem) but this one relates to the inner class somehow.. can anyone answer this?
    Thanks so much!!!

    For example, in the caller
      A a = new A();
      (a.new B()).mm();for the method mm() in the inner class B of the class A.
    class A{
    class B{
      void mm(){
        System.out.println(Integer.MIN_VALUE);
    }

  • Trying to use super class's methods from an anonymous inner class

    Hi all,
    I have one class with some methods, and a second class which inherits from the first. The second class contains a method which starts up a thread, which is an anonymous inner class. Inside this inner class, I want to call a method from my first class. How can I do this?
    If I just call the method, it will use the second class's version of the method. However, if I use "super," it will try to find that method in the Thread class (it's own super class) and complain.
    Any suggestions?
    Code:
    public class TopClass
         public void doSomething(){
              // do something
    =============================
    public class LowerClass extends TopClass
         // overrides TopClass's doSomething.
         public void doSomething(){
              // do something
         public void testThread(){
              Thread t = new Thread(){
                   public void run(){
                        doSomething();               //fine
                        super.doSomething();          //WRONG: searches class Thread for doSomething...
              t.start();
    }

    Classes frequently call the un-overridden versions of methods from their superclasses. That's that the super keyword is for, if I'm not mistaken.You're not mistaken about the keyword, but you're not calling the superclass method from a subclass. Your anonymous inner class is not a subtype of TopLevel. It's a subtype of Thread.
    Here it is no different, except that I happen to be in a thread at the time.It's vastly different, since you're attempting to call the method from an unrelated class; i.e., Thread.
    I could also be in a button's action listener, for example. It seems natural to me that if I can do it in a method, I should be able to do it within an anonymous inner class which is inside a method.If you were in an button's action listener and needed to call a superclass' implementation of a method overridden in the button, I'd have the same questions about your design. It seems smelly to me.
    ~

  • Main method not found and how to put event handlers in an inner class

    How would I put all the event handling methods in an inner class and I have another class that just that is just a main function, but when i try to run the project, it says main is not found. Here are the two classes, first one sets up everything and the second one is just main.
    mport java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    public class JournalFrame extends JFrame {
    private JLabel dateLabel = new JLabel("Date: ");
    private JTextField dateField = new JTextField(20);
    private JPanel datePanel = new JPanel(new FlowLayout());
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel radioPanel = new JPanel();
    JPanel statusPanel = new JPanel();
    JPanel textAreaPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    GridLayout gridLayout1 = new GridLayout();
    FlowLayout flowLayout1 = new FlowLayout();
    GridLayout gridLayout2 = new GridLayout();
    GridLayout gridLayout3 = new GridLayout();
    JRadioButton personalButton = new JRadioButton();
    JRadioButton businessButton = new JRadioButton();
    JLabel status = new JLabel();
    JTextArea entryArea = new JTextArea();
    JButton clearButton = new JButton();
    JButton saveButton = new JButton();
    ButtonGroup entryType = new ButtonGroup();
    public JournalFrame(){
    try {
    jbInit();
    catch(Exception e) {
    e.printStackTrace();
    private void initWidgets(){
    private void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    radioPanel.setLayout(gridLayout1);
    statusPanel.setLayout(flowLayout1);
    textAreaPanel.setLayout(gridLayout2);
    buttonPanel.setLayout(gridLayout3);
    personalButton.setSelected(true);
    personalButton.setText("Personal");
    personalButton.addActionListener(new JournalFrame_personalButton_actionAdapter(this));
    businessButton.setText("Business");
    status.setText("");
    entryArea.setText("");
    entryArea.setColumns(10);
    entryArea.setLineWrap(true);
    entryArea.setRows(30);
    entryArea.setWrapStyleWord(true);
    clearButton.setPreferredSize(new Dimension(125, 25));
    clearButton.setText("Clear Journal Entry");
    clearButton.addActionListener(new JournalFrame_clearButton_actionAdapter(this));
    saveButton.setText("Save Journal Entry");
    saveButton.addActionListener(new JournalFrame_saveButton_actionAdapter(this));
    this.setTitle("Journal");
    gridLayout3.setColumns(1);
    gridLayout3.setRows(0);
    this.getContentPane().add(datePanel, BorderLayout.NORTH);
    this.getContentPane().add(radioPanel, BorderLayout.WEST);
    this.getContentPane().add(textAreaPanel, BorderLayout.CENTER);
    this.getContentPane().add(buttonPanel, BorderLayout.EAST);
    entryType.add(personalButton);
    entryType.add(businessButton);
    datePanel.add(dateLabel);
    datePanel.add(dateField);
    radioPanel.add(personalButton, null);
    radioPanel.add(businessButton, null);
    textAreaPanel.add(entryArea, null);
    buttonPanel.add(clearButton, null);
    buttonPanel.add(saveButton, null);
    this.getContentPane().add(statusPanel, BorderLayout.SOUTH);
    statusPanel.add(status, null);
    this.pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    private void saveEntry() throws IOException{
    if( personalButton.isSelected())
    String file = "Personal.txt";
    BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
    entryArea.getText();
    bw.write("Date: " + dateField.getText());
    bw.newLine();
    bw.write(entryArea.getText());
    bw.newLine();
    bw.flush();
    bw.close();
    status.setText("Journal Entry Saved");
    else if (businessButton.isSelected())
    String file = "Business.txt";
    BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
    bw.write("Date: " + dateField.getText());
    bw.newLine();
    bw.write(entryArea.getText());
    bw.newLine();
    bw.flush();
    bw.close();
    status.setText("Journal Entry Saved");
    void clearButton_actionPerformed(ActionEvent e) {
    dateField.setText("");
    entryArea.setText("");
    status.setText("");
    void saveButton_actionPerformed(ActionEvent e){
    try{
    saveEntry();
    }catch(IOException error){
    status.setText("Error: Could not save journal entry");
    void personalButton_actionPerformed(ActionEvent e) {
    class JournalFrame_clearButton_actionAdapter implements java.awt.event.ActionListener {
    JournalFrame adaptee;
    JournalFrame_clearButton_actionAdapter(JournalFrame adaptee) {
    this.adaptee = adaptee;
    public void actionPerformed(ActionEvent e) {
    adaptee.clearButton_actionPerformed(e);
    class JournalFrame_saveButton_actionAdapter implements java.awt.event.ActionListener {
    JournalFrame adaptee;
    JournalFrame_saveButton_actionAdapter(JournalFrame adaptee) {
    this.adaptee = adaptee;
    public void actionPerformed(ActionEvent e) {
    adaptee.saveButton_actionPerformed(e);
    class JournalFrame_personalButton_actionAdapter implements java.awt.event.ActionListener {
    JournalFrame adaptee;
    JournalFrame_personalButton_actionAdapter(JournalFrame adaptee) {
    this.adaptee = adaptee;
    public void actionPerformed(ActionEvent e) {
    adaptee.personalButton_actionPerformed(e);
    public class JournalApp {
    public static void main(String args[])
    JournalFrame journal = new JournalFrame();
    journal.setVisible(true);
    }

    Bet you're trying "java JournalFrame" when you need to "java JournalApp".
    Couple pointers toward good code.
    1) Use white space (extra returns) to separate your code into logical "paragraphs" of thought.
    2) Add comments. At a minimum a comment at the beginning should name the file, state its purpose, identify the programmer and date written. A comment at the end should state that you've reached the end. In the middle, any non-obvious code should be commented and closing braces or parens should have a comment stating what they close (if there is non-trivial separation from where they open).
    Here's a sample:
    // JournalFrame.java - this does ???
    // saisoft, 4/18/03
    // constructor
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        radioPanel.setLayout(gridLayout1);
        statusPanel.setLayout(flowLayout1);
        textAreaPanel.setLayout(gridLayout2);
        buttonPanel.setLayout(gridLayout3);
        personalButton.setSelected(true);
        personalButton.setText("Personal");
        personalButton.addActionListener(new JournalFrame_personalButton_actionAdapter(this));
        businessButton.setText("Business");
        status.setText("");
        entryArea.setText("");
        entryArea.setColumns(10);
        entryArea.setLineWrap(true);
        entryArea.setRows(30);
        entryArea.setWrapStyleWord(true);
    } // end constructor
    // end JournalFrame.java3) What would you expect to gain from that inner class? It might be more cool, but would it be more clear? I give the latter (clarity) a lot of importance.

  • Main method not found and how to implement events in an inner class

    How would I put all the event handling methods in an inner class and I have another class that just that is just a main function, but when i try to run the project, it says main is not found. Here are the two classes, first one sets up everything and the second one is just main.
    mport java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    public class JournalFrame extends JFrame {
    private JLabel dateLabel = new JLabel("Date: ");
    private JTextField dateField = new JTextField(20);
    private JPanel datePanel = new JPanel(new FlowLayout());
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel radioPanel = new JPanel();
    JPanel statusPanel = new JPanel();
    JPanel textAreaPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    GridLayout gridLayout1 = new GridLayout();
    FlowLayout flowLayout1 = new FlowLayout();
    GridLayout gridLayout2 = new GridLayout();
    GridLayout gridLayout3 = new GridLayout();
    JRadioButton personalButton = new JRadioButton();
    JRadioButton businessButton = new JRadioButton();
    JLabel status = new JLabel();
    JTextArea entryArea = new JTextArea();
    JButton clearButton = new JButton();
    JButton saveButton = new JButton();
    ButtonGroup entryType = new ButtonGroup();
    public JournalFrame(){
    try {
    jbInit();
    catch(Exception e) {
    e.printStackTrace();
    private void initWidgets(){
    private void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    radioPanel.setLayout(gridLayout1);
    statusPanel.setLayout(flowLayout1);
    textAreaPanel.setLayout(gridLayout2);
    buttonPanel.setLayout(gridLayout3);
    personalButton.setSelected(true);
    personalButton.setText("Personal");
    personalButton.addActionListener(new JournalFrame_personalButton_actionAdapter(this));
    businessButton.setText("Business");
    status.setText("");
    entryArea.setText("");
    entryArea.setColumns(10);
    entryArea.setLineWrap(true);
    entryArea.setRows(30);
    entryArea.setWrapStyleWord(true);
    clearButton.setPreferredSize(new Dimension(125, 25));
    clearButton.setText("Clear Journal Entry");
    clearButton.addActionListener(new JournalFrame_clearButton_actionAdapter(this));
    saveButton.setText("Save Journal Entry");
    saveButton.addActionListener(new JournalFrame_saveButton_actionAdapter(this));
    this.setTitle("Journal");
    gridLayout3.setColumns(1);
    gridLayout3.setRows(0);
    this.getContentPane().add(datePanel, BorderLayout.NORTH);
    this.getContentPane().add(radioPanel, BorderLayout.WEST);
    this.getContentPane().add(textAreaPanel, BorderLayout.CENTER);
    this.getContentPane().add(buttonPanel, BorderLayout.EAST);
    entryType.add(personalButton);
    entryType.add(businessButton);
    datePanel.add(dateLabel);
    datePanel.add(dateField);
    radioPanel.add(personalButton, null);
    radioPanel.add(businessButton, null);
    textAreaPanel.add(entryArea, null);
    buttonPanel.add(clearButton, null);
    buttonPanel.add(saveButton, null);
    this.getContentPane().add(statusPanel, BorderLayout.SOUTH);
    statusPanel.add(status, null);
    this.pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    private void saveEntry() throws IOException{
    if( personalButton.isSelected())
    String file = "Personal.txt";
    BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
    entryArea.getText();
    bw.write("Date: " + dateField.getText());
    bw.newLine();
    bw.write(entryArea.getText());
    bw.newLine();
    bw.flush();
    bw.close();
    status.setText("Journal Entry Saved");
    else if (businessButton.isSelected())
    String file = "Business.txt";
    BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
    bw.write("Date: " + dateField.getText());
    bw.newLine();
    bw.write(entryArea.getText());
    bw.newLine();
    bw.flush();
    bw.close();
    status.setText("Journal Entry Saved");
    void clearButton_actionPerformed(ActionEvent e) {
    dateField.setText("");
    entryArea.setText("");
    status.setText("");
    void saveButton_actionPerformed(ActionEvent e){
    try{
    saveEntry();
    }catch(IOException error){
    status.setText("Error: Could not save journal entry");
    void personalButton_actionPerformed(ActionEvent e) {
    class JournalFrame_clearButton_actionAdapter implements java.awt.event.ActionListener {
    JournalFrame adaptee;
    JournalFrame_clearButton_actionAdapter(JournalFrame adaptee) {
    this.adaptee = adaptee;
    public void actionPerformed(ActionEvent e) {
    adaptee.clearButton_actionPerformed(e);
    class JournalFrame_saveButton_actionAdapter implements java.awt.event.ActionListener {
    JournalFrame adaptee;
    JournalFrame_saveButton_actionAdapter(JournalFrame adaptee) {
    this.adaptee = adaptee;
    public void actionPerformed(ActionEvent e) {
    adaptee.saveButton_actionPerformed(e);
    class JournalFrame_personalButton_actionAdapter implements java.awt.event.ActionListener {
    JournalFrame adaptee;
    JournalFrame_personalButton_actionAdapter(JournalFrame adaptee) {
    this.adaptee = adaptee;
    public void actionPerformed(ActionEvent e) {
    adaptee.personalButton_actionPerformed(e);
    public class JournalApp {
    public static void main(String args[])
    JournalFrame journal = new JournalFrame();
    journal.setVisible(true);
    }

    Here is the complete code (with crappy indentation) :
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    public class JournalFrame extends JFrame {
        private JLabel dateLabel = new JLabel("Date: ");
        private JTextField dateField = new JTextField(20);
        private JPanel datePanel = new JPanel(new FlowLayout());
        BorderLayout borderLayout1 = new BorderLayout();
        JPanel radioPanel = new JPanel();
        JPanel statusPanel = new JPanel();
        JPanel textAreaPanel = new JPanel();
        JPanel buttonPanel = new JPanel();
        GridLayout gridLayout1 = new GridLayout();
        FlowLayout flowLayout1 = new FlowLayout();
        GridLayout gridLayout2 = new GridLayout();
        GridLayout gridLayout3 = new GridLayout();
        JRadioButton personalButton = new JRadioButton();
        JRadioButton businessButton = new JRadioButton();
        JLabel status = new JLabel();
        JTextArea entryArea = new JTextArea();
        JButton clearButton = new JButton();
        JButton saveButton = new JButton();
        ButtonGroup entryType = new ButtonGroup();
        public JournalFrame(){
            try {
                jbInit();
            catch(Exception e){
                e.printStackTrace();
        private void initWidgets(){
        private void jbInit() throws Exception {
            this.getContentPane().setLayout(borderLayout1);
            radioPanel.setLayout(gridLayout1);
            statusPanel.setLayout(flowLayout1);
            textAreaPanel.setLayout(gridLayout2);
            buttonPanel.setLayout(gridLayout3);
            personalButton.setSelected(true);
            personalButton.setText("Personal");
            personalButton.addActionListener(new JournalFrame_personalButton_actionAdapter(this));
            businessButton.setText("Business");
            status.setText("");
            entryArea.setText("");
            entryArea.setColumns(10);
            entryArea.setLineWrap(true);
            entryArea.setRows(30);
            entryArea.setWrapStyleWord(true);
            clearButton.setPreferredSize(new Dimension(125, 25));
            clearButton.setText("Clear Journal Entry");
            clearButton.addActionListener(new JournalFrame_clearButton_actionAdapter(this));
            saveButton.setText("Save Journal Entry");
            saveButton.addActionListener(new JournalFrame_saveButton_actionAdapter(this));
            this.setTitle("Journal");
            gridLayout3.setColumns(1);
            gridLayout3.setRows(0);
            this.getContentPane().add(datePanel, BorderLayout.NORTH);
            this.getContentPane().add(radioPanel, BorderLayout.WEST);
            this.getContentPane().add(textAreaPanel, BorderLayout.CENTER);
            this.getContentPane().add(buttonPanel, BorderLayout.EAST);
            entryType.add(personalButton);
            entryType.add(businessButton);
            datePanel.add(dateLabel);
            datePanel.add(dateField);
            radioPanel.add(personalButton, null);
            radioPanel.add(businessButton, null);
            textAreaPanel.add(entryArea, null);
            buttonPanel.add(clearButton, null);
            buttonPanel.add(saveButton, null);
            this.getContentPane().add(statusPanel, BorderLayout.SOUTH);
            statusPanel.add(status, null);
            this.pack();
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        private void saveEntry() throws IOException{
            if( personalButton.isSelected()){
                String file = "Personal.txt";
                BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
                entryArea.getText();
                bw.write("Date: " + dateField.getText());
                bw.newLine();
                bw.write(entryArea.getText());
                bw.newLine();
                bw.flush();
                bw.close();
                status.setText("Journal Entry Saved");
            else if (businessButton.isSelected()){
                String file = "Business.txt";
                BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
                bw.write("Date: " + dateField.getText());
                bw.newLine();
                bw.write(entryArea.getText());
                bw.newLine();
                bw.flush();
                bw.close();
                status.setText("Journal Entry Saved");
        void clearButton_actionPerformed(ActionEvent e) {
            dateField.setText("");
            entryArea.setText("");
            status.setText("");
        void saveButton_actionPerformed(ActionEvent e){
            try{saveEntry();}catch(IOException error){
                status.setText("Error: Could not save journal entry");
        void personalButton_actionPerformed(ActionEvent e) {
        class JournalFrame_clearButton_actionAdapter implements java.awt.event.ActionListener {
        JournalFrame adaptee;
        JournalFrame_clearButton_actionAdapter(JournalFrame adaptee) {
            this.adaptee = adaptee;
        public void actionPerformed(ActionEvent e) {
            adaptee.clearButton_actionPerformed(e);
    class JournalFrame_saveButton_actionAdapter implements java.awt.event.ActionListener {
        JournalFrame adaptee;
        JournalFrame_saveButton_actionAdapter(JournalFrame adaptee) {
            this.adaptee = adaptee;
        public void actionPerformed(ActionEvent e) {
            adaptee.saveButton_actionPerformed(e);
    class JournalFrame_personalButton_actionAdapter implements java.awt.event.ActionListener {
        JournalFrame adaptee;
        JournalFrame_personalButton_actionAdapter(JournalFrame adaptee) {
            this.adaptee = adaptee;
        public void actionPerformed(ActionEvent e) {
            adaptee.personalButton_actionPerformed(e);
    }BadLands

  • How to override a method in an inner class of the super class

    I have a rather horribly written class, which I need to adapt. I could simply do this if I could override a method in one of it's inner classes. My plan then was to extend the original class, and override the method in question. But here I am struggling.
    The code below is representative of my situation.
    public class ClassA
       ValueChecks vc = null;
       /** Creates a new instance of Main */
       public ClassA()
          System.out.println("ClassA Constructor");
          vc = new ValueChecks();
          vc.checkMaximum();
         // I want this to call the overridden method, but it does not, it seems to call
         // the method in this class. probably because vc belongs to this class
          this.vc.checkMinimum();
          this.myMethod();
       protected void myMethod()
          System.out.println("myMethod(SUPER)");
       protected class ValueChecks
          protected boolean checkMinimum()
             System.out.println("ValueChecks.checkMinimum (SUPER)");
             return true;
          protected boolean checkMaximum()
             return false;
    }I have extended ClassA, call it ClassASub, and it is this Class which I instantiate. The constructor in ClassASub obviously calls the constructor in ClassA. I want to override the checkMinimum() method in ValueChecks, but the above code always calls the method in ClassA. The ClassASub code looks like this
    public class ClassASub extends ClassA
       public ClassAInner cias;
       /** Creates a new instance of Main */
       public ClassASub()
          System.out.println("ClassASub Constructor");
       protected void myMethod()
          System.out.println("myMethod(SUB)");
       protected class ValueChecks extends ClassA.ValueChecks
          protected boolean checkMinimum()
             System.out.println("ValueChecks.checkMinimum (SUB)");
             return true;
    }The method myMethod seems to be suitably overridden, but I cannot override the checkMinimum() method.
    I think this is a stupid problem to do with how the ValueChecks class is instantiated. I think I need to create an instance of ValueChecks in ClassASub, and pass a reference to it into ClassA. But this will upset the way ClassA works. Could somebody enlighten me please.

    vc = new ValueChecks();vc is a ValueChecks object. No matter whether you subclass ValueChecks or not, vc is always of this type, per this line of code.
    // I want this to call the overridden method, but it does not, it seems to > call
    // the method in this class. probably because vc belongs to this class
    this.vc.checkMinimum();No, it's because again vc references a ValueChecks object, because it was created as such.
    And when I say ValueChecks, I mean the original class, not the one you tried to create by the same name, attempting to override the original.

  • Accessing method in an inner class

    I have a class, which has an Inner Class, which is an extension of AbstractTableModel. The extended TableModel class has a new method, so it looks something like this;
    public class TheOuterClass
        JTable aTable
        TableModel theTableModel
        public initTable
             theTableModel = new MyTableModel();
         public TableModel getModel()
             return theTableModel;
         private class MyTableModel extends AbstractTableModel
                public void myTableModelMethod()
    }So, the idea here is that I have a class that has a table referenced by 'aTable', which uses MyTableModel as the class for it's table model. I have only implemented the basics here. The class also has a method called getModel(), so from a reference to 'TheOuterClass', I can access the table model.
    Now, say I have a reference to TheOuterClass called toc, and I want to access my new method in the table model;
    toc.getModel().myTableModelMethod()The above won't work, because getModel() returns a type of TableModel.
    My question then is how do I cast this to the correct type, so I can access the method 'myTableMethod()'?
    Is for example, the following a legal possibility, because I cannot seem to make it work;
    (toc.getModel().getClass())(toc.getModel()).myTableMethod();The quick fix, I guess is to correct getModel in TheOuterClass, so it returns the correct type, but I am hoping to not do this. (This is part of a larger piece of code obviously, and TheOuterClass is in reality a bean, and I don't wnat to disturb anymore than I have to).
    Any suggestions / ideas would be gratefully appreciated

    You are of course both correct, the class is private, should have spotted that! Doh. Also correct in that this is 'not the most elegant design', but you know the way it is you have to work with what you are given.
    So, I changed the class to public....
    What I had hoped is that the following would work
    ((toc.getModel().getClass())(toc.getModel())).myTableMethod()get a reference to the table model
    (toc.getModel())cast it to the correct type (not sure if this is a valid way to cast??)
    ((toc.getModel().getClass())(toc.getModel()))then call the method.
    This does not compile, it complains about a missing ')', and I'm sure they are all there. My question here then is, Is this a valid way to cast, now that the inner class is public?
    As to why I want to do this, then some explanation is required;
    The table model holds a Vector with all the data in it, some which is not actually in the table (it was originally written this way). My additional method myTableMethod() is intended to help access the data that is not shown in the table.
    Coming back to kajbj's point of creating an interface, I presume what is being suggested is that I create a public interface with the myTableMethod() in it, and make myTableModel implement this interface. Since the interface is public, then I can cast to that. Is this what you meant?
    Thanks for your help so far

Maybe you are looking for

  • BPM Studio call web service deployed by R12 Intergration Repository error

    Hi, I encountered a problem when calling a web service deployed by R12 Intergration Repository (SOA Gateway) w/ username/password authentication. It seems that the BPM Studio can not pass the security information to the web service. But using SoapUI

  • Internal order - FI postings not allowed

    Hi   I am trying to release a billing document. I am getting an error saying "FI postings not allowed - I.O number". I am not much aware of internal orders. Could anyone here let me know how I can enable FI postings for this particular internal order

  • Display all images in a folder

    Hi I want to display all the jpg images stored in my images folder. Is there a way to do this in JSP, JSTL or Applets?

  • HP 7610

    My HP 7610 will scan all types of documents, but will not save anything but pdf or jpg.  When I scan an editable document it scans but will not allow me to save.  I have unloaded and reloaded the most recent software, I have run HP Print and scan doc

  • How to I turn off tip notifications?

    Skype is posting up tips in my notification bar. How do I prevent this from happening?