Results 1 to 6 of 6
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default Constructor call not calling

    I am trying to call a constructor for a self-designed class. However, when I use new TextAreaUI, nothing happens. The println statement at the beginning of the constructor does not work either... any ideas?

    TextAreaUI class:
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class TextAreaUI extends JPanel implements ActionListener{
    
    	private static final long serialVersionUID = 1L;
    	private ScrollableJTextArea allMessages = new ScrollableJTextArea(), newMessages = new ScrollableJTextArea(); 
    	private JButton send = new JButton("Send");
    	
    	public TextAreaUI(){
    		[color=red][b]System.out.println("TAUI construct");[/b]//Never prints[/color]
    		setLayout(new SpringLayout());
    		System.out.println("Layout");
    		JPanel bottomPanel = new JPanel(new SpringLayout());
    		bottomPanel.add(newMessages);
    		bottomPanel.add(send);
    		System.out.println("BPanel");
    		SpringUtilities.makeCompactGrid(bottomPanel, 1, 2, 0, 0, 0, 0);
    		JPanel topPanel = new JPanel();
    		topPanel.add(allMessages);
    		add(topPanel);
    		add(bottomPanel);
    		System.out.println("Added");
    		SpringUtilities.makeCompactGrid(this, 2, 1, 4, 4, 4, 4);
    	}
    	
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == send){
    			if(newMessages.getText().length() != 0){
    				allMessages.append("\n\n"+newMessages.getText());
    				newMessages.setText("");
    			}
    		}
    	}
    }
    The class calling the constructor
    Java Code:
    import javax.swing.*;
    
    import graphics.images.Movement;
    
    public class MainUI extends JFrame{
    
    	private static final long serialVersionUID = 1L;
    	private JPanel panel = new JPanel();
    	public MainUI(){
    		System.out.println("Checkpoint: Constructor call");
    		TextAreaUI txtPanel;
    		System.out.println("Checkpoint: txtPanel allocated");
    		[b][color=red]txtPanel = new TextAreaUI();[/color][/b][color=red]//BLOCKS[/color]
    		System.out.println("Checkpoint: txtPanel initialized");
    		panel.add(txtPanel);
    		System.out.println("Checkpoint: panel setup");
    		setContentPane(panel);
    		pack();
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    	}
    	public static void main(String[] args){
    		new MainUI();
    	}
    }
    Seems to be a very strange problem. Never seen anything like this.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  2. #2
    Join Date
    Apr 2009
    Posts
    9
    Rep Power
    0

    Default

    How does your TextAreaUI code compile? It does not import the SpringUtilities (org.concord.swing.SpringUtilities right?) class in the code?
    Also you could check if all the necessary files are on your class path. You don't seem to be using any packages ...

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Lol... picked up the SpringUtilities code online, its in the same package (never even realized it was actually in a package anywhere). The entire code is in the graphics package of my project, so I don't need imports.

    It's not an error... the constructor is just not being called.

    EDIT: and the package you mentioned is not included in the standard JDK in any case
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Run your code through a debugger. If I remember right, this line
    Java Code:
    	private ScrollableJTextArea allMessages = new ScrollableJTextArea(), newMessages = new ScrollableJTextArea();
    will be run before your println, but as part of the "new TextAreaUI()" call, and I can only assume that ScrollableJTextArea is also one of your classes, and I have no idea what might be happening in that constructor.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I just ran this
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ConstructorTest extends JFrame{
        private static final long serialVersionUID = 1L;
        private JPanel panel = new JPanel();
    
        public ConstructorTest(){
            System.out.println("Checkpoint: Constructor call");
            TextAreaUI txtPanel;
            System.out.println("Checkpoint: txtPanel allocated");
            txtPanel = new TextAreaUI();//BLOCKS
            System.out.println("Checkpoint: txtPanel initialized");
            panel.add(txtPanel);
            System.out.println("Checkpoint: panel setup");
            setContentPane(panel);
            pack();
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
        }
    
        public static void main(String[] args){
            new ConstructorTest();
        }
    }
    
    class TextAreaUI extends JPanel implements ActionListener{
        private static final long serialVersionUID = 1L;
        private ScrollableJTextArea
            allMessages = new ScrollableJTextArea("all messages"),
            newMessages = new ScrollableJTextArea("new messages");
        private JButton send = new JButton("Send");
    
        public TextAreaUI(){
            System.out.println("TAUI construct");//Never prints
            setLayout(new SpringLayout());
            System.out.println("Layout");
            JPanel bottomPanel = new JPanel(new SpringLayout());
            bottomPanel.add(newMessages);
            bottomPanel.add(send);
            System.out.println("BPanel");
            SpringUtilities.makeCompactGrid(bottomPanel, 1, 2, 0, 0, 0, 0);
            JPanel topPanel = new JPanel();
            topPanel.add(allMessages);
            add(topPanel);
            add(bottomPanel);
            System.out.println("Added");
            SpringUtilities.makeCompactGrid(this, 2, 1, 4, 4, 4, 4);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == send){
                if(newMessages.getText().length() != 0){
                    allMessages.append("\n\n"+newMessages.getText());
                    newMessages.setText("");
                }
            }
        }
    
        private class ScrollableJTextArea extends JTextArea {
            public ScrollableJTextArea(String text) {
                super(text);
            }
    
            public ScrollableJTextArea() {}
        }
    }
    and got this in the console
    Java Code:
    C:\jexp>java ConstructorTest
    Checkpoint: Constructor call
    Checkpoint: txtPanel allocated
    TAUI construct
    Layout
    BPanel
    Added
    Checkpoint: txtPanel initialized
    Checkpoint: panel setup
    and a small gui appeared with three components in it.

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Fixed it. The problem was in the ScrollableJTextArea constructor... I had a Runnable that did checking for conditions(was mistaking a Runnable for Thread).
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Constructor calling
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-22-2007, 06:53 PM
  2. calling another call file
    By Yaya B in forum New To Java
    Replies: 1
    Last Post: 12-21-2007, 06:11 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •