Change Label into Textbox/Button - java

I would like to create a dialog window that shows a user's profile (such as name, avatar, sex, etc.) in a visually pleasing way by using labels that contain the relevant information. At the same time, I would like to show an edit button that transforms the labels into textboxes or buttons allowing the name or avatar to be changed.
How can I realize this with SWT? What would be the best strategy to approach this?

You can use a Text and then set setEditable(boolean) to enable/disable editing of the text box from the SWT.Selection Listener of the Button:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
// Create a read-only text field
final Text text = new Text(shell, SWT.BORDER);
text.setEditable(false);
text.setText("Some text here");
Button button = new Button(shell, SWT.PUSH);
button.setText("Change");
button.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event event)
{
text.setEditable(!text.getEditable());
}
});
shell.open();
shell.pack();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Alternatively, you can use a StackLayout and have two versions of the same UI (or just parts of it) and switch between them when the button is pressed.

Related

How do I right align the check box in my Java SWT application

How do I right align the check box in my Java SWT application ? Here is my code snippet:
Button checkFullECR = new Button(scrolledForm.getBody(), SWT.CHECK);
checkFullECR.setAlignment(SWT.RIGHT);
checkFullECR.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
checkFullECR.setText("I need the complete ECR/ECN Process:");
It looks like the following:
[ ] I need the complete ECR/ECN Process:
I want it to look like:
I need the complete ECR/ECN Process: [ ]
You can't do this using just a Button control, you will need to use a Label for the text and a checkbox Button with no text arranged in two columns.
Something like:
Composite body = new Composite(parent, SWT.NONE);
body.setLayout(new GridLayout(2, false));
Label label = new Label(body, SWT.LEFT);
label.setText("I need the complete ECR/ECN Process:");
Button checkFullECR = new Button(body, SWT.CHECK);
... more label / button pairs ....
For once I kind of have to disagree with Greg.
You can use the setOrientation(int) method of the Button to force right-to-left layout:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.CHECK);
button.setText("TEST");
button = new Button(shell, SWT.CHECK);
button.setOrientation(SWT.RIGHT_TO_LEFT);
button.setText("TEST");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Looks like this:
(Tested on Linux Mint and Windows. Does not work on the Mac.)

SWT Browser Control Right click Popup menu Function called on Button click

I am implementing a SWT Browser in my project to display HTML
Pages. When user right clicks on this browser it shows a popup menu with functionalities like "Print", "Print View". Can this be done with separate buttons, if I place them on a Toolbar?
Another functionality of the browser control is using "Ctrl+F", which bring a find dialogue. Can this dialogue be called using a button?
please help me?
You can achieve some of those goals by executing JavaScript when the Button is pressed.
Here is an example for the print dialog:
public static void main(String[] args)
{
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
final Browser browser = new Browser(shell, SWT.NONE);
browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button button = new Button(shell, SWT.PUSH);
button.setText("Search");
button.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
browser.execute("window.print()");
}
});
shell.pack();
shell.setSize(600, 400);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Just search online how to achieve the other functionalities that you want to add by using javascript.

How can i expand a textfield when it's in focus and when it's out of focus it should collapse?

I want to use a text field which will expand when the user enters some data and when the user is done editing and move out of text field than it should collapse. After that when ever user focus on the text field it should expand like a tooltip. Any pointer regarding this will help me.
This code should give you an idea of how to do it:
public static void main(String[] args)
{
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
final Text text = new Text(shell, SWT.BORDER | SWT.MULTI);
text.setText("edasdasdas\n\nasdasda\n\nasdasd");
final GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
data.heightHint = 100;
text.setLayoutData(data);
text.addListener(SWT.FocusIn, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
data.heightHint = 100;
shell.layout(true);
}
});
text.addListener(SWT.FocusOut, new Listener()
{
#Override
public void handleEvent(Event arg0)
{
data.heightHint = 50;
shell.layout(true);
}
});
Button button = new Button(shell, SWT.PUSH);
button.setText("Button");
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
It basically changes the heightHint of the GridData when focus is lost/gained. Afterwards you need to re-layout the parent.
You will have to make some adjustments for the "focused height" and "unfocused height" values yourself.
Here are some screenshots:
With focus:
Without focus:
Just press the Button to lose focus.
Just add a FocusListener to your text field:
text.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {
text.setSize(); // put in the size you want
}
#Override
public void focusLost(FocusEvent e) {
text.setSize(); // put in the size you want
}
});
Note that for setSize to work properly, parent of text can't have a layout.

SWT Tray Item in gnome

I have an issue about the tray items display in Gnome Shell.
The item icon appears in the lower bar, and I'm fine with that, and just like the other ones, when moving the mouse over the icon, a text is displayed.
My problem is that this text cannot be changed: setting a text with .setText does not work, neither the class support any event, but Selected and MenuDetect, which detect the left and right click, respectively.
Has anybody experienced the same problem?
Thanks a lot in advance for your answers,
Gianluca
Not quite sure what problem you are facing, but this works for me:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Tray tray = display.getSystemTray();
TrayItem item;
if (tray != null) {
item = new TrayItem(tray, SWT.NONE);
item.setToolTipText("A");
item.setImage(display.getSystemImage(SWT.ICON_ERROR));
}
Button button = new Button(shell, SWT.PUSH);
button.setText("Change tray text");
button.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event arg0) {
if(tray != null)
{
TrayItem item = tray.getItem(0);
item.setToolTipText(item.getToolTipText() + "A");
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
Note that I don't use setText(), but rather setToolTipText().

Enabling focus listeners on the composite container

I have four composites aligned in order.
Each composite has a check box, Label, and 2 Buttons. Now these composites are aligned one after another.
I want to enable focus on these items, i.e. when I use tab to go from one composite to other, the current composite should look highlighted. Ideally I want it to behave like a list, when you choose an item then that gets highlighted. Is this possible?
I understand that composite acts as a container for others widgets, control. My requirement is that I have a list of 5 entries, and that each item in the list has a check box, label, and two Buttons. I would also want it to be focused on when they are selected.
Also please let me know alternative solutions for the same UI that I have described above.
To make tab go from composite to composite, set the tab list for each composite to be one control that you want to have focus after a tab. For example, the check box:
composite.setTabList(new Control[]{checkButton});
To make the highlight, your imagination is the limit. You can change background, add some border, you name it. You just have to update it whenever one of the controls from the composite get focus.
Here is a full example:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout(SWT.VERTICAL));
createElement(shell);
createElement(shell);
createElement(shell);
createElement(shell);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private static void createElement(final Composite parent) {
final Composite composite = new Composite(parent, SWT.BORDER);
composite.setLayout(new GridLayout(4, false));
final Button checkButton = new Button(composite, SWT.CHECK);
new Label(composite, SWT.NONE);
final Button button1 = new Button(composite, SWT.PUSH);
final Button button2 = new Button(composite, SWT.PUSH);
Listener listener = new Listener() {
#Override
public void handleEvent(Event event) {
for (Control control : parent.getChildren()) {
control.setBackground(null);
}
composite.setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_RED));
if (event.widget == button1 || event.widget == button2) {
checkButton.setFocus();
}
}
};
checkButton.addListener(SWT.FocusIn, listener);
button1.addListener(SWT.FocusIn, listener);
button2.addListener(SWT.FocusIn, listener);
composite.setTabList(new Control[]{checkButton});
}

Categories