Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm creating frame which asks user to enter his information. I want to notify user using labels or something else when he does not give required input. Like here is the screenshot when all fields are empty:
As when wrong input is entered and i move to the next text field text appears in red color below the text field. here is the screenshot:. i want to do this. Does anyone tell me how to do this..????
There are few ways to achieve this...
The first thing you need to do is valid the field...
You Could...
Use an InputVerifier which allows to validate a field when it loses focus as demonstrated here.
This method also allows you to determine if focus should be continue to the next field or remain with the current field.
You Could...
Use a FocusListener and valid the field when focus is lost. This is pretty much the same thing as the above suggestion, just know you have to do the work yourself
You Could...
Use a DocumentListener on text fields to monitor changes to the fields contents and perform real time validation of the field.
Next, you need to determine the best way to display the state...
You can use:
A LineBorder to change the field's border state. Be careful though, some look and feels won't like you doing this (looking at you MacOS). Take a look at How to use borders for more details
The tool tip API
setVisible to toggle the visibility of JLabels to display error messages, but to be honest, I might be tempted to use a full alpha color to hide the text and a full opaque color to show as this won't effect the layout
Ballon Tip
GridBagLayout would probably be my choice of layout manager, but you could also use compound layouts to make things eaiser
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to use a JSlider to display my data, and as such I don't want users to be able to move them as then it would no longer display the correct value. At the moment I'm disabling the JSlider so the user can't move the slider, but doing that makes the visibility of the slider really poor.
I would recommend against showing an enabled, but inspirational slider, as it is against typical UI conventions and will likely confuse the users.
Nonetheless, if you really want to do it: The default JSlider is either enabled or not, so you cannot do what you want directly. A workaround would be to have it enabled and add an ActionListener to it. This would be called once the user changes the value. In this ActionListener, you could just reset the value of the slider to its original value, so the slider would snap back. Again, this is very atypical behaviour of UI elements and might confuse users.
In the end, I would suggest coming up with your own component that displays a bar or other slider-like element to visualize your values, but which do not accept any user input.
I made a landing interface in Java using NetBeans. I want to set when the textfield input format does not meet the requirements, then Red tips occur, how to achieve this? I know it is related to focus event, but I do not know the details
I suggest that use ActionEvent and ActionListener for textfield.
then use textfield.getText() to get String data.
you can now examine the String's content by separate it.
If you wanna check the format is a number or not, just use NumberFormatException.
https://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html
after that,
you want a red warning tip, use label with color red, and .setVisible(boolean) to control it shows or not.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
If I am taking a large amount of input from a user lets say the favorite colour of a student in a classroom of 30 students.
I want the teacher to input each favorite colour. I dont want to ask the teacher for each name of the student in showDialogBox then search a file to show that the student exists then ask to input the colour.
Is there a way of displaying a Box which looks something like this
John-(blank space to enter txt eg.colour)
James-(blank space to enter colour)
Or something like that, or would there be a better more convienient way for the user to input the data.
Then what comes to mind is changing a colour for a student.
Any help in pointing in the right direction would be much appreciated.
I would use a JFrame/JPanel
Frame:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html
Panel:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JPanel.html
Yes, there are many ways. Some of your many options are:
Create a custom JPanel based component that has a label to display the name and a text input box. Read the student names and dynamically add one of these components per student to your form.
Use a JTable that has a column for the name and an editable column for the color (see http://docs.oracle.com/javase/tutorial/uiswing/components/table.html for a tutorial on creating tables with editable input).
Iterate through student names and display a dialog box for each. Rather than having the teacher enter the names, display the names from the file and just have the teacher enter the color.
The first two options require you to build your own input window, which would likely be a JFrame based component. The last option lets you use JOptionPane without having to do any of your own interface coding.
You should try one of these ways, or maybe some other way that you think of, then if you are having a specific problem, come back here and ask.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to develop an editor without scrollbars using jtextarea, so if the text is too long to fit within jtextarea it should be splitted into smaller substrings which could be edited within the jtextarea without showing vertical scrollbar because it is easy to get rid of horizontal one by using setLineWrap and setWrapStyleWord. I have tried to use vertical scrollbar adjusmentListener to listen to its changes but it doesnt work because jtextarea append and setText methods activated at the end of business logic, so I tried to use multithreaded and SwingWorker to invoke these methods but also doesnt work. I also tried to invoke repaint, revalidate and update methods with no hope. please help me to pass this issue and thanks in advance.
As Hovercraft Full Of Eels already suggested, you have to take a look at the Document, DocumentFilter and/or DocumentListener interfaces.
By adjusting those you have full control over what text is displayed when somebody wants to append text. You could for example remove the first part of the text before/after appending new text, hence limiting the number of lines/characters in the document.
The Swing tutorial about textcomponents contains such an example in the Implementing a DocumentFilter section, where the DocumentSizeFilter class is the one you are looking for (not part of the JDK, part of the Swing tutorial code). As already suggested as a comment in that sample code, it would be an option to remove the first part of the document when appending new text which would make the contents too long, but I leave that up to you since it is tagged as homework.
I am trying to develop an editor without scrollbars using jtextarea, so if the text is too long to fit within jtextarea it should be splitted into smaller substrings which could be edited within the jtextarea without showing vertical scrollbar because it is easy to get rid of horizontal one by using setLineWrap and setWrapStyleWord.
I'm not sure I fully understand this. Perhaps you can explain further? Why not place the JTextarea inside of a JScrollPane? What is your desired behavior if the text it contains is greater than that which the JTextArea can display?
I have tried to use vertical scrollbar adjusmentListener to listen to its changes but it doesnt work because jtextarea append and setText methods activated at the end of business logic, so I tried to use multithreaded and SwingWorker to invoke these methods but also doesnt work. I also tried to invoke repaint, revalidate and update methods with no hope.
If you want to trap entered text before it is committed to the text component, consider setting a DocumentFilter to the JTextArea's associated Document (a PlainDocument).
Thank you both Hovercraft and Robin I got it, your answers trend me to the correct way, I found the accurate answer at "Core Swing: advanced programming" book by using JTextArea.modelToView() passing the JTextArea Document Length as parameter this method returns a Rectangle object whose coordination represent the final character coordination and then compare these coordination with the bottom corner of the JTextArea to see if this final char reachs to this bottom or not.
Sorry for the odd choice of words for the title, however, "border" seems to be the inappropriate term. While it is true that the visible line surrounding an icon in a JToggleButton can be made invisible by using setBorderPainted(false), the same is not true for JCheckBox and JRadioButton.
I can not use the JToggleButton and therefore need to use either the JCheckBox or JRadioButton (or some derivative of JToggleButton I am not aware of), but need the square or circle, respectively, to be non-visible when there is no icon on the button. Also, using setVisible(false) eliminates the button from the layout, however, I need the space to be reserved and not have the component layout change (using GroupLayout).
Any suggestions? Am I going to have to create a custom renderer? I will be looking at that in the mean time.
The route into this would be through customising the look at feel by changing some of the UI properties in the UImanager (the sort of thing that allows you to make simple tweaks with fonts and colours and presumably the images used for the checkboxes or radiobuttons) -- but it's many years since I last did that sort of thing and can't remember the details.
A little Googling turned up this project to inspect current property values, so might at least help with indicating the right part of the APIs to be looking at.
You have to choices here:
1) Customize Look and Feel as described in previous entry.
2) Create your own custom controls by inheriting from existing ones and overriding component painting.
I found a cheap and easy (read hack) for this. I created an empty transparent icon and used it when I didn't want any item to be displayed.