I am trying to set a SWT Button into a "pressed" state programmatically.
Is that possible somehow?
Update:
What I am trying to achieve - is render draw a Button in it's selected state onto an Image.
Image buttonimg_mouseover = new Image(getDisplay(), 100, 100);
Button button = new Button(parent.parent, SWT.PUSH);
button.setAlignment(SWT.CENTER);
button.setImage(arrowimg);
button.setSize(100, 100);
button.setSelection(true); // doesn't work
GC gcbutton = new GC(buttonimg_mouseover); //draw an image of the button
button.print(gcbutton);
You can do it with the following snippet
Button myButton = new Button(parent, SWT.TOGGLE);
myButton.setSelection(true);
However, this will only work with the types CHECK, RADIO or TOGGLE.
See Javadoc of Button#setSelection(boolean).
Related
I have a school project or something like that and I am trying to make a sign up panel for users. This panel opens when user clicks on sign up. It looks like this.
What I am trying to do is I want to disable that Create Button and It will be enabled only if there are 3 checks on the dialog.
I am using a GridPane on Dialog and I was thinking about returning those certain nodes (Checks which are ImageViews) at those cells and check whether the condition is true. However, I could not figure out how to return a node from GridPane. If you have any other approach for this problem it is fine too.
This is the code's relevant part.
public void SignUp(){
//Create the custom dialog.
Dialog signUpDialog = new Dialog();
//Dialog Title
signUpDialog.setTitle("Sign Up");
//Setting "OK" button type.
ButtonType buttonTypeCreate = new ButtonType("Create", ButtonBar.ButtonData.OK_DONE);
//Adding Button types.
signUpDialog.getDialogPane().getButtonTypes().addAll(buttonTypeCreate, ButtonType.CANCEL);
//Creating the GridPane.
GridPane gridPane = new GridPane();
gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.setPadding(new Insets(20, 150, 10, 10));
//Setting the Check Icon.
Image imageCheck = new Image("resources/check_icon.png");
//Setting 3 different ImageViews for Check Icon because can't add duplicates to GridPane.
ImageView imageViewCheck1 = new ImageView(imageCheck);
ImageView imageViewCheck2 = new ImageView(imageCheck);
ImageView imageViewCheck3 = new ImageView(imageCheck);
//Setting the X Icon.
Image imageX = new Image("resources/x_icon.png");
//Setting 3 different ImageViews for X Icon because can't add duplicates to GridPane.
ImageView imageViewX1 = new ImageView(imageX);
ImageView imageViewX2 = new ImageView(imageX);
ImageView imageViewX3 = new ImageView(imageX);
//TextField for User ID.
TextField textFieldDialogUserID = new TextField();
textFieldDialogUserID.setPromptText("User ID");
textFieldDialogUserID.setAlignment(Pos.CENTER_RIGHT);
//PasswordField for Password.
PasswordField passwordFieldDialogPassword = new PasswordField();
passwordFieldDialogPassword.setPromptText("Password");
passwordFieldDialogPassword.setAlignment(Pos.CENTER_RIGHT);
//PasswordField for Confirm Password.
PasswordField passwordFieldDialogConfirmPassword = new PasswordField();
passwordFieldDialogConfirmPassword.setPromptText("Confirm Password");
passwordFieldDialogConfirmPassword.setAlignment(Pos.CENTER_RIGHT);
gridPane.add(new Label("User ID"), 0, 0);
gridPane.add(textFieldDialogUserID, 1, 0);
gridPane.add(new Label("Password"), 0, 1);
gridPane.add(passwordFieldDialogPassword, 1, 1);
gridPane.add(new Label("Confirm Password"), 0, 2);
gridPane.add(passwordFieldDialogConfirmPassword, 1, 2);
gridPane.add(imageViewX1,2,0);
gridPane.add(imageViewX2,2,1);
gridPane.add(imageViewX3,2,2);
signUpDialog.getDialogPane().setContent(gridPane);
Stage signUpStage = (Stage) signUpDialog.getDialogPane().getScene().getWindow();
signUpStage.getIcons().add(new Image("resources/application_icon.png"));
Optional<Pair<String, String>> result = signUpDialog.showAndWait();
}
Create an appropriate BooleanBinding which expresses when the button should be disabled. You can use the Bindings utility class to create the expression, including comparison, ands and ors. To make the code more readable do a static import of the functions.
Get the create button from your panel and bind the boolean expression to the disable property of your button.
If any of the values change the JavaFX framework will automatically reevaluate the bindings and update the button's state accordingly.
import static javafx.beans.binding.Bindings.*;
BooleanBinding notComplete = or(
equal(textFieldDialogUserID.textProperty(), null),
equal(passwordFieldDialogPassword.textProperty(), null));
Node createButton = signUpDialog.getDialogPane().lookupButton(buttonTypeCreate);
createButton.disableProperty().bind(notComplete);
You can use the same mechanism to control the visibility of each checkmark. Create a 'incomplete' BooleanBinding for each textfield and bind it with a not binding to the visible property of the checkmark. Use all these BooleanBindings in a compound or to determine the button state. This way the button state and checkmarks will always be in sync.
I could not figure out how to return a node from GridPane.
gridPane.getChildren()
provides the list of nodes, but you already have your components textFieldDialogUserID, passwordFieldDialogPassword, passwordFieldDialogConfirmPassword.
=> Add an action listener for each of them, that checks the values when its value is changed. depending on the result, enable/disable the Create button (per default, it should be disabled).
you can have an example : http://docs.oracle.com/javafx/2/ui_controls/text-field.htm
I am trying to make dynamic buttons of fixed dimensions. I am able to alter the height but unable to change the width. The width of the button seems to MATCH_PARENT (it takes up whole screen width. If there are two buttons, each button width is half screen width and when there is only one button, button width is screen width).
TableLayout table = (TableLayout)findViewById(r.id.table);
TableRow tableRow = new TableRow(this);
table.addView(tableRow);
Button button = new Button(this);
button.setLayoutParams(new TableRow.LayoutParams(30,30));
tableRow.addView(button);
Could anybody point out where I am going wrong.
Only This You have to do to change Button Dynamically
First I get Button
Button btn = (Button) findViewById(R.id.button1);
Then Button Click event I put This Code Only
TableRow.LayoutParams lp = new TableRow.LayoutParams(70, 70);
btn.setLayoutParams(lp);
And Works Fine. Try It. Will solve your Problem
Instated "button.setLayoutParams(new TableRow.LayoutParams(30,30));" change to following code change the button Height and Width.
LayoutParams lp = new LayoutParams(40, 40);
table.addView(button, lp);
I am creating some buttons in a tablelayout. I am creating the buttons programmatically.
For example the following code is in the "onresume" method...
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(0,android.widget.TableRow.LayoutParams.MATCH_PARENT,3f);
TableLayout tableLayout = (TableLayout) findViewById(R.id.sometablelayoutivealreadydefinedinxml);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(tableParams);
Button btnOne = new Button(this);
btnOne.setId(100);
btnOne.setLayoutParams(rowParams);
btnTwo.setId(200);
btnTwo.setLayoutParams(rowParams);
tableRow.addView(btnOne);
tableRow.addView(btnTwo);
tableLayout.addView(tableRow);
I want to set the height of the button to be the SAME as the width. The width is determined by the size of the screen. The problem as I understand it,is that until the buttons are actually drawn to the screen they don't exist so any width returned will be zero. So I tried the following code.
Button yourBtn = (Button)findViewById(100);
int btnSize;
btnSize = yourBtn.getWidth();
yourBtn.setLayoutParams(new TableRow.LayoutParams(btnSize, btnSize));
Now the problem is that if I run this code immediately after creating the buttons the width of the button is returned as 0 as the button hasn't been drawn yet. However, if I assign the code to a test button then it resize the button as I want it to. However, I don't want the user to have to press a button after the screen has loaded to resize the buttons properly.
So, the question is when is the appropriate time to call this to resize the buttons properly when the activity is created but after the buttons have been drawn and how do I do this?
I'm new to JavaFX, so this might be a trivial question. I have a ListView inside a dialog that shows when the user clicks a menu button. The problem is that when the dialog shows, the first item in the ListView is already selected. I have listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<NewChoices>()) as the listener for when a user selects an item, but since the first item is already selected, when I click on the first item nothing happens. I need to find a way to make sure that no item is selected when the dialog shows. I tried using listView.getSelectionModel().clearSelection() before showing the dialog, but if the program loses then regains focus, the first item is selected again without being clicked on. How can I prevent the first item, or any item for that matter, being preselected?
Here is the code:
Pane rootPane = new Pane();
ObservableList<NewChoices> listChoices = FXCollections.observableArrayList(NewChoices.values());
ListView<NewChoices> listView = new ListView<NewChoices>();
listView.setItems(listChoices);
rootPane.getChildren().add(listView);
Stage newDialog = new Stage(StageStyle.UTILITY);
newDialog.initModality(Modality.APPLICATION_MODAL);
newDialog.setTitle("New");
Scene newDialogScene = new Scene(rootPane);
newDialog.setScene(newDialogScene);
newDialog.show();
Should be fixed in 8u40 - see https://javafx-jira.kenai.com/browse/RT-38517
Since you have only a list on your new scene, with no buttons, when the scene gains the focus, as the only focusable node is the list, its first item gets selected.
Adding some OK/Cancel buttons may help since they can gain the focus instead, but for that they need to gain first the focus.
Any of these two options will work:
Put first the button, so its the first focusable node:
listView.setItems(listChoices);
Button button = new Button("Ok");
VBox rootPane = new VBox(10, button, listView);
Stage newDialog = new Stage(StageStyle.UTILITY);
newDialog.initModality(Modality.APPLICATION_MODAL);
newDialog.setTitle("New");
Scene newDialogScene = new Scene(rootPane);
newDialog.setScene(newDialogScene);
newDialog.show();
Remove temporaly the focusable property of the list:
listView.setItems(listChoices);
listView.setFocusTraversable(false);
Button button = new Button("Ok");
VBox rootPane = new VBox(10, listView, button);
Stage newDialog = new Stage(StageStyle.UTILITY);
newDialog.initModality(Modality.APPLICATION_MODAL);
newDialog.setTitle("New");
Scene newDialogScene = new Scene(rootPane);
newDialog.setScene(newDialogScene);
newDialog.show();
listView.setFocusTraversable(true);
In any case, both require an extra node. Anyway you will need it to close the modal stage, right?
If you still want to go without buttons, you could use JDK 8u40 early versions where the problem has already been fixed, as #wzberger points out, or if this is not possible, use ControlsFX new Dialogs API (a fork of the API that will ship in JDK 8u40), and you won't have any problem.
listView.setItems(listChoices);
final Dialog dialog = new Dialog();
dialog.getDialogPane().setContent(listView);
Optional<ButtonType> result = dialog.showAndWait();
Im new to Swing, first of all. Im trying to set the background color of an audio recording applet to make it blend with my webpage (white instead of the default grey), but the change never seems to take. Heres the applet initialization...
public void init()
{
setLayout(null);
setBackground(Color.white);
JLabel recorder = new JLabel("Record");
JLabel fileName = new JLabel("Please Enter File Name");
JLabel status = new JLabel("Status...");
fnametxt = new JTextField("FileNameHere");
statustxt = new JTextField("");
record = new JButton("Record");
play = new JButton("Play");
pause = new JButton("Pause");
stop = new JButton("Stop");
send = new JButton("Upload");
listen = new JButton("Listen");
save = new JButton("Save and Submit");
//A bunch of other stuff, event listeners and whatnot.
Im using no layout managers, Im setting all the positions manually. Any ideas?
You set (presumably) the background of the Applet, but that background will only show where it is not obstructed by another component.
Depending on how you structured your GUI, there may be inner panels or other components covering the area. You need to change the color of those components, too (or alternately set them to be transparent using setOpaque(false)).
Edit: setOpaque() is only available for Swing components, not the Applet itself (since that is plain old AWT).
You probably should set the background color of the content pane.
If you're using a JPanel in your applet, you'll have to color the JPanel's content pane as well. The following code sets the background of the JPanel itself AND its content pane to white:
setBackground(Color.white);
getContentPane().setBackground(Color.white); //Color JPanel