How can I align different elements in two composites? - layout

I try to make this widget :
For that I create one composite for the group (with 1 column) and one composite with 2 columns (for the label and text) for id and password
And then, I create one composite for the 3 fields (check box, label and Text) with 3 columns
But, the last line is not align with the id and password because I would like to align vertically the "Retry attemps" with the Text fields :
My code is:
Composite sqlComposite = new Composite(parent, SWT.NONE);
sqlComposite.setLayout(new GridLayout(1, false));
sqlComposite .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
Group sqlGoup = new Group(sqlComposite, SWT.NONE);
group.setLayout(new GridLayout(1, false));
group.setText("sql connection");
Composite sql2Composite = new Composite(sqlGoup, SWT.NONE);
sql2Composite.setLayout(new GridLayout(2, false));
sql2Composite .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
Label label = new Label(sql2Composite, SWT.NONE);
label.setText("Id");
Text textBoxID = new Text(sql2Composite, SWT.BORDER);
textBoxID.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Label label = new Label(sql2Composite, SWT.NONE);
label.setText("Password");
Text textBoxPass = new Text(sql2Composite, SWT.BORDER);
textBoxPass.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Composite sqlButtonsComposite = new Composite(sqlGoup, SWT.NONE);
sqlButtonsComposite.setLayout(new GridLayout(3, false));
sqlButtonsComposite.setLayoutData(new GridData(SWT.NONE, SWT.NONE, false, false));
_encryptCheckButton = new Button(sqlButtonsComposite, SWT.CHECK);
_encryptCheckButton.setText("Encrypt");
_encryptCheckButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, false));
Label labelSpinner = new Label(sqlButtonsComposite , SWT.NONE);
labelSpinner.setText("RetryAttempts");
_retryAttemptsSpinner = new Spinner(sqlButtonsComposite, SWT.BORDER);
_retryAttemptsSpinner.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, true, false));
_retryAttemptsSpinner.setMinimum(MIN_DELTA_SPINNER);
_retryAttemptsSpinner.setMaximum(MAX_DELTA_SPINNER);
_retryAttemptsSpinner.setSelection(DEFAULT_SPINNER_NUMBER);
_retryAttemptsSpinner.setIncrement(INCREMENT_SPINNER_NUMBER);
So my question is : how can I align the id, password and the checkbox named encrypt?
Thanks

You can't get alignment using two Composites easily. So use one Composite with 3 columns and make the text controls span two columns:
Composite sql2Composite = new Composite(sqlGoup, SWT.NONE);
// 3 columns
sql2Composite.setLayout(new GridLayout(3, false));
sql2Composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
Label label = new Label(sql2Composite, SWT.NONE);
label.setText("Id");
Text textBoxID = new Text(sql2Composite, SWT.BORDER);
// Span 2 columns
textBoxID.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
label = new Label(sql2Composite, SWT.NONE);
label.setText("Password");
Text textBoxPass = new Text(sql2Composite, SWT.BORDER);
// Span 2 columns
textBoxPass.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
// No second composite
_encryptCheckButton = new Button(sql2Composite, SWT.CHECK);
_encryptCheckButton.setText("Encrypt");
// Don't grab extra space
_encryptCheckButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
Label labelSpinner = new Label(sql2Composite , SWT.NONE);
labelSpinner.setText("RetryAttempts");
_retryAttemptsSpinner = new Spinner(sql2Composite, SWT.BORDER);
// Don't grab extra space
_retryAttemptsSpinner.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
I have added comments above where things need to be changed

Related

Control image placement on JTextPane

I am able to add ImageIcons to a JTextPane, but when I add them they show up in the center of the JTextPane. I can't find a way to control where they are placed on the JTextPane. Can someone please help me with this?
This method is making the JTextPane:
private void loadTextPanel(JPanel contentPane) {
chatLogPanel = new JPanel();
chatLogPanel.setLayout(null);
EmptyBorder eb = new EmptyBorder(new Insets(10, 10, 10, 10));
DefaultStyledDocument document = new DefaultStyledDocument();
chatLog = new JTextPane(document);
chatLog.setEditorKit(new WrapEditorKit());
chatLog.setBorder(eb);
chatLog.setMargin(new Insets(5, 5, 5, 5));
chatLogScrollPane = new JScrollPane(chatLog);
addComponent(chatLogPanel, chatLogScrollPane, 0, 0, 500, 240);
addComponent(contentPane, chatLogPanel, 0, 40, 500, 240);
}
This is the code I'm using to add a string to the Panel:
private static void appendToChatLog(JTextPane tp, String msg, Color c) {
chatLog.setEditable(true);
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
aset = sc.addAttribute(aset, StyleConstants.Alignment, Integer.valueOf(3));
int len = tp.getDocument().getLength();
tp.setCaretPosition(len);
tp.setCharacterAttributes(aset, false);
tp.replaceSelection(msg);
chatLog.setEditable(false);
}
And this is what I'm currently using to add the image to the JTextPane:
BufferedImage image = generateBufferedImage(message.getImage());
Icon icon = new ImageIcon(image);
StyleContext context = new StyleContext();
StyledDocument document = (StyledDocument) chatLog.getDocument();
Style labelStyle = context.getStyle(StyleContext.DEFAULT_STYLE);
JLabel label = new JLabel(icon);
StyleConstants.setComponent(labelStyle, label);
try {
document.insertString(document.getLength(), "Ignored", labelStyle);
} catch (BadLocationException badLocationException) {
badLocationException.printStackTrace();
}
To insert a component to a JTextPane, and display it like a character, use the insertComponent method.
To insert an Icon instead, use the insertIcon method.
Quite intuitive isn't it ;)

