Let's say I made a swt and a button triggers this line of code:
Label Charname = new Label(shell, SWT.NONE);
Charname.setBounds(250,10+a,500,40);
Charname.setText("Hello");
a=a+40;
I press the button twice, so it makes 2 labels, like so:
hello
hello
If I wanted to .getText the FIRST label, how would I do so? I know these labels are both the same but this is just a example, in what I'm working in these labels are different.
You just need to remember the labels you have created so you can access them again. One way would be to save them in a list in your class.
public class MyClass {
List<Label> labels = new ArrayList<>();
.... other code
Label charname = new Label(shell, SWT.NONE);
...
// Save in the list
labels.add(charname);
.....
// Access old label
int index = ... index of label required
Label oldLabel = labels.get(index);
}
Related
I have an ArrayList of buttons, and I'm trying to set all the buttons the same size of the last button.
This is my code:
ArrayList <Button> buttons = new ArrayList<Button>();
Composite numbersComposite = new Composite(composite, SWT.NONE);
numbersComposite.setLayout(new GridLayout(5, true));
for (int i=0; i<=49; i++) {
Button b = new Button(numbersComposite, SWT.TOGGLE);
b.setText(""+i);
buttons.add(b);
}
for (Button b : buttons) {
b.setSize(buttons.get(buttons.size()-1).getSize());
}
Something is wrong because not all the buttons have the same size. Is there any problem with setSize method on Buttons with TOGGLE style?
Edit
I see that buttons.get(buttons.size()-1).getSize() is giving a Point with 0,0 value. Why?
Edit 2
I tried with this code but nothing happens! not all of them have the same size. Why is this?
Point point = new Point(20, 20);
for (Button b : buttons) {
b.setSize(point);
}
You are mixing layouts with absolute positioning, which doesn't work.
setSize will only work when you are not using layouts.
If you are using layouts (which is usually the best choice) then you should set the appropriate layout data to the components you want to display.
Since you are using GridLayout for the parent Composite, its children should set GridData as layout data; for example this GridData will make the buttons use all the available space in the parent Composite:
for (int i = 0; i <= 49; i++) {
Button b = new Button(numbersComposite, SWT.TOGGLE);
// set the layout data
GridData buttonLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
b.setLayoutData(buttonLayoutData);
b.setText("" + i);
buttons.add(b);
}
Look to the constructors of GridData to see which other options you have.
Look here to know more about layouts in SWT: http://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html
I am new to Vaadin.
I want to create a page where on the left side there are three options. Depending on the option selection combo boxes will appear on the right side which is specific to the option on the left. I want the combobox with label.
Name: "Combo box menu"
So far I can click the option on the left side and that displays the combo box on the right side but could not display the labels with the combo box. Label is a component and so is the combo box. When I add both to the panel. Expecting both label and combo box to showup but the last one (Combo Box) ignores the first one (Label). I am not sure why is that. I would really appreciate if someone could help me with that.
Thanks
Here is my code:
HorizontalLayout hLayout;
Panel hpanel;
HorizontalSplitPanel hsplit;
VerticalSplitPanel vsplit;
tree.setImmediate(true);
tree.addItem("OP1");
tree.addItem("OP2");
tree.addItem("OP3");
hsplit.setFirstComponent(tree);
tree.addListener(new Component.Listener() {
public void componentEvent(Component.Event event) {
Object o = tree.getValue();
System.out.println("selected = " + o);
showWindowtab(o);
}
});
hpanel.setContent(hsplit);
hLayout.addComponent(hpanel);
hLayout.setSpacing(true);
final VerticalLayout main = new VerticalLayout();
main.setMargin(true);
setContent(main);
main.addComponent(hLayout);
private void showWindowtab(Object itemClicked) {
Label label = new Label("Here is example of Combo Box");
hsplit.setSecondComponent(label);
String document[] = { "X", "Y", "Z" };
ComboBox cb = new ComboBox();
cb.setInputPrompt("Select values");
cb.setInvalidAllowed(false);
cb.setNullSelectionAllowed(false);
for (int i = 0; i < document.length; i++) {
cb.addItem(document[i]);
}
cb.setImmediate(true);
setFocusedComponent(cb);
hsplit.setSecondComponent(cb);
}
You call setSecondComponent() on both label and cb, so the second may override the first.
Maybe what you wanted was setfirstComponent(label)?
Another option is to call addComponent(label) on the component where you want to see your label.
The following is some code which makes multiple fields (items). I know that the parameter after new JTextField is the content of the text box, and I understand how item2, 3, and passwordField work, but do not understand item1. In the line of code:
item1 = new JTextField(10);
What does the (10) mean? I would expect there to be a default number inside that text box, but that is not the case. Any help would be appreciated.
public eventhandling(){
super("The title");
setLayout(new FlowLayout());
item1 = new JTextField(10);
add(item1); //Adds item to window
item2 = new JTextField ("Enter text here"); //Making a text box that has the words "enter text here" in it
add(item2);
item3 = new JTextField ("uneditable", 20);
item3.setEditable(false); //This text field cannot be changed now
add(item3);
passwordField = new JPasswordField ("mypass"); //Setting the password field with a default password : "mypass"
add(passwordField);
This is what i found from the Oracle Site
JTextField(int columns):
Constructs a new empty TextField with the specified number of columns.
First you need to understand that there are different ways to initialize a component. Then you also need to notice that there is a difference between "10" and 10. The first is a string, the second is an integer. That being said, if you want the number 10 to show in the JTextField box then you need to pass "10" in the constructor. If you pass 10 that is telling the constructor to set the 10 columns in the JTextField, that is, it restricts the number of characters that can be entered to 10. Check the API ...
http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html
I have a JFrame extended class that implements a multi-tab chat. Every tab is a chat with someone or with a group of people. What I have implemented works fine, except when I assign a ToolTipText to the label of a tab. In this case I can't click anymore (and select) the tab that has a ToolTipText assigned. The others work fine.
Graphical example:
As you can see the tabs are properly being added, and the first two tabs ("Gruppo prova" and "Gruppo test") have a ToolTipText, the other two don't. I can switch between the last two, but I can't do the same with the first two. I thought that the icon next to the label could be a problem, but I removed it and still doesn't work. However I can still click all the 'X' (close) buttons (working properly).
This is a piece of the code I used to add a tab:
// Some stuff...
JChat chat = new JChat(gui.chatClient, email, name, group);
jTabbedPane.add(email, chat); // I instantiated this before
int index = jTabbedPane.indexOfTab(email);
JPanel pnlTab = new JPanel(new GridBagLayout());
pnlTab.setOpaque(false);
// Core function
JLabel lblTitle;
if (group == 1) {
// If it's a group and not a single chat I assign a name, an icon and a ToolTipText to the tab
lblTitle = new JLabel(name, icon, JLabel.LEFT);
lblTitle.setToolTipText(membersList.toString());
} else {
// otherwise I only assign a name to the tab
lblTitle = new JLabel(name);
}
jTabbedPane.setTabComponentAt(index, pnlTab);
// This applies the 'X' (close) button next to the tab name
CloseButton btnClose = new CloseButton(this, jTabbedPane, tabs, email);
lblTitle.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
pnlTab.add(lblTitle);
pnlTab.add(btnClose);
Is this a Swing bug or am I doing something wrong?
you can use :
void setToolTipTextAt(int, String) to set tool-tip text to specific tab.
void setIconAt(int index, Icon icon) to set the icon to specific tab.
No need to use JLabel for setting tool-tip text or icon.
The above solution, however doesn't however answer your question:
except when I assign a ToolTipText to the label of a tab. In this
case I can't click anymore (and select) the tab that has a ToolTipText
assigned
The only reason i am suspecting:
JLabel doesn't register to any mouse listener by default. When no mouse listener is set to JLabel any mouse clicked event will go through to the UI objects underneath: in this case the JTabbedPane. But when we are setting tool-tip text using setToolTipText(text), the ToolTipManger adds a mouse listener to this JLabel, which will continue to consume the mouse click event.
Check the following code snippets demonstrating the issue and providing a work around setSelectedIndex function:
JLabel label = new JLabel("a Label");
System.out.println(label.getMouseListeners().length); // length is printed as 0
label.setToolTipText("Danger: setting tool tip will consume mouse event");
System.out.println(label.getMouseListeners().length); // length is printed as 1
jTabbedPane1.setTabComponentAt(0, label);
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
int index = jTabbedPane1.indexOfTabComponent((Component)e.getSource());
jTabbedPane1.setSelectedIndex(index);
}
});
I have grid layout witch some fields added like that:
private Component userDetailsTab(final User user) {
final GridLayout details = new GridLayout(2, 1);
details.setMargin(true);
details.setSpacing(true);
details.addComponent(createDetailLabel(Messages.User_Name));
final Component username = createDetailValue(user.getName());
details.addComponent(username);
...
I have also Layout click listener which replace labels on text field, it looks like that:
final TextField tf = new TextField();
details.addListener(new LayoutClickListener() {
private static final long serialVersionUID = -7374243623325736476L;
#Override
public void layoutClick(LayoutClickEvent event) {
Component com = event.getChildComponent();
if (event.getChildComponent() instanceof Label) {
Label label = (Label)event.getChildComponent();
details.replaceComponent(com, tf);
tf.setValue(label.getValue());
}
}
});
In future I want to enable click on label, edit it and write changes to database after clicking somewhere else (on different label for example).
Now when I click on 1st label and then on 2nd label, effect is: 1st has value of 2nd and 2nd is text field witch value of 2nd. Why it's going that way? What should i do to after clicking on 1st and then 2nd get 1st label witch value of 1st?
You don't need to swap between Labels and TextFields, you can just use a TextField and style it look like a Label when it's not focused.
When I tried to create click-to-edit labels, it created a ton of extra work for me. I'd discourage it (and do as Patton suggests in the comments).
However, if you're going to insist on trying to create in-place editing, you will want to do the following:
Create a new class that extends a layout (e.g. HorizontalLayout), which can swap out a label for a text field
use LayoutClickListener to removeComponent(myLabel) and addComponent(myTextField)
use BlurListener to swap back to the label
use ValueChangeListener on the text field to copy its value to the label
This is a still a bad idea because:
Users cannot see affordances as easily (they can't tell what's editable)
Users cannot use the keyboard to tab to the field they want to edit
It adds unncessary complexity (maintenance time, etc).
I would recommend, if you want in-place editing, just show the text field, and save the new value with the BlurListener.