i'd like to create JTextField with input characters limited to someting like
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYWZZ0123456789+&##/%?=~_-|!:,.;"
so i tried overriding
public class CustomJTextField extends JTextField {
String goodchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYWZZ0123456789+&##/%?=~_-|!:,.;";
//... my class body ...//
#Override
public void processKeyEvent(KeyEvent ev) {
if(c != '\b' && goodchars.indexOf(c) == -1 ) {
ev.consume();
return;
}
else
super.processKeyEvent(ev);}}
but it isn't what i want because user cannot ctrl-c ctrl-v ctrl-x any more... so i addeded
&& ev.getKeyCode() != 17 && ev.getKeyCode() !=67 && ev.getKeyCode() != 86 && ev.getKeyCode() !=0 &&
to the if condition, but now the user can paste inappropriate input, ie '(' or '<', without any problem...
what can i do?
maybe better would be use DocumentFilter with Pattern,
Try a JFormattedTextField and use
MaskFormatter mf = new MaskFormatter();
mf.setValidCharacters("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYWZZ0123456789+&##/%?=~_-|!:,.;");
JFormattedTextField textField = new JFormattedTextField(mf);
Edit: Sorry, that was the wrong code, here's the working one
Related
I've seen other forums here with suggestions for solutions I don't get any of them to work.
I want to check if a cell is either null or blank, the depriciated code that I use is (both getCellType() and CELL_TYPE_BLANK is depreciated):
if( (c == null) || c.getCellType() == c.CELL_TYPE_BLANK){
//do something
}
For example I've been looking at solution in this thread:
Alternative to deprecated getCellType
and I was thinking that a solution could possibly look like this:
if( (c == null) || c.getCellTypeEnum() == CellType.BLANK){
//Error: incomparable types: org.apache.poi.ss.usermodel.CellType and int
//do something
}
or
if( (c == null) || c.getBooleanCellValue()){
//do something
}
but it does'nt work and apaches documentation is not that helpful either. Does anyone have a solution that does'nt produce warnings? I'm using poi 3.17.
BR
I've been struggling with the same issue and I found the following works for me.
It is the inverse of your approach.
I check if the value is not null.
The code below is for handling string values:
private static String checkForNullString(Cell cellToCheck) {
String strCheck;
if (cellToCheck != null && cellToCheck.getCellTypeEnum() != CellType.BLANK) {
cellToCheck.setCellType(CellType.STRING);
strCheck = cellToCheck.getStringCellValue();
}
else
strCheck = "";
return strCheck;
}
To handle numeric values, simply change the following:
cellToCheck.getStringCellValue();
to
cellToCheck.getNumericCellValue());
This what I have done so far, even though I check all of them I get AlertDialog message.
private void validateCheckBoxes() {
if (toilets.isSelected() || wifi.isSelected() || trolleys.isSelected() || lifts.isSelected()
&& ticketMachine.isSelected() || stepFree.isSelected()) {
saveRecordsToDatabase();
} else {
AlertDialog.Builder facilitiesError = new AlertDialog.Builder(AddStation.this);
facilitiesError.setTitle("Station Facilities are not selected");
facilitiesError.setMessage("Please select at least one facility ");
facilitiesError.setNegativeButton("OK", null);
facilitiesError.create().show();
}
}
By "selected" do you mean "checked"?
if (toilets.isChecked() || wifi.isChecked() || trolleys.isChecked() || lifts.isChecked()
|| ticketMachine.isChecked() || stepFree.isChecked()) {
saveRecordsToDatabase();
}
modify your code:
if (toilets.isSelected() || wifi.isSelected() || trolleys.isSelected() || lifts.isSelected()
|| ticketMachine.isSelected() || stepFree.isSelected()) {
saveRecordsToDatabase();
}
Explanation: you are using all && operators in if statement, which means you get alert message only if all the check boxes are checked.
If you use all || (or) operator, it means you get alert message if at least one checkbox is checked.
After getting an action from the KeyListener, using the event.getKeyCode() and later on the KeyEvent.getKeyText(keyCode), how would i check if the outcome of .getKeyText(keyCode) is a single character like 'a' and not a whole word like "Space" ?
How about this:
KeyEvent.getKeyText(keyCode).length == 1
You can use getKeyChar() of the KeyEvent, and then'll you be certain that what you get back is a single char.
E.g. something like this:
public void keyTyped(KeyEvent e) {
keyChar = e.getKeyChar();
...
}
Try this:-
if ((event.keyCode > 64 && event.keyCode < 91) || (event.keyCode > 96 && event.keyCode < 123) || event.keyCode == 8)
{
if(KeyEvent.getKeyText(keyCode).length == 1)
{
//Only one character is pressed.
}
}
This solution worked for everything except the delete key and numpad keys...
event.getKeyChar() != '\uFFFF'
Because any Java outputs that character for non-renderable keys, it works quite consistently.
To fix the delete key problem...
event.getKeyChar() != '\uFFFF' && event.getKeyCode() != KeyEvent.VK_DELETE
It will return true if the key is printable, and false if not.
I want to check whether there is something in my JButton. What would i insert into the equalsIgnoreCase() area?
if (jButton1.getText().equalsIgnoreCase("") &&
jButton2.getText().equalsIgnoreCase("") &&
jButton3.getText().equalsIgnoreCase("") &&
jButton4.getText().equalsIgnoreCase("") &&
jButton5.getText().equalsIgnoreCase("") &&
jButton6.getText().equalsIgnoreCase("") &&
jButton7.getText().equalsIgnoreCase("") &&
jButton8.getText().equalsIgnoreCase("") &&
jButton9.getText().equalsIgnoreCase(""))
To check that there is a text, you can do:
!jButton1.getText().isEmpty()
or, if you want to exclude a text that only contains spaces:
!jButton1.getText().trim().isEmpty()
You can use
jButton1.getText().isEmpty()
(use ! to negate if you want to know it's not empty...)
or you can check the length of the value
jButton1.getText().length > 0
you can use
bool somethingIn = !jButton1.getText().isEmpty();
Nothing, just what you did. But you should put exclamation symbols before conditions to negate them:
if (!jButton1.getText().equals("") &&
!jButton2.getText().equals("") &&
!jButton3.getText().equals("") &&
!jButton4.getText().equals("") &&
!jButton5.getText().equals("") &&
!jButton6.getText().equals("") &&
!jButton7.getText().equals("") &&
!jButton8.getText().equals("") &&
!jButton9.getText().equals(""))
In this case body of if will be executed if all your JButtons have some text. And ignoreCase is not necessary. Emptiness has no lower or upper case ))
Does anyone knows how to detect printable characters in java?
After a while ( trial/error ) I get to this method:
public boolean isPrintableChar( char c ) {
Character.UnicodeBlock block = Character.UnicodeBlock.of( c );
return (!Character.isISOControl(c)) &&
c != KeyEvent.CHAR_UNDEFINED &&
block != null &&
block != Character.UnicodeBlock.SPECIALS;
}
I'm getting the input via KeyListener and come Ctr-'key' printed an square. With this function seems fairly enough.
Am I missing some char here?
It seems this was the "Font" independent way.
public boolean isPrintableChar( char c ) {
Character.UnicodeBlock block = Character.UnicodeBlock.of( c );
return (!Character.isISOControl(c)) &&
c != KeyEvent.CHAR_UNDEFINED &&
block != null &&
block != Character.UnicodeBlock.SPECIALS;
}
I'm not perfectly sure whether I understand your problem. But if you want detect if character can be drawn to Graphics object, and if not print some placeholder char you might find usefull:
Font.canDisplay(int)
It will check whether font can display specific codepoint (it is more that check whether font is displayable at all -- since there are chars that are displayable - like ą - but some fonts cant display them.