Simple Java user interface questions.

R

ri.johnson

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
 
O

oulan bator

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
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?

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
 
T

Thomas Hawtin

- 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?

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
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?

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
 
V

VisionSet

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.
 
R

ricky.clarkson

- 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?

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.
 
R

ri.johnson

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.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top