Results 1 to 6 of 6
Thread: Constructor call not calling
- 06-08-2009, 08:08 AM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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:
The class calling the constructorJava 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(""); } } } }
Seems to be a very strange problem. Never seen anything like this.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(); } }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!
- 06-08-2009, 09:18 AM #2
Member
- Join Date
- Apr 2009
- Posts
- 9
- Rep Power
- 0
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 ...
- 06-08-2009, 09:38 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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 caseIf 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!
- 06-08-2009, 10:09 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Run your code through a debugger. If I remember right, this line
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.Java Code:private ScrollableJTextArea allMessages = new ScrollableJTextArea(), newMessages = new ScrollableJTextArea();
- 06-08-2009, 09:18 PM #5
I just ran this
and got this in the consoleJava 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 a small gui appeared with three components in it.Java Code:C:\jexp>java ConstructorTest Checkpoint: Constructor call Checkpoint: txtPanel allocated TAUI construct Layout BPanel Added Checkpoint: txtPanel initialized Checkpoint: panel setup
- 06-09-2009, 01:06 AM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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
-
Constructor calling
By ravian in forum New To JavaReplies: 2Last Post: 12-22-2007, 06:53 PM -
calling another call file
By Yaya B in forum New To JavaReplies: 1Last Post: 12-21-2007, 06:11 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks