custom text constrains in JTextField - java

How do I add custom text constrains to JTextField?
For example, I would like users to provide input in such format:
12-100
78-023

Have you tried using a JFormattedTextField? Sounds like just what you need.

Related

Flexible Swing Input Dialog

I want to create a dialog box that consists of one column of field titles and another column of fields. I want to be able to specify the fields to only allow specific types of data. Using swing, what is the most idiomatic way of creating a reusable class for accomplishing this?
A shown in Validating Input, you can use an InputVerifier to restrict input; a complete example is examined here. In the particular case of columnar data, a JTable can choose the editor based on the underlying model's data type—the value returned by getColumnClass(). You can let the editor validate user-entered text in your implementation of stopCellEditing(), illustrated here. In either case, you can add a panel containing the input component(s) to a dialog, as outlined here.

Changing the font of a text in a JTextPane dynamically

I want to change the certain texts written on a JTextPane dynamically. I have a String array containing the words which should be changed
String ListMethod [] = {"forward", "backward", "left", "right"};
I've gone through some posts and many suggest to use JTextPane or JEditorPane to edit text but most of the answers given work on static text. I want to do it in a way such that as I type "forward" or "backward", etc... in the textpane, it detects this words and changes the color. How can I go about it?
Thanks for your help.
See here how to implement a DocumentListener Value Change Listener to JTextField. The have a look at javax.swing.text.Highlighter and javax.swing.text.HighlightPainter.
You have to capture the appropriate event and perform actions. For example in your case you can create an ActionListener that changes color and use registerKeyBoardAction to attach it on your JTextPane.
Oracle has a good tutorial: http://docs.oracle.com/javase/tutorial/uiswing/events/index.html on event listeners. I suggest you start getting yourself familiar there

Get input values from JComboBox

How can I get the input for an editable JComboBox. When user gives an input to the combo how I can get the input text from it?
You need to get the edited text from the combobox editor via combo.getEditor().getItem().
If you need the text that is selected on a JComboBox and you are sure it's a String and not any other object, just use something like String text = (String)myCombobox.getSelectedItem().
If the thing you have in your Model is other than a String, then you need to cast it to the appropriate class, and then use the toString() method of that object.
If you need more help, you should paste a bit of your code, at least declaration and inicialization of your JComboBox...
Just have a look at the oracle tutorial. They do explain how to handle the common swing components http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html

how to set text to a jcombobox, but the text set should not be an element of jcombobox

is their any way to set text for a combo box, which is not an element of the combobox.
cboSubjects=new JComboBox();
cbo.addItem("Maths"); // and few more subjects are added
cbo.setSelectedItem("subjects"); // this does not set the default text of combobox
Is their any way to solve this problem ? I need something which works like combobox.text
property of combobox in visual basic
I am working on school management system. I need help.
I'd like to show you a possible alternative :)
Note, you need to call label.setDisplayedMnemonic('s'); and label.setLabelFor(combo) to complete the effect!
You can make jComboBox editable, cbo.setEditable(true);, and after that set the text you want: cbo.setSelectedItem("subjects");

How to change the String Style

Can we change the String Style in java from normal to bold...
Example: I wish to change the String "Name" from normal Style to bold Style. Any default
method included in java?
The question is a bit vague, but in Swing you can use HTML in order to do format the way your strings appear in components:
jLabel1.setText("<html><b>Bold text</b></html>");
jButton1.setText("<html><b><i>Bold and Italic text</i></b></html>");
A string is just the representation of some text. Applying some style information depends on the GUI framework you use: Swing, AWT, JSP, etc...
It depends where do you want to change its style ?
If you are using Swings' components or AWT or something like that then surely you can do it.
A String object in Java just holds the data. It does not have any styling associated with it. Display styles are associated with the presentation components. You sure are missing something.
If you are using a GUI System like a JLabel, JTextField etc, then you can go two ways:
eg:
JLabel myLabel = new JLabel("<html><b>My Text");
or
Font F = new Font("Calibre",Font.BOLD,12);
myLabel.setFont(f);
Hope that answers you Question.
There are two ways,
Either create a font by yourself ,
something like, Font abc = new Font("Arial",font.BOLD,15)
and for any of the components you can use the setFont() method to set the font for the component.
See the javadoc for more
Use html,
Java supports html formating on all of its swing components.
Here is How to Use HTML in Swing Components.
Any default method included in java?
So I suppose for you its better to use a font, while setting the style via html means, if only for that text ,its not default for the component.
This is a generalized answer, for more specific answer,ask more specific question :)

Categories