Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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
 
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
 
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
 
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




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





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