Friends
I am a beginner and trying to develop a system level java swing software.
I have 2 JRadio buttons viz A & B in a RadioButton group bg.
I want to keep the radio button selection after restart or until further selection.
Searched for this long in net but getting code for PHP,HTML etc.
Somebody please help me.
rdbtA = new JRadioButton("A");
contentPane.add(rdbtA);
rdbtB = new JRadioButton("B");
contentPane.add(rdbtB);
ButtonGroup bg = new ButtonGroup();
bg.add(A);
bg.add(B);
When you start your application all the graphic components ar re-created, so you have to save the selection in some way when you close your program or when the selection changes (the easiest is to save your choice in a file) and restore it when the software is started (after the buttons creation).
Let me explain this in code:
rdbtA = new JRadioButton("A");
contentPane.add(rdbtA);
rdbtB = new JRadioButton("B");
contentPane.add(rdbtB);
/*
1. If exists a save-file, open it (else, ignore 2. and 3.)
2. read the value of previous A and previous B
3. Set these values to rdbtA and rdbtB
*/
//Rest of the code
Related
Hi guy I just started learn some JavaFX and I made a simple product TableView program which you can add and delete items. the items include name price and quantity.
I tried to prevent a bug so I did everytime the name\price\quantity fields are empty the add button will be disabled.
addButton = new Button("Add");
if(name.getText().isEmpty()&& price.getText().isEmpty() && quantity.getText().isEmpty()) {
addButton.setDisable(true);
} else {
addButton.setDisable(false);
addButton.setOnAction(e-> addButtonClicked());
}
The button is indeed disable but when I input some data to the field it remain disable.
I would like if someone can help me figure it out.
srry for the broken english ;)
Ok I fixed this, if someone got the same issue this is the solution :
addButton = new Button("Add");
addButton.disableProperty().bind(
name.textProperty().isEmpty()
.or(price.textProperty().isEmpty())
.or(quantity.textProperty().isEmpty())
);
So, i'm asking cause i searched and didn't found nothing about this, i don't know if i'm only searching it wrong.
I'm building a POS (Point of Sale) for my final School work but instead off adding the buttons manually i wanted to make an interface for the admn where he could add the buttons to the main project (ex. I want to add the button for Meat, Fish, etc.)
It's much likely to be easy to do it, my other doubt becomes with, if the button is generated how it will be called so i can use it later on?
With the NetBeans form designer you can see what code must be created.
Then instead of jButton1, jButton2 use List<JButton> buttons = new ArrayList<>();
In the initComponents (or after its call) create the buttons dynamically, using some list with button data: caption Meat / Fish / ... and so on. These data could come from a file you generated, so they are persist even if quitting the application.
A file can be read as:
Path path = Paths.get("buttons.txt");
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
for (String line : lines) {
String[] words = line.split(";\\s*");
if (words.length > 2 && words[0].equals("button")) {
JButton button = new JButton(word[1]);
button.addActionListener(this); ...
... add(button);
buttons.add(button);
}
}
I think you shouldn't generate new buttons. The best way is to hide the buttons you've created by calling button.setVisibility(View.Gone). So just create buttons and call setVisibility(View.Gone) in onCreate. And where needed make them visible by calling button.setVisibility(View.visible).
Ia m new to vVadin. I created one project with grid with two columns but i want to add one Textfield column and one checkbox column and check all checkboxes when click on header checkbox.
List<Person> people = Arrays.asList(
new Person("Nicolaus Copernicus", 15),
new Person("Galileo Galilei", 15),
new Person("Johannes Kepler", 15));
TextField txt =new TextField();
CheckBox chk=new CheckBox();
// Create a grid bound to the list
Grid<Person> grid = new Grid<>();
grid.setItems(people);
grid.addColumn(Person::getName).setCaption("Name");
grid.addColumn(Person::getAge).setCaption("Year of birth");
grid.addColumn(Person-> new TextField());
layout.addComponents(grid);
setParent(layout);
can anyone suggest me.how to add those two columns
Selection via check boxes works with the multi selection mode, see docs. On the same page you can read about ComponentRenderer which allows to put any component in a column. Note that this feature is available since Vaadin 8.1. released few days ago.
I am creating a window with two image buttons (using TriplePlay in my playN game).
Now I need dynamic text on these buttons. But when I add buttons with images (setIcon), I am not able to add Text on it same time. Please check the following code block I use now.
Interface iface = new Interface(null);
pointer().setListener(iface.plistener);
Styles buttonStyles = Styles.none().add(Style.BACKGROUND.is(new NullBackground())).
addSelected(Style.BACKGROUND.is(Background.solid(0xFFCCCCCC)));
Stylesheet rootSheet = Stylesheet.builder().add(Button.class, buttonStyles).create();
Root buttonroot = iface.createRoot(AxisLayout.horizontal().gap(150), rootSheet);
buttonroot.setSize(width_needed, height_needed);
buttonroot.addStyles(Styles.make(Style.BACKGROUND.is(new NullBackground())));
graphics().rootLayer().add(buttonroot.layer);
Button you = new Button().setIcon(buttonImage);
Button friend = new Button().setIcon(buttonImage);
buttonroot.add(you).add(friend);
buttonroot.layer.setTranslation(x_needed, y_needed);
Root nameroot = iface.createRoot(AxisLayout.horizontal().gap(300), rootSheet);
nameroot.setSize(width_needed, height_needed);
nameroot.addStyles(Styles.make(Style.BACKGROUND.is(new NullBackground())));
graphics().rootLayer().add(nameroot.layer);
name = new Label("YOU");// we need the dynamic string variable instead
friendName = new Label("FRIEND"); // we need the dynamic string variable instead
nameroot.add(name).add(friendName);
nameroot.layer.setTranslation(x_needed, y_needed);
here I have tried making a root then add button with images to it then making another root and add labels on it so that it will be show like text on the image buttons. But I know this is a bad way of doing it, and the alignment will not be according to what needed as it a dynamic text. Is there anyway to add a button, with image and a label on it?
Thanks in anticipation
Creating a button with text and an icon is trivial:
Button button = new Button("Text").setIcon(iconImage);
You can then change the text on the button any time you like, like so:
button.text.update("New Text");
If you want a button with a background image with text rendered over the background, then do the following:
Button button = new Button("Text").
addStyles(Background.is(Background.image(bgImage)));
Note that you will need the latest TriplePlay code (from Github) to use the ImageBackground. The latest code also supports Flash-style "scale9" backgrounds:
Button button = new Button("Text").
addStyles(Background.is(Background.scale9(bgImage)));
I'm a total noob in SWT, just getting started, but I have previously worked with GUI frameworks such as Swing.
I have a Composite which contains a Group and a Button. The Group is initially set to invisible (using group.setVisible(false)), and is set to visible when clicking the button. This starts a thread which perform some calculations, updating a label inside the group with the progress (kind of a manual progress bar. This is what the customer wants :) ).
Anyway, for some reason, the group only appears after the thread has finished running, and I can't seem to make it appear, no matter what I've used (tried calling this.pack(), this.layout(), this.getShell().layout(), redraw() on a variety of controls in the path - nothing).
Here's how I create the group:
statusGroup = new Group(this, SWT.SHADOW_NONE);
statusGroup.setLayout(null);
statusGroup.setVisible(false);
percentCompleteLabel = new Label(statusGroup, SWT.NONE);
percentCompleteLabel.setText("0% complete");
Here's how I'm updating it from the Button's SelectionListener:
this.statusGroup.setVisible(true);
this.statusGroup.pack(true);
this.statusGroup.layout();
this.getShell().layout();
myThreadStartupCode(); // psuedo
while (!workIsDone) // psuedo
{
final int progress = myProgressCalcMethod(); // psuedo
percentCompleteLabel.setText(progress + "% complete");
percentCompleteLabel.pack(true);
this.layout();
this.redraw();
Thread.sleep(100);
}
Any clue would be appreciated.
Apparently, the solution is to use Display.getCurrent().update();