Get the original value from JFormattedTextField - java

I'm trying to get original values from MyTextField
try {
MaskFormatter mf1 = new MaskFormatter("##/##/##");
MyTextField.setFormatterFactory(new DefaultFormatterFactory(mf1));
}
//input 123456
//System.out.println(MyTextField.getValue());
//display 12/34/56
With MyTextField.getText() and MyTextField.getValue() , I always get "12/34/56".
Is there any way to get the original value (123456) from MyTextField ?

Remove the undesired characters with MyTextField.getValue().replace("/", "");

Related

To print the value using webdriver when one of the textbox value is changed . But the output is fetched only from the first textbox everytime

Actual Scenario: I am inserting some values in 2-3 textbox and click on Search button. Based on the value, the page refreshes and it takes 10-20 sec to fetch the value and it is displayed as ->total Log count: 25. I have to print this value but unable to do so.
driver.findElement(By.id("txtMessage")).sendKeys("Push Success");
driver.findElement(By.id("txtMachineName")).sendKeys("AC204");
driver.findElement(By.id("txtPortal")).sendKeys("92");
driver.findElement(By.id("btnSearch")).click();
// use it just before the sendkeys code like this
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[#id='dvCount']/span[2]")));
String text = driver.findElement(By.xpath("//*[#id='dvCount']/span[2]")).getText();
System.out.println(text);
The result is 41 which is fine. But now i want to change
driver.findElement(By.id("txtPortal")).sendKeys("91");
driver.findElement(By.id("btnSearch")).click();
The Result is still 41. How i could print the value for 91, 93 ,94 .please help
I would wait for the result to change and store it for each iteration:
// store the result
String result = "";
// iterate each value to type
for(String value : new String[]{"82", "73"}) {
driver.findElement(By.id("txtMachineName")).sendKeys("AC204");
driver.findElement(By.id("txtPortal")).sendKeys(value);
driver.findElement(By.id("btnSearch")).click();
// wait for the result to change
result = wait.until((WebDriver drv) -> {
String text = drv.findElement(By.xpath("id('dvCount')/span[2]")).getText();
return text != result ? text : null;
});
// print the result
System.out.println(result);
}

java apache poi (part 3)

cont. on java apache poi (part 2)
code
LinkedList list = new LinkedList();
list.add("1|Ali");
list.add("2|Abu");
list.add("3|Ahmad");
StringBuilder outputResult = new StringBuilder();
for(Object staffList: list){
outputResult.append(staffList.toString());
outputResult.append("\n");
}
From above code, I try the following:
First, I display the output: System.out.println(outputResult.toString());
Output: 1|Ali2|Abu3|Ahmad
Second, I want to put the above output into the label:
jLabel1.setText("<HTML>"+outputResult.toString()+"<br /></HTML>");
Output: 1|Ali 2|Abu 3|Ahmad
My expected output on the label:
1|Ali
2|Abu
3|Ahmad
My question is how to display the value into the label same as the expected output?
HTML doesn't support newline characters, that's the point, that's the way it's designed.
You would need to reformat the output using <br> instead...
StringBuilder outputResult = new StringBuilder();
for(Object staffList: list){
outputResult.append(staffList.toString());
outputResult.append("<br>");
}
You could create a helper method which took the line separator that you wanted to use and build the list the way you wanted to...
String newLines = buildOutput(list, "/n");
String htmlBR = buildOutput(list, "<br>");
Or you could even use an Unordered list (<ul>).
Or you could even use a HTML table

How to Insert null Values in JDate

hey guys i m suffering with a problem with NullPointerException i've a jTable wish it have values from those values one Value i put it an Empty value it's a Date this date Can Sometimes Empty means we don't have this Date and Sometimes it's full i tried to Edit this Table so when i click on the Table and click on Button Edit it's shows me a new Frame wish have a Fields to fill them , i want it to populate automaticly it's works very good exept with this empty value wish i've the problem :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
I tried this code
public void fillDateSortie(){
int row = AdminFW.TableStock.getSelectedRow();
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
String dateInString2 = AdminFW.TableStock.getModel().getValueAt(row, 9).toString();
try{
java.util.Date date2 = formatter2.parse(dateInString2);
DateSortieStock.setDate(date2);
}catch(Exception e ){
javax.swing.JOptionPane.showMessageDialog(null, e);
}
if(DateSortieStock.getDate()==null){
DateSortieStock.setDate(null);
}
}
Try with this .This code not will gives you a exception .
if( ((JTextField)DateSortieStock.getDateEditor().getUiComponent()).getText().equals("")){
DateSortieStock.setDate(null);
}
Hope it helps..

JLabel HTML New Line Issue

I'm trying to call a method from a class called Circle in my project which displays some basic information about the object in a JLabel. For some reason the text won't go to a new line even when I use HTML to try and format it:
#Override
public String toString(){
return "<html>Type: " + this.getClass().getSimpleName() + "<br>Radius: " + getRadius() + "<br>Area: " + df.format(getArea()) + "<br>Perimeter: </html>" + df.format(getPerimeter());
}
I'm trying to display the info with this code:
#Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==btnCalc && x==1){
//create object
double R = Double.parseDouble(Txt1.getText());
Circle circ = new Circle(R);
lblResult.setText(circ.toString());
}
When I run the program it just returns this:<html>Type: Circle<br>Radius: 4.0<br>Area: 50.27<br>Perimeter:</html> 25.13
edit: I tried just setting the text as an exception message instead of calling the method and it didn't work this way either
edit: Now this happens when I try to run the cylinder-sphere classes, but it doesn't do that when I don't have any html in the toString() method.
Turns out I was using a DecimalFormat in the last four classes which was what was giving me the exception. Once I got rid of that, the strings formatted nicely using a JTextPane instead of a JtextField.
From the image you pasted, it looks like it is a text INPUT control (under Show Info button), like JTextField and not a JLabel.
You can use HTML content with JLabel constructor as well as with its setText method too. It works fine.
JLabel lbl = new JLabel("<html>Type: Circle<br>Some info<br>More info</html>")
JLabel lbl2 = new JLabel();
lbl2.setText("<html>Type: Circle<br>Some info<br>More info</html>")
But if you want to have an INPUT control (as in your image), you can not use HTML with JTextField. You have to use JTextPane for this.
JTextPane txt = new JTextPane();
txt.setContentType("text/html");
txt.setText("<html>Type: Circle<br>Some info<br>More info</html>");

