Imitating a JFrame extended program with JPanel; help needed...

A

Amr

hi all,
there is a simple prog in the book "Sams teach ..." which extends
JFrame. its an example for actionListener.

since i heard i cant apply lookandfeel() for JFrame, i wanted to write
the programe extending JPanel.
the code i wrote correctly gets compiled. but nothing is getting
displayed ( i mean no buttons popping out).

please can you check whats the prob?

below are the two codes. (first one from the book extending JFrame and
next mine extending JPanel)

thank you for your time and support.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package DayEleven;

/**
*
* @author arshad
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class AL extends Frame implements WindowListener,ActionListener
{


TextField text = new TextField(20);
Button b;
private int numClicks = 0;




public static void main(String[] args) {

AL myWindow = new AL("My first window");

myWindow.setSize(350,100);
// myWindow.lookAndFeel();
myWindow.setVisible(true);

}

public AL(String title) {

super(title);
setLayout(new FlowLayout());
addWindowListener(this);


b = new Button("Click me");
add(b);
add(text);
b.addActionListener(this);

lookAndFeel();


}

public void lookAndFeel(){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){
System.out.println("errr");
}
}



public void actionPerformed(ActionEvent e) {
numClicks++;
text.setText("Button Clicked " + numClicks + " times");


}

public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}

public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}


}

--------------------------------------------------

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;

/**
*
* @author arshad
*/
public class AL2 extends JPanel implements ActionListener{

JTextField countText=new JTextField();
JButton button=new JButton("Click to increment");
private int numClicks=0;

public AL2(){

super();
themes();
Dimension
d=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setSize(d);
JPanel pane=new JPanel(new MigLayout("Wrap 1"));

pane.add(countText);
pane.add(button);
add(pane);
button.addActionListener(this);
setVisible(true);

}

public void actionPerformed(){
numClicks++;
countText.setText("Button CLicked"+numClicks+"Times");
}

public void actionPerformed(ActionEvent arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void main(String arg[]){
AL2 a=new AL2();
}

public void themes(){

try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);}catch (Exception e)
{
System.out.println("errror in applying the theme");
}
}

}
 
R

RedGrittyBrick

hi all,
there is a simple prog in the book "Sams teach ..." which extends
JFrame. its an example for actionListener.

since i heard i cant apply lookandfeel() for JFrame, i wanted to write
the programe extending JPanel.
the code i wrote correctly gets compiled. but nothing is getting
displayed ( i mean no buttons popping out).

please can you check whats the prob?

--------------------------------------------------

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;

/**
*
* @author arshad
*/
public class AL2 extends JPanel implements ActionListener{

JTextField countText=new JTextField();
JButton button=new JButton("Click to increment");
private int numClicks=0;

public AL2(){

super();
themes();

If I remember correctly, Look & Feel has to be applied before any GUI
code runs.

Dimension
d=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setSize(d);
JPanel pane=new JPanel(new MigLayout("Wrap 1"));

Same mistake as before! See my reply in your earlier thread.
pane.add(countText);
pane.add(button);
add(pane);
button.addActionListener(this);
setVisible(true);

}

public void actionPerformed(){
numClicks++;
countText.setText("Button CLicked"+numClicks+"Times");
}

public void actionPerformed(ActionEvent arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void main(String arg[]){
AL2 a=new AL2();
}

public void themes(){

try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);}catch (Exception e)
{
System.out.println("errror in applying the theme");
}
}

}
 
A

Amr

hi all,
there is a simple prog in the book "Sams teach ..." which extends
JFrame. its an example for actionListener.
since i heard i cant apply lookandfeel() for JFrame, i wanted to write
the programe extending JPanel.
the code i wrote correctly gets compiled. but nothing is getting
displayed ( i mean no buttons popping out).
please can you check whats the prob?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;
/**
  *
  * @author arshad
  */
public class AL2 extends JPanel implements ActionListener{
     JTextField countText=new JTextField();
     JButton button=new JButton("Click to increment");
     private int numClicks=0;
     public AL2(){
         super();
         themes();

If I remember correctly, Look & Feel has to be applied before any GUI
code runs.
         Dimension
d=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
         setSize(d);
         JPanel pane=new JPanel(new MigLayout("Wrap 1"));

Same mistake as before! See my reply in your earlier thread.


         pane.add(countText);
         pane.add(button);
         add(pane);
         button.addActionListener(this);
         setVisible(true);
     }
     public void actionPerformed(){
         numClicks++;
         countText.setText("Button CLicked"+numClicks+"Times");
     }
     public void actionPerformed(ActionEvent arg0) {
         throw new UnsupportedOperationException("Not supported yet.");
     }
     public static void main(String arg[]){
         AL2 a=new AL2();
     }
     public void themes(){
try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
         SwingUtilities.updateComponentTreeUI(this);}catch (Exception e)
{
             System.out.println("errror in applying the theme");
         }
     }

