From: Amr on
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");
}
}

}
From: RedGrittyBrick on
On 15/02/2010 16:02, Amr wrote:
> 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");
> }
> }
>
> }

From: Amr on
On Feb 15, 10:10 pm, RedGrittyBrick <RedGrittyBr...(a)spamweary.invalid>
wrote:
> On 15/02/2010 16:02, Amr wrote:
>
>
>
> > 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 :(
From: Lew on
Amr wrote:
> 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?

--
Lew
From: RedGrittyBrick on
On 15/02/2010 17:25, Amr wrote:
> On Feb 15, 10:10 pm, RedGrittyBrick wrote:
>> On 15/02/2010 16:02, Amr wrote:
>>
>>
>>> 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).
>>> --------------------------------------------------
>>
>>> public class AL2 extends JPanel implements ActionListener{
>>
>>> public AL2(){
....
>>> JPanel pane=new JPanel(new MigLayout("Wrap 1"));
>>
>> Same mistake as before! See my reply in your earlier thread.
>>
>
> 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.