Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Simple Java user interface questions.

Thread view: 
ri.johnson@juno.com - 19 Dec 2005 15:16 GMT
Just starting out in Java and I have a few questions that someone could
help me resolve I would be most appreciative.

- Is there a way to control the case of text typed into a JTextField
and JComboBox components?  If I wanted to force only uppercase is that
possible?  I realize that I can convert the string afterwards but I
want the user to only see uppercase.  Or is there another component
that has the same functionality that does the same thing?

- Is there a way to only allow numieric characters in a JTextField
component?  In addition is there a way to apply masks for both input
and output of the data?

- Is there a way to programatically clear the check from a JCheckBox
component?

- How do you close an object by using a JButton or Check box similar to
the JFrame.EXIT_ON_CLOSE functionality?

- Is there a way to not opaque a JTextField and JComboBox text when the
fields are not enabled?

- When working with menu items how can I determine easily within the
actionPerformed meathod what menu item was selected.  After calling the
getSource()  method for buttons one can programmatically check for what
button was called by checking the object against the list of button
object names.  Unfortunatly I have not been successfull with this on
menu items.  The compiler fails and indicates that the submenu item is
not defined.  Any suggestions?

Thank you in advance for your assistance on the above.

Russell
oulan bator - 19 Dec 2005 15:51 GMT
hi,

>Is there a way to control the case of text typed into a JTextField
>and JComboBox components?  If I wanted to force only uppercase is that
>possible?
yes, have a look at JFormattedTextField

>Is there a way to only allow numieric characters in a JTextField
>component?  In addition is there a way to apply masks for both input
>and output of the data?

idem

>Is there a way to programatically clear the check from a JCheckBox
>component?

yes,  public void setSelected(boolean b)

>How do you close an object by using a JButton or Check box similar to
>the JFrame.EXIT_ON_CLOSE functionality?

I don't understand

>Is there a way to not opaque a JTextField and JComboBox text when the
>fields are not enabled?

I don't understand

>- When working with menu items how can I determine easily within the
>actionPerformed meathod what menu item was selected.  After calling the
[quoted text clipped - 3 lines]
>menu items.  The compiler fails and indicates that the submenu item is
>not defined.  Any suggestions?

I suggest that you have a look at the object javax.swing.Action, Its a
good way to start building GUI. I recommend that you do not fall in the
trap to add actionListeners to buttons, and that you write your own
menuitem. In java it's higly recommended that you use reflexion to
perform those boring tasks.

regards
ri.johnson@juno.com - 19 Dec 2005 20:18 GMT
Thank you for the advice.  On the check box's I am using the
button.setSelected(false) mehtod in order to remove the check from the
screen but the check is still painted.  I tried using the doClick
method after the setSelected method but that had no affect either.  The
screen still has the "check mark" in the box.
ri.johnson@juno.com - 19 Dec 2005 20:23 GMT
I am using the setSelected(false) method on the checkboxs to clear the
check mark off the screen but the checkmark remains.  I have even
experimented with using the doClick method again with no luck.  Since I
issue the repaint() method after resetting all the fields is there
something else I have to do in order to remove the check mark from the
screen?
Thomas Hawtin - 19 Dec 2005 22:07 GMT
> I am using the setSelected(false) method on the checkboxs to clear the
> check mark off the screen but the checkmark remains.  I have even
> experimented with using the doClick method again with no luck.  Since I
> issue the repaint() method after resetting all the fields is there
> something else I have to do in order to remove the check mark from the
> screen?

It should clear the tick. Below is some code to repeatedly select and
deselect a tick box. There should be no need to repaint.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class Check {
    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    swing();
                }
        });
    }
    private static void swing() {
        assert java.awt.EventQueue.isDispatchThread();
        JFrame frame = new JFrame("Check");
        final JCheckBox checkBox = new JCheckBox("Check box");
        frame.add(checkBox);
        frame.pack();
        frame.setVisible(true);
        new javax.swing.Timer(2000, new ActionListener() {
                private boolean set;
                public void actionPerformed(ActionEvent event) {
                    checkBox.setSelected(set);
                    set = !set;
                }
       }).start();
   }
}

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Thomas Hawtin - 19 Dec 2005 16:07 GMT
> - Is there a way to control the case of text typed into a JTextField
> and JComboBox components?  If I wanted to force only uppercase is that
[quoted text clipped - 5 lines]
> component?  In addition is there a way to apply masks for both input
> and output of the data?

A good way to achieve these two is to use a DocumentFilter. I don't know
why this sort of thing isn't a standard part of Swing. There is
JFormattedTextField, but that is just an amazing piece of crap.

There is an example of a document filter in the createNumberDocument
method of the source linked.

http://jroller.com/resources/t/tackline/PayCalculator.java
http://jroller.com/resources/t/tackline/PayCalculator.jnlp

> - Is there a way to programatically clear the check from a JCheckBox
> component?

Use setSelected, which is hidden in AbstractButton (and ButtonModel).

> - How do you close an object by using a JButton or Check box similar to
> the JFrame.EXIT_ON_CLOSE functionality?

Add an ActionListener or ItemListener. Put code in the listener to do
whatever you want (like System.exit(0);).

> - Is there a way to not opaque a JTextField and JComboBox text when the
> fields are not enabled?

You mean to stop them greying out when disabled? For text components you
can use setEditable (the method is present in JComboBox, but means
something entirely different). The general solution is to use a custom
model (or filter) implementation that can be disabled, without disabling
the component.

> - When working with menu items how can I determine easily within the
> actionPerformed meathod what menu item was selected.  After calling the
[quoted text clipped - 3 lines]
> menu items.  The compiler fails and indicates that the submenu item is
> not defined.  Any suggestions?

What do you mean by "The compiler fails and indicates that the submenu
item is not defined."?

Probably the easiest thing to do is to use a different ActionListener
anonymous class for each menu item. There is also setActionCommand.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

VisionSet - 19 Dec 2005 19:06 GMT
> There is JFormattedTextField, but that is just an amazing piece of crap.

I would like to second that!
They managed to make something that is actually fairly complex every bit as
complex and less flexible at the same time. No one's saying that writing
library code is easy, but I wish they hadn't bothered, I think Mr
GregorianCalendar got the job!

Go with Document model.

--
Mike W
ricky.clarkson@gmail.com - 19 Dec 2005 19:10 GMT
> - When working with menu items how can I determine easily within the
> actionPerformed meathod what menu item was selected.  After calling the
[quoted text clipped - 3 lines]
> menu items.  The compiler fails and indicates that the submenu item is
> not defined.  Any suggestions?

Well, if you use one ActionListener implementation per menu item, then
there is no need to test.

E.g.,:

item.addActionListener(new FileNewListener());

Then in another file:

final class FileNewListener implements ActionListener
{
  public void actionPerformed(final ActionEvent event)
  {
     No need to test, I know the user clicked on 'item'.
  }
}

The reason for your compile error was most likely scope:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/scope.html

I don't see any reason to make these ActionListener implementations
anonymous inner classes like some other poster suggested.  Anonymous
inner classes introduce their own complications, and discourage reuse
of code.
ri.johnson@juno.com - 19 Dec 2005 20:20 GMT
Thank you for the advice the code where the action listener is
associated with a specific class works perfectly.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.