How to stop animation in random button text, codenameone? - java

I want to stop any component text animation.
For Ex:
Setting text on selection as below
final Button statusButton = new Button("Select Status");
final CheckBox inProcessButton = new CheckBox("In-Progress");
inProcessButton.setUIID("filter_combo_label");
inProcessButton.setName(Constants.STATUS_ACTIVE_NONCOMPLETE);
radioButtonGroup.add(inProcessButton);
inProcessButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
String checkBoxVal = ComponentUtils.getCSMultiCheckValues(radioButtonGroup);
statusButton.setText(checkBoxVal);
statusButton.repaint();
}
});
See snapshot as below.
Before selection:
After selection label image animating.

Do statusButton.setTickerEnabled(false);. This will prevent tickering the button text.
You might also want to end long texts with 3 dots:
statusButton.setEndsWith3Points(true);

Related

Unable to drag and drop a text box in flowlayoutcontainer in gxt

I want to drag a text box from vertical layout container and drop it in a flow layout container. On drop, it should appear as a Rich Text Area. Please find the code below. I tried to debug the code and it is getting entered only inside dragging. Debugging is not coming inside droptarget. Can you please help me?
final TextButton textButton = new TextButton();
textButton.setText("Text Box");
DragSource source = new DragSource(textButton) {
#Override
protected void onDragStart(DndDragStartEvent event) {
super.onDragStart(event);
}
};
DropTarget dropTarget = new DropTarget(flowLayoutContainer);
dropTarget.setOperation(Operation.COPY);
dropTarget.addDropHandler(new DndDropHandler() {
#Override
public void onDrop(DndDropEvent event) {
final RichTextArea textBox1 = new RichTextArea();
flowLayoutContainer.add(textBox1);
}
});

Java GUI adding buttons with a for loop

Hi i am making a lotto gui where the user picks 4 numbers from a selection of 28. The way i am currently doing it is as follows
private void no1InputButtonActionPerformed(java.awt.event.ActionEvent evt) {
numberSelectionList.add("1");
}
private void no2InputButtonActionPerformed(java.awt.event.ActionEvent evt) {
chosenNumDisplayLabel.setText(chosenNumDisplayLabel.getText()+" 2");
}
private void no3InputButtonActionPerformed(java.awt.event.ActionEvent evt) {
chosenNumDisplayLabel.setText(chosenNumDisplayLabel.getText()+" 3");
}
etc up through the 28 numbers.
Is there a way to add the actions to each button through a for loop
as this seems more logical?
Also is there a way to add each number picked into an array?
Create a single Action that can be shared by all buttons. The Action will then simply get the text of the button and then do some processing.
Check out setText method with panel and button. This example will show you how to:
create a single ActionListener to be shared by each button
"append" the text to the text field instead of replacing the text
use Key Bindings so the user can also just type the number
On each button you can set an action command:
button.setActionCommand("1");
And you can get the value after that using your ActionEvent:
evt.getActionEvent();
More complete:
ActionListener listener = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand()+" clicked");
}
};
int howMuchYouWant = 32;
for(int i = 0; i<howMuchYouWant; i++)
{
JButton button = new JButton(""+(i+1));
button.setActionCommand(""+i);
button.addActionListener(listener);
//add to whatever gui you want here
}

Disabling Enabled Effect on JToggleButton

I've got an application that is using Swing for it's UI. I want a button that switch the type of communication that the app is using. I want to use a Toggle Button to identify the type of communication that's selected.
My problem is that I don't want the color of the button to change after it's been clicked. Currently the button looks like this...
Non-Selected
And then when clicked it looks like this...
Selected
The text changing is what I want, but I would prefer them to have the same color / style.
Here's my code for this...
JToggleButton tglbtnCommunicationType = new JToggleButton("AlwaysOn");
tglbtnCommunicationType.setFocusPainted(false);
tglbtnCommunicationType.addChangeListener(new ChangeListener( ) {
public void stateChanged(ChangeEvent tgl) {
System.out.println("ChangeEvent!");
if(tglbtnCommunicationType.isSelected()){
tglbtnCommunicationType.setText("REST");
tglbtnCommunicationType.setBackground(UIManager.getColor("Button.background"));
}
else
{
tglbtnCommunicationType.setText("AlwaysOn");
};
}
});
My thought is that setting the background when it is selected to the standard background color would fix that, but it doesn't look like it. Any ideas?
Thanks!
Answer:
I switched to a JButton instead, thanks for the help everyone!
JButton btnCommunicationType = new JButton("AlwaysOn");
btnCommunicationType.setFocusPainted(false);
btnCommunicationType.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btnCommunicationType.getText().equals("AlwaysOn"))
{
btnCommunicationType.setText("REST");
//TODO: Insert Code for Switching Communication to REST here
}
else if(btnCommunicationType.getText().equals("REST")){
btnCommunicationType.setText("AlwaysOn");
//TODO: Insert Code for Switching Communication to AlwaysOne here
}
}
});
btnCommunicationType.setBounds(275, 199, 97, 25);
thingWorxConnectionPanel.add(btnCommunicationType);
you can do it by using JButton only instead of JToggleButton ,
JButton showButton = new JButton("AlwaysOn");
showButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String currentText = showButton.getText();
if("AlwaysOn".equals(currentText)){
showButton.setText("REST");
}else{
showButton.setText("AlwaysOn");
}
}
});