JavaFX: setting background color for Text controls

I'm using a TextFlow and some Text items to show a styled text, but i cant find a way to set a simple background color for the Text items.
I can set the fill color and font but it does not have a java method or css property that sets its background color.
Based on this solution, this is a quick implementation of a method to provide background coloring for all the Text nodes within a FlowPane, using CSS and the ability to set a series of paint values separated by commas (as much as Text items) and insets for each one of them:
private FlowPane flow;
private Scene scene;
#Override
public void start(Stage primaryStage) {
Text text0 = new Text("These are several ");
Text text1 = new Text("Text Nodes ");
Text text2 = new Text("wrapped in ");
Text text3 = new Text("a FlowPane");
text0.setFill(Color.WHEAT);
text0.setFont(new Font("Times New Roman", 20));
text1.setFill(Color.WHITE);
text1.setFont(new Font("Verdana", 32));
text2.setFill(Color.WHITESMOKE);
text2.setFont(new Font("Arial", 24));
text3.setFill(Color.WHITESMOKE);
text3.setFont(new Font("Arial", 18));
flow = new FlowPane(text0, text1, text2, text3);
scene = new Scene(flow, 300, 200);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
setBackgroundColors();
flow.needsLayoutProperty().addListener((obs,d,d1)->setBackgroundColors());
}
private void setBackgroundColors(){
final Bounds out = flow.getBoundsInLocal();
final StringBuilder sbColors = new StringBuilder();
final StringBuilder sbInsets = new StringBuilder();
AtomicInteger cont = new AtomicInteger();
flow.getChildrenUnmodifiable().forEach(n->{
sbColors.append("hsb(")
.append((((double)cont.get())/((double)flow.getChildren().size()))*360d)
.append(", 60%, 90%)");
Bounds b = ((Text)n).getBoundsInParent();
sbInsets.append(b.getMinY()).append(" ");
sbInsets.append(Math.min(scene.getWidth(),out.getMaxX())-b.getMaxX()).append(" ");
sbInsets.append(Math.min(scene.getHeight(),out.getMaxY())-b.getMaxY()).append(" ");
sbInsets.append(b.getMinX());
if(cont.getAndIncrement()<flow.getChildren().size()-1){
sbColors.append(", ");
sbInsets.append(", ");
}
});
flow.setStyle("-fx-background-color: "+sbColors.toString()+"; -fx-background-insets: "+sbInsets.toString()+";");
}
This will lead to this:
and after resizing the scene:
EDIT
Based on the OP request of using a TextFlow layout instead of a FlowPane, since Text nodes can be spanned over several lines within a TextFlow, the given solution will no longer be valid, as the bounding box of each text node will overlap others.
As a workaround, we can split the Text nodes in single word Text nodes, while keeping the same background color for those in the same original phrase.
I won't go into the splitting logic, but I will add a list of indices, where each index maps the text node with its index of background color.
private FlowPane flow;
private Scene scene;
private final List<Integer> indices=Arrays.asList(0,0,0,1,1,2,2,3,3);
#Override
public void start(Stage primaryStage) {
List<Text> text0 = Arrays.asList(new Text("These "), new Text("are "), new Text("several "));
List<Text> text1 = Arrays.asList(new Text("Text "), new Text("Nodes "));
List<Text> text2 = Arrays.asList(new Text("wrapped "), new Text("in "));
List<Text> text3 = Arrays.asList(new Text("a "), new Text("FlowPane"));
text0.forEach(t->t.setFill(Color.WHEAT));
text0.forEach(t->t.setFont(new Font("Times New Roman", 20)));
text1.forEach(t->t.setFill(Color.WHITE));
text1.forEach(t->t.setFont(new Font("Verdana", 32)));
text2.forEach(t->t.setFill(Color.WHITESMOKE));
text2.forEach(t->t.setFont(new Font("Arial", 24)));
text3.forEach(t->t.setFill(Color.WHITESMOKE));
text3.forEach(t->t.setFont(new Font("Arial", 18)));
flow = new FlowPane();
flow.getChildren().addAll(text0);
flow.getChildren().addAll(text1);
flow.getChildren().addAll(text2);
flow.getChildren().addAll(text3);
scene = new Scene(flow, 300, 200);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
setBackgroundColors();
flow.needsLayoutProperty().addListener((obs,d,d1)->setBackgroundColors());
}
private void setBackgroundColors(){
final Bounds out = flow.getBoundsInLocal();
final StringBuilder sbColors = new StringBuilder();
final StringBuilder sbInsets = new StringBuilder();
AtomicInteger cont = new AtomicInteger();
flow.getChildrenUnmodifiable().forEach(n->{
sbColors.append("hsb(")
.append((double)indices.get(cont.get())/(double)(indices.get(flow.getChildren().size()-1)+1)*360d)
.append(", 60%, 90%)");
Bounds b = ((Text)n).getBoundsInParent();
sbInsets.append(b.getMinY()).append(" ");
sbInsets.append(Math.min(scene.getWidth(),out.getMaxX())-b.getMaxX()-1).append(" ");
sbInsets.append(Math.min(scene.getHeight(),out.getMaxY())-b.getMaxY()).append(" ");
sbInsets.append(b.getMinX());
if(cont.getAndIncrement()<flow.getChildren().size()-1){
sbColors.append(", ");
sbInsets.append(", ");
}
});
flow.setStyle("-fx-background-color: "+sbColors.toString()+"; -fx-background-insets: "+sbInsets.toString()+";");
}
This FlowPane now behaves as a TextFlow:
There is no background for Text objects. You'd either have to group it with a shape (rectangle, ellipse, etc) and set the color of that shape, or you could put the objects inside a StackPane and set the background color of the StackPane.