Set caret position on JScrollPane

I have a JEditoPane inside a JScrollPane. I have some text content that contain some pre-defined tokens. I'm storing the location of these tokens in the database.
When I set the text content into the JEditorPane, I embed the tokens with HTML. I also add HTML break lines to format the content.
Now problem comes when I want to scroll to one of the highlighted tokens. It seems that the start position of the tokens, which I stored in database, do not match when using the setCaretPosition(int). I know it's probably because my content in JEditorPane Document is mixed with HTML.
So is there a way to search for a String in the JEditorPane content, then somehow get the caret position where the string was found?
That's how you do it (ignore not using best practices ;) ) -
public static void main( String[] args ) {
final JEditorPane jEditorPane = new JEditorPane( "text/html", "<h1>This is some header</h1>After this text would be the CARRET<br>This is some more text<br>And more" );
final JScrollPane jScrollPane = new JScrollPane( jEditorPane );
final JFrame jframe = new JFrame( "HHHHHH" );
jframe.add( jScrollPane );
jframe.setSize( new Dimension( 200, 200 ) );
jframe.setVisible( true );
final String text = jEditorPane.getText();
final int index = text.indexOf( "T" );
jEditorPane.setCaretPosition( index + 1 );
while ( true ) {
try {
Thread.sleep( 1000000 );
} catch ( InterruptedException e ) {
e.printStackTrace();
}
}
}
And that's the result:
You should store the result of indexof in the DB.
Do the strings have any commonalities? If they do, you could try using a combination of the java.util.scanner or/and java.util.regex.Matcher. Make sure to get the right regex for what you need. Once you have found a string get the indexOf the first letter and set the caret position to it.
Java Scanner
Java Matcher

Categories

Resources