How to move the text pointer from one field to another in java

Iam using javafx to implement a calculator ... my calculator's design should be from Hewlett-Packard from seventies .. I have two text fields , in each text field I insert a number ... When I put a number in first text field so should I press ENTER in my calculator to move the text pointer to next text field to put anther number in it ... The problem that I do not know how to move the text pointer to the next text field when I press ENTER ... Here is my code for button Enter :
TextField text1 = new TextField();
TextField text2 = new TextField();
Button Enter = new Button("ENTER");
Enter.setLayoutX(140);
Enter.setLayoutY(270);
Robot robot = new Robot();
Enter.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if (event.getSource() == Enter) {
robot.mouseMove(250,150); // (250,150) is the position of the second text field
}
}
});
The problem with this code that when I press ENTER then my mouse pointer moves to
(250,150) but not my text pointer ...
How to move the text pointer instead of mouse pointer?
thank you
EDIT :
TextField text1 = new TextField();
TextField text2 = new TextField();
TextField text3 = new TextField();
Button Enter = new Button("ENTER");
Enter.setLayoutX(140);
Enter.setLayoutY(270);
Enter.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if (event.getSource() == Enter) {
if (!text1.requestFocus() || text2.requestFocus()) {
text3.requestFocus();
}
}
}
});
You can just request focus on the next element.
enter.setOnAction( event -> {
if(text1.isFocused()) {
text2.requestFocus();
} else if(text2.isFocused()) {
text3.requestFocus();
}
});
You can do the same in Java 7 or prior versions using :
enter.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
text2.requestFocus();
}
});

How can I get a Button's text in JavaFX if the Button is being read as a Node? Looping through Group/VBox of Buttons. Returns it as Nodes

I'm learning JavaFX and this is just a small programming question.
I have 3 buttons in a VBox. And I want to apply the same 3 effects on all buttons after I put them in the Vbox. But when I use a for loop and getChildren() on the VBox, they are returned as 'Nodes'. I can't use the Button.getText() to find out the text of the button.
Is there a way I can getText of a Node? Or maybe convert the current Node to a Button and get the text that way?
VBox vbox = new VBox();
Button option1 = new Button("Single Player");
Button option2 = new Button("Network Player");
Button option3 = new Button("View Rules");
vbox.getChildren().add(option1);
vbox.getChildren().add(option2);
vbox.getChildren().add(option3);
for (final Node button : vbox.getChildren()) {
button.setOnMouseEntered(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
button.setEffect(addEffect(Color.web("#53CFA6"), .8, 10));
}
});
button.setOnMouseExited(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
button.setEffect(addEffect(Color.web("#FF6800"), .8, 10));
}
});
button.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
button.setEffect(addEffect(Color.web("#E62800"), .8, 10));
//Need to use button.getText()
//Button button; button.getText() works
}
});
}
there is two options:
1. Convert types. Easy, but not safe.
If you sure you wouldn't add other children to this VBox you can just convert Node to Button:
for (Node node : vbox.getChildren()) {
if (node instanceof Button) {
final Button button = (Button) node;
// all your logic
}
2. Use Factory pattern. Best suites, IMHO.
introduce method createButton which will setup button as you need:
private Button createButton(String name) {
final Button button = new Button(name);
button.setOnMouseEntered(...);
button.setOnMouseExited(...);
button.setOnMouseClicked(...);
return button;
}
and you code will look next way:
Button option1 = createButton("Single Player");
Button option2 = createButton("Network Player");
Button option3 = createButton("View Rules");
vbox.getChildren().addAll(option1, option2, option3);
3. Introduce your own Button class. Better if you plan to extend buttons logic.
public void FancyButton extends Button {
public FancyButton(String name) {
super(name);
//handlers logic here
}
}
You can get text from button and assign it to a string variable by this code :-
String Val = ((Button)event.getSource()).getText();

Categories

Resources