Programmatically display a message next to text field in JavaFX

I have a TextField in my JavaFX application. I want to programmatically display a message on the right side of the text (like the validation message). I thought of using Popup and setting the Label with message in that Popup. But I'm not sure how I can position this to the right side of the text field. Below is the sample code for this. Can you please help me with this?
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX Welcome");
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Text scenetitle = new Text("Welcome");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
grid.add(scenetitle, 0, 0, 2, 1);
Label userName = new Label("User Name:");
grid.add(userName, 0, 1);
TextField userTextField = new TextField();
grid.add(userTextField, 1, 1);
Label pw = new Label("Password:");
grid.add(pw, 0, 2);
PasswordField pwBox = new PasswordField();
grid.add(pwBox, 1, 2);
Label label=new Label();
label.setText("This is an error message");
final Text actiontarget = new Text();
grid.add(actiontarget, 1, 6);
final Popup popup = new Popup();
popup.getContent().add(label);
//Want to display this popup to the right of the userTextField.
Scene scene = new Scene(grid, 300, 275);
primaryStage.setScene(scene);
primaryStage.show();
}
After showing the stage, do
Bounds userTextFieldBounds = userTextField.getBoundsInLocal();
Point2D popupLocation = userTextField.localToScreen(userTextFieldBounds.getMaxX(), userTextFieldBounds.getMinY());
popup.show(userTextField, popupLocation.getX(), popupLocation.getY());
The localToScreen(...) method was introduced in Java 8; if you are in an earlier version you will need
Bounds userTextFieldBounds = userTextField.getBoundsInLocal();
Point2D popupLocation = userTextField.localToScene(userTextFieldBounds.getMaxX(), userTextFieldBounds.getMinY());
popup.show(userTextField,
popupLocation.getX()+scene.getX()+primaryStage.getX(),
popupLocation.getY()+scene.getY()+primaryStage.getY());

UI alignments in LWUIT?

