Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 24th, 2008, 08:36 PM
Authorized User
 
Join Date: Dec 2007
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to see a label

Hello

I hawe two clsses
I am tryin to see the label that say "CAN YOU SEE ME?"

Can anyone tell me whay can't I see it, please?

=============
First.java
=============
Code:
import javax.swing.*;

public class First extends JFrame {
  Second second;

  public static void main(String[] args) {
    new First();
  }

  public First() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    second = new Second(this);
    second.setSize(220, 150);

    setLocation(400,200);
    setSize(500, 200);
    setVisible(true);

    second.setLocationRelativeTo(this);
    second.setVisible(true);
  }
}

=============
Second.java
=============
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Second extends JDialog implements ActionListener {
    JButton button;

    public Second(Frame parent) {
        super(parent, true);
        button = new JButton("##### - = b u t t o n = - #####");
        button.addActionListener(this);
        add(button);
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == button) odpriPovezavo();
    }

    public void odpriPovezavo() {
        JDialog jDialog = new JDialog(this, "Do you see the label?", false);
        jDialog.add(new JLabel("CAN YOU SEE ME?", JLabel.CENTER));
        jDialog.setSize(300, 70);
        jDialog.setLocationRelativeTo(this);
        jDialog.setVisible(true);

        try {
            Thread.sleep(2000);
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }

        jDialog.setVisible(false);
    }
}
thank you for any help
Reply With Quote
  #2 (permalink)  
Old February 29th, 2008, 02:31 PM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

If it were me, I'd just use a timer. Like this:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Second extends JDialog implements ActionListener {
  JButton button;
  // we need these in more than one method its ok to make them class attributes
  JDialog jDialog;
  Timer t;    

  public Second(Frame parent) {
    super(parent, true);
    button = new JButton("##### - = b u t t o n = - #####");
    button.addActionListener(this);
    add(button);
    jDialog = new JDialog(this, "Do you see the label?", false);
    t = new Timer(2000,this) ; // We have a sw4nky Swing timer now :-)
  }

  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source == button) odpriPovezavo();
    else if(jDialog.isVisible()==true) {
      jDialog.setVisible(false); 
      t.stop(); // stop timer
    }   
  }

  public void odpriPovezavo() {
    jDialog.getContentPane().add(new JLabel("CAN YOU SEE ME?", JLabel.CENTER));
    jDialog.setSize(300, 70);
    jDialog.setLocationRelativeTo(this);
    jDialog.setVisible(true);
    t.start(); // start timer
  }

}
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
  #3 (permalink)  
Old March 2nd, 2008, 10:33 AM
Authorized User
 
Join Date: Dec 2007
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

what about like this:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Second extends JDialog implements ActionListener {
    JButton button;
    JDialog jDialog;  //class attributes

    public Second(Frame parent) {
        super(parent, true);
        button = new JButton("##### - = b u t t o n = - #####");
        button.addActionListener(this);
        add(button);
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == button) odpriPovezavo();

        else if(jDialog.isVisible() == true) {
            jDialog.setVisible(false); 
            //t.stop(); // stop timer
        }   
    }

    public void odpriPovezavo() {
        jDialog = new JDialog(this, "Do you see the label?", false);
        jDialog.getContentPane().add(new JLabel("CAN YOU SEE ME?", JLabel.CENTER));
        jDialog.setSize(300, 70);
        jDialog.setLocationRelativeTo(this);
        jDialog.setVisible(true);

        Timer t = new Timer(2000, this) ; // We have a sw4nky Swing timer now :-)
        t.setRepeats (false);
        t.start(); // start timer
    }
}
what do you mean with "sw4nky"

whay do we need (is it necessary):
t.stop(); // stop timer
Reply With Quote
  #4 (permalink)  
Old March 4th, 2008, 06:15 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I was trying to write s-w-a-n-k-y but, because w-a-n-k is a swearword in the opinion of the folks at wrox, it got "starred out". So the '4' is just a substitute for 'a' ;)

Nah, you don't really need the t.stop(); if you have setRepeats set false.

Cheers,
Charlie

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bind a Label cp75 ASP.NET 1.0 and 1.1 Basics 19 January 10th, 2007 10:53 AM
label in repeater boomsoftware@gmail.com ASP.NET 1.0 and 1.1 Basics 0 April 13th, 2006 09:38 AM
Label generation lily611 General .NET 8 June 6th, 2005 03:19 AM
whitespace in label Twinklestar ASP.NET 1.0 and 1.1 Basics 4 July 28th, 2004 07:37 AM
length of label lily611 General .NET 6 July 22nd, 2004 02:00 PM



All times are GMT -4. The time now is 09:16 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.