thank you very much for your reply, i removed lookandfeel.
but still nothing is displayed :(
 
L

Lew

Amr said:
since i [sic] heard i [sic] cant apply lookandfeel() for JFrame, ...

Where did you hear that? It's not true. As Andrew Thompson and John B.
Matthews have told you, L&F works with 'JFrame' and other Swing components.
It does not work with AWT components, such as 'Frame'.

There is a Swing tutorial on java.sun.com. Have you read it?
 
R

RedGrittyBrick

thank you very much for your reply, i removed lookandfeel.
but still nothing is displayed :(

Your variable 'pane' is not a reference to an instance of AL2, it is not
connected with the "new AL2()" you instantiate.

Please read my previous replies more carefully!

Also you don't seem to have a JFrame! You need one!

Have a look at my simple example in an earlier reply.
 
L

Lew

RedGrittyBrick said:
If I remember correctly, Look & Feel has to be applied before any GUI
code runs.

Not true, according to
Once the look and feel has been changed it is imperative
to invoke updateUI on all JComponents. The method
SwingUtilities.updateComponentTreeUI(java.awt.Component)
makes it easy to apply updateUI to a containment hierarchy.
Refer to it for details. The exact behavior of not invoking
updateUI after changing the look and feel is unspecified.
It is very possible to receive unexpected exceptions,
painting problems, or worse.

See
<http://java.sun.com/javase/6/docs/a...tml#updateComponentTreeUI(java.awt.Component)>
 
A

Amr

Have a look at my simple example in an earlier reply.

hi,
if you don't mind can you please give me the link for the thread you
are referring to.
i couldn't find which thread you are referring.
thanks a lot.
 
A

Amr

thank you all,
the program somewhat working now after making some changes from the
given suggestion.
i have to go through the suggestions again to see how can i optimize.
thank you very much.


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;

/**
*
* @author arshad
*/
public class AL2 extends JFrame implements ActionListener{


JTextField countText=new JTextField(20);
JButton button=new JButton("Click to increment");
private int numClicks=0;

public AL2(){

super();
themes();


// Dimension
d=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
// setSize(d);
JPanel pane=new JPanel(new MigLayout("Wrap 1"));
button.addActionListener(this);
pane.add(countText);
pane.add(button);
add(pane);

setVisible(true);
pack();

}

public void themes(){

try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);}catch (Exception e)
{
System.out.println("errror in applying the theme");
}
}

public void actionPerformed(ActionEvent e){
numClicks++;
countText.setText("Button CLicked"+numClicks+"Times");
}



public static void main(String arg[]){

AL2 a=new AL2();
}



}


PS: is there any way in this group that each and every mail lands in
my email box, so that i can reply from there itself without coming to
the webpage?
(i don't like the one or two emails per day. i can't wait untill the
end of the day to recieve the mail)
thank you :)
 
M

markspace

Amr said:
PS: is there any way in this group that each and every mail lands in
my email box, so that i can reply from there itself without coming to
the webpage?


Thunderbird (from Mozilla.com) will read newsgroups, as will most other
email readers. I use www.eternal-september.org as the new feed since
ATT recently discontinued support for newsgroups.
 
L

Lew

Amr said:
the program somewhat working now after making some changes from the
given suggestion.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;

public class AL2 extends JFrame implements ActionListener{

JTextField countText=new JTextField(20);

Why do you use package-private (a.k.a. "default") access instead of private?
JButton button=new JButton("Click to increment");
private int numClicks=0;

Initialization of a member variable to zero is redundant, causing zero to be
assigned to it twice. (The same holds true of initialization of a reference
member to 'null'.) The performance impact is negligible, though, and some
will argue that the explicit initialization is useful for code maintainers.
public AL2(){

super();

The explicit invocation of the no-argument 'super()' constructor is completely
unnecessary.
themes();


// Dimension
d=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
// setSize(d);
JPanel pane=new JPanel(new MigLayout("Wrap 1"));
button.addActionListener(this);
pane.add(countText);
pane.add(button);
add(pane);

setVisible(true);
pack();

The call to 'pack()' should come before the call to 'setVisible()'.
}

public void themes(){

try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);}catch (Exception e)
{
System.out.println("errror in applying the theme");
}
}

Please use conventional indentation to keep your code readable.
public void actionPerformed(ActionEvent e){
numClicks++;
countText.setText("Button CLicked"+numClicks+"Times");
}

public static void main(String arg[]){
AL2 a=new AL2();
}
}

You continue to perform GUI operations not on the EDT. This is dangerous and
will lead to weird bugs. Several folks have mentioned this to you.
PS: is there any way in this group that each and every mail lands in
my email box, so that i [sic] can reply from there itself without coming to
the webpage?

No. This is a newsgroup, not a web page. The posts are not emails, but
newsgroup posts. It's like the difference between a paper letter that you
send via the post office, and an index card announcement that you post on a
community bulletin board.