I'm using LWUIT to create J2ME application. I used GridLayout to add the controls but they are not aligned.
Code:
flowLayout = new FlowLayout();
roundsPage.setLayout(flowLayout);
try {
image = image.createImage("/11.png"); // All image 40 x 40
starImage = Image.createImage("/j1.png");
smileImage = Image.createImage("/j2.png");
imgClock = Image.createImage("/clock.jpg");
imageTeam = Image.createImage("/name.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dateLabel=new Label("14-02-2012, Round1 ");
dateLabel.getStyle().setBgColor(0x8b8989);
dateLabel.getStyle().setFgColor(0xffffff);
roundsPage.addComponent(dateLabel);
//int screenWidth = Display.getInstance().getDisplayWidth();
Container tabContainer1 = new Container(new BoxLayout(BoxLayout.Y_AXIS));
tabContainer1.addComponent(new Button("Click"));
tabContainer1.addComponent(new Button("Click"));
tabContainer1.addComponent(new Button("Click"));
tabContainer1.addComponent(new Button("Click"));
Container tabContainer2 = new Container(new BoxLayout(BoxLayout.X_AXIS));
team1=new Label(starImage);
team1.setText("Villarreal");
team1.setTextPosition(Component.BOTTOM);
//team1.setWidth(10);
//team1.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN ,Font.SIZE_SMALL));
tabContainer2.addComponent(team1);
tabContainer2.setWidth(100);
clockLabel=new Label(imgClock);
clockLabel.setText("18:00");
clockLabel.setTextPosition(Component.RIGHT);
tabContainer2.addComponent(clockLabel);
teamLabel=new Label(imageTeam);
teamLabel.setText("El Madrigal");
teamLabel.setTextPosition(Component.RIGHT);
tabContainer2.addComponent(teamLabel);
team2=new Label(smileImage);
team2.setText("Betis");
team2.setTextPosition(Component.BOTTOM);
team2.setFocusable(true);
tabContainer2.addComponent(team2);
Container tabContainer3 = new Container(new BoxLayout(BoxLayout.Y_AXIS));
tabContainer3.addComponent(new Button("Click"));
tabContainer3.addComponent(new Button("Click"));
tabContainer3.addComponent(new Button("Click"));
tabContainer3.addComponent(new Button("Click"));
TabbedPane tabbbed = new TabbedPane();
tabbbed.addTab(null, image, tabContainer1 );
tabbbed.addTab(null, image, tabContainer2);
tabbbed.addTab(null, image, tabContainer3);
roundsPage.addComponent(tabbbed);
Thanks in advance.
Don't use like this "14-02-2012, Round1 ". Get the width of the display and set the preferredW on dateLabel. For getting screen width use this,
int width = Display.getInstance().getDisplayWidth();.
Also Don't use TabbedPane. Use Tabs. Add the button in to addTab and set the width of the button. Because each tab having same width and height when you use TabbedPane. Thats an issue.

Monotouch.Dialog : Section font

It it possible to access the font and color properties of the section header and footer or do I need to subclass Section? I changed my UI to all black and everything looks great except my section headers and footers.
Reflection API:
class Login
{
public string Version = "1.2.3";
[Section ("Enter your credentials", "Email and password are required")]
[Entry ("Enter your email address")]
public string email;
[Caption ("Password"), Password ("Enter your password")]
public string password;
[OnTap ("Login")]
[Alignment (UITextAlignment.Center)]
public string Logon;
}
Element API:
return new RootElement ("Login") {
new Section() {
new StringElement ("Version", "1.2.3")
},
new Section ("Enter your credentials", "Email and password are required") {
new EntryElement("Email", "Enter your email address", "azcoov"),
new EntryElement("Password", "Enter your password", "password", true),
new StringElement("Logon", Login)
}
}
Section headers and footers can either be specified as strings or UIViews, there is sadly, nothing in between.
If you want to have custom headers/views, you would need to create a UILabel and use that in your constructor to the Section type (only available for the Elements API).
Something like:
var header = new UILabel (new RectangleF (0, 0, 320, 48)){
Font = UIFont.BoldSystemFontOfSize (22),
BackgroundColor = UIColor.Red
}
new Section(header, footer) {
...
}
Using Miguel's answer I was able to get what I needed:
RootElement CreateRoot (String lat, String lng)
{
var header = new UILabel (new RectangleF (0, 0, 320, 48)) {
Font = UIFont.BoldSystemFontOfSize (22),
BackgroundColor = UIColor.Black,
TextColor = UIColor.White,
Text = "This is my header"
};
var footer = new UILabel (new RectangleF (0, 0, 320, 48)) {
Font = UIFont.BoldSystemFontOfSize (22),
BackgroundColor = UIColor.Black,
TextColor = UIColor.White,
Text = "This is my footer"
};
var rootElement = new RootElement ("Location Example"){
new Section (header, footer){
new StyledStringElement("Latitude", lat),
new StyledStringElement ("Longitude", lng)
},
new Section() {
new StringElement ("Get location", GetLocation),
new StringElement ("Clear location", ClearLocation),
new StringElement ("Show on Map", ShowOnMap)
}
};
return rootElement;
}

Resources