Java Centring multiline JLabel Text properly - java

I need to centre two lines of text in a JLabel. My code is:
lblSolution.setHorizontalAlignment(JLabel.CENTER);
...
lblSolution.setText("<html>" + result.getNearest() + "<br>" + "<br>" + result.getSolutionString() + "</html>");
The problem is, it centres the longer of the two lines, and aligns the shorter at the same x position as the other.

This fixed it:
lblSolution.setText("<html><div style='text-align:center'>" + result.getNearest() + "<br>" + "<br>" + result.getSolutionString() + "</div></html>");

Related

How to get input data in textArea

I am a new programmer in javafx.
Using java FX in IDE I want to make a student information application.
I want to print the input data into the text area but it prints out in the console now.
(After clicking display button I want to show all the input data in textarea)
please help me out to solve this problem.
I am attaching my code below for your suggestion..
Main.java
Just place
textArea.setText("Name: " + t1.getText() +"\nAddress: " + t2.getText()
+"\nProvince: " + t3.getText() +"\nCity: " + t4.getText()
+"\nPostal Code: " + t5.getText() +"\nPhone Number: " + t6.getText()
+"\nEmail: " + t7.getText() + "\nCourses: "+comboBox.getItems()
+ "\nActivities: "+c1.getText() + "\n"+c2.getText());
into the action handler.
And remove the imports
import javax.swing.JTextArea;
import java.awt.*;
They are not JavaFX.
You are printing your result into the standard output stream (System.out). I assume you want to display what is printed by:
System.out.println("Name: " + t1.getText() +"\nAddress: " + t2.getText()
+"\nProvince: " + t3.getText() +"\nCity: " + t4.getText()
+"\nPostal Code: " + t5.getText() +"\nPhone Number: " + t6.getText()
+"\nEmail: " + t7.getText() + "\nCourses: "+comboBox.getItems()
+ "\nActivities: "+c1.getText() + "\n"+c2.getText());
Please visit the documentation of the TextArea class. Printing your result into the standard output stream has nothing to do with printing your result in your TextArea. Simply use a normal String that contains your result and set the text of your TextArea accordingly.

I can't set a string in a TextView in an Android app (using Java)

I'm building a simple calculator Android app in Java that will receive 2 numbers as inputs and when the user presses one of the 4 action buttons (+, -, *, /) the exercise and it's solution will appear in the bottom of the screen inside a TextView in this format:
{num1} {action} {num2} = {solution}
I tried to declare a string and form the exercise's string in it and in the end I used "setText" to change the TextView but instead of showing the full exercise when I run the app it shows something like "androidx.appcompat.widget.AppCom".
Here is an example for the string I form when the user clicks on the + button:
exerciseStr = etNum1.toString() + " + " + etNum2.toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));
Does anybody know what the issue may be?
You should call getText() befor calling toString():
exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));
Change it to like this.
exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));

GWT Java calculate days between two dates on the server side

I am using GWT java. I have a report on the client side that I want to export to csv. So I am trying to create the report on the server side to pass back to the client side as a csv file so the user can store the csv file in their selected destination.
The following code works on the client side; however, does not work on the server side (i.e., "Print 6." is displayed and "Print 7." is not displayed on the server side). There is no error message. The dates are "2016-11-09" and "2000-02-02".
System.out.println(todays_date);
System.out.println(pack.getDob());
System.out.println("Print 6.");
float diffDOB = (CalendarUtil.getDaysBetween(pack.getDob(), todays_date));
System.out.println("Print 7.");
I was talking about this with a fiend, who is not a programmer, and she said why not just send the MS Excel formula. Brilliant! So I have removed all the calclations and am now sending:
fileContent.append(pack.getSurname() + "," + pack.getFirstname() + "," + pack.getScout_no() + "," +
pack.getgroup() + "," + pack.getDob() + "," + '"' + "=ROUNDDOWN(((TODAY()-E"+row+")/365),1)" + '"' + "," +
pack.getstartDate() + "," + '"' + "=ROUNDDOWN(((TODAY()-G"+row+")/365),1)" + '"' + "," +
'"' + "=EDATE(E"+row+",216)" + '"' + "\n");
The only issue now is that spaces in the string fields appear as + in the output file.

JAVA GWT DialogBox how to add new line char

I am having an issue trying to add a new line to a DialogBox. I have tried the normal "\n" and also tried putting my lines inside of HTML style tags. Neither of these attempts worked. Help is appreciated.
DialogBox dialog = new DialogBox();
dialog.setText(" Meter ID:\t\t" + blink.getMeterId() + "\n X coordinate:\t\t" + blink.getXCoord() + "\n Y coordinate:\t\t" + blink
.getYCoord());
Issue resolved via using setHTML to set the contents of the DialogBox
dialog.setHTML("<p> Meter ID: " + blink.getMeterId() + "</p> <p> X coordinate:\t\t" + blink.getXCoord() + "</p> <p> Y coordinate:\t\t" + blink
.getYCoord() + "</p>");
note this is using <p></p> for new paragraph but <br> and other html will work also
just use another \ as an escape character in a string:
new line: \\n
DialogBox dialog = new DialogBox();
dialog.setText(" Meter ID:\\t\\t" + blink.getMeterId() + "\\n X coordinate:\\t\\t" + blink.getXCoord() + "\\n Y coordinate:\\t\\t" + blink
.getYCoord());

Set jTextArea to visible false by default

I am making an application that show the information I entered in a textfield, combo box, etc.. into a text area.
I want to hide the text area when I started up the application and when I press on a button I want to show with the desired information.
I've tried to place <nameOfTextArea>.setVisible(false); in the frame constructor, but it is still visible.
How can I start up the frame without seeing this text area?
Constructor frame:
public StudentInfoFrame() {
initComponents();
textAreaVoorOpslaanInfo.setVisible(false);
}
My button in the frame:
private void uitvoerButtonActionPerformed(java.awt.event.ActionEvent evt) {
try{
..... Variables here .....
textAreaVoorOpslaanInfo.setVisible(true);
textAreaVoorOpslaanInfo.append("Voornaam: \t\t" + voornaam + "\n"
+ "Achternaam: \t\t" + achternaam + "\n"
+ "E-mail adres: \t\t" + email + "\n"
+ "Geboortedatum: \t" + geboortedatum + "\n"
+ "Lengte: \t\t" + lengte + "m\n"
+ "Gewicht: \t\t" + gewicht + "kg\n"
+ "Geslacht: \t\t" + geslacht + "\n"
+ "Vooropleiding(en): \t" + vooropleiding + "\n"
+ "Uitwonend: \t\t" + uitwonend);
} catch (Exception e){
System.out.println(e);
}
}
My question is solved. The jTextArea was invisible, but the jScrollPane was not.
Made the jScrollPane invisible in the constructor and it worked as intended.
Thanks for the help from the people above this post.
Call JComponent#revalidate() on parent component after changing element's visibility.
Call JFrame#setVisible(true) in the end after adding all the components.
Try this one:
textAreaVoorOpslaanInfo.hide();
or this:
textAreaVoorOpslaanInfo.show();

Categories

Resources