Don't use a web interface, use a newsreader instead. markspace mentioned
Mozilla Thunderbird, a decent one although not necessarily the best.

You will need access to a news server if you use a newsreader.
 
A

Arshad

thank you guys for the help and suggestions.
i greatly appreciate this.
Please use conventional indentation to keep your code readable.

is that mean, i should delete all the tabs indents infront of the
lines?
(i left it as it was in netbeans ide, bcz i heard from some one that
using tabs is a good programming practice :confused:)
You continue to perform GUI operations not on the EDT.  This is dangerous and
will lead to weird bugs.  Several folks have mentioned this to you.

actually i didnt understand what an EDT is.
i will do some searching/reading about this and will try to stick to
that.

thank you.
 
L

Lew

Please attribute when you're quoting.
is that mean, i should delete all the tabs indents infront of the
lines?

For Usenet, yes - TAB characters get rendered very wide and newsreaders tend
to break and wrap lines with relatively narrow margins. This makes source
listings on Usenet very hard to read and decipher when TAB characters indent
lines. Particularly when you're asking for help, you don't want to make your
code examples hard to read.
(i left it as it was in netbeans ide, bcz [!?] i heard from some one that
using tabs is a good programming practice :confused:)

Be careful whom you listen to. There's a lot of bad advice out there.

Nearly every programming project I have been on standardized on spaces, not
TAB characters for indentation. You can control indentation precisely with
spaces. Not so with TABs - you're at the mercy of different settings for TAB
rendition in different editors and different workstations. Plus, TABs tend to
render far too wide by default.

As for leaving it the way it was in NetBeans, that is a circular argument.
Since you chose how it was in NetBeans, you are effectively saying you chose
to leave it as you chose to leave it.
actually i didnt understand what an EDT is.
i will do some searching/reading about this and will try to stick to
that.

EDT is the Event Dispatch Thread. Read the Swing tutorial on java.sun.com.
Java is a multi-threaded language - you have the power and the responsibility
to coordinate threads. Graphics instructions need to occur on a single
singular thread called the "EDT" in order for things to coordinate properly.
This is introduced in the Swing tutorial on java.sun.com. If you don't use
the EDT properly you'll get weird bugs, as with all threading mistakes.
 
R

RedGrittyBrick

thank you guys for the help and suggestions.
i greatly appreciate this.


is that mean, i should delete all the tabs indents infront of the
lines?

No, have your IDE convert those tabs to multiple spaces.

I configure my IDE to use spaces (not tab characters) for indentation
and I set the indentation to 4 spaces. Sometimes I reduce indentation
to two spaces for posting to usenet. Your IDE can quickly reformat your
code for you.
(i left it as it was in netbeans ide, bcz i heard from some one that
using tabs is a good programming practice :confused:)

It's an ancient debate carried over from previous millenia.
http://www.jwz.org/doc/tabs-vs-spaces.html

I find tab characters hinder far more than they help. This is especially
true on usenet.
 
A

Arne Vajhøj

For Usenet, yes - TAB characters get rendered very wide and newsreaders
tend to break and wrap lines with relatively narrow margins. This makes
source listings on Usenet very hard to read and decipher when TAB
characters indent lines. Particularly when you're asking for help, you
don't want to make your code examples hard to read.

I think you need to emphasize that tabs should not be deleted but
replaced with spaces (usually 4 per tab).
(i left it as it was in netbeans ide, bcz [!?] i heard from some one that
using tabs is a good programming practice :confused:)

Be careful whom you listen to. There's a lot of bad advice out there.

Nearly every programming project I have been on standardized on spaces,
not TAB characters for indentation. You can control indentation
precisely with spaces. Not so with TABs - you're at the mercy of
different settings for TAB rendition in different editors and different
workstations. Plus, TABs tend to render far too wide by default.

I am all for spaces.

The only argument for tabs that I know of is that it allows different
users to use different indentation.

Which is a nice theory.

But the reality is that people always use some spaces and when
changing the tab definition the result looks horrible.

Arne
 
A

Amr

No.  This is a newsgroup, not a web page.  The posts are not emails, but
newsgroup posts.  It's like the difference between a paper letter that you
send via the post office, and an index card announcement that you post on a
community bulletin board.

Don't use a web interface, use a newsreader instead.  markspace mentioned
Mozilla Thunderbird, a decent one although not necessarily the best.

You will need access to a news server if you use a newsreader.

OK. I'm going to configure this on evolution. I gave USENET as server
type. But what should I give for the server name?

Thank you very much.
 
R

RedGrittyBrick

OK. I'm going to configure this on evolution. I gave USENET as server
type. But what should I give for the server name?

Your Internet Service Provider (ISP) may provide an NNTP server (check
the support section of your ISP's web-pages for info).

If your ISP doesn't provide one, you can buy service from a commercial
provider or you can use one of the free providers. Try googling for
"free nntp" and other combinations to see if there is one in your region.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,521
Latest member
maryb

Latest Threads

Top