VAADIN -- Add TextField dynamical - java

in a VAADIN Component a User should have the Option to insert one to N Answers to a Question.
Initial there should be one Textfield for the first Answer with a Button ("Add another Answer")
At Button Push there should added a new Textfield for the second Answer, and so on ...
My Question is, how can I realize the dynamical loading of a new Textfield at Button Push?
I Added a picture of how I imagine this Problem.

Just add a click listener to the button which adds a text field to the layout. Basic Vaadin, no tricks.
If your code does not work, add it to your question.

Put the Textfield in a vertical Layout of its own, whenever the button is clicked, add a new Textfield to the vertical layout.
when its time to retrieve the answers, get the reference to the vertical layout and call getChildren(), this will return all the TextFields in the vertical layout.
something like this:
List<String> answers = verticalLaout.getChildren()
.stream()
.map(TextField::getValue)
.collect(asList());

Related

Change JLabel Text without click on Button

I am creating a quiz and I missed the labels with a transition text. I want the labels with the question and answers to be changed when the program starts. So it would not make sense to change the labels with a button. I would only do that after the first question.

how to display certain rows in JTable

Hello I have a JTable and there a 3 radio buttons at the bottom when the user clicks the buttons I want the JTable to only show certain rows for that button. I've tried deleting the rows when the button is pressed but I get an error when i click the other button that re-adds them. Is there anyway I can just hide the rows when the button is pressed and show them when the other button is pushed?
Is there anyway I can just hide the rows when the button is pressed and show them when the other button is pushed?
Use a RowFilter for your table. So you need some data in the table that you will be able to specify a filter for. Every time you click the button you will need to change the filter for the new requirements.
Read the section from the Swing tutorial on Sorting and Filtering for more information and working examples.
I think the way to go is implementing your own TableModel (preferably by extending AbstractTableModel)

how to remove the content of grid cell in swt?

In my wizard page I am using gridlayout with 3 colums. The 3 column will have remove button. I am using add button for the composite. when i push add button it will add new row to the grid layout.
Now I am trying to add listener to remove button. When I push the remove button it should remove the row in which the remove button is pushed from the gridlayout and should resize the composite.
How to achieve this. And how to get the row index of the gridlayout?
To remove the controls completely you need to call dispose() on each control and then call layout() on the parent composite.
GridLayout does not make any information about the positions of the controls available so you can't really get a row index. It does sound like you might be better using a TableViewer to show a proper table.

How can I add an extra item to an existing item in a JTabbedPane?

I've been trying to avoid to ask a question and solve this myself, but I can't seem to solve this problem. I made it work but not exactly how I wanted it.
I have an Inventory program I'm working on, and I already have some items added and divided in different tabs. I'm supposed to add new items directly from the program. I did this by adding JOptionPane, and once the item is added, it adds an extra tab panel, but what I want is the item to be added in the selected tab panel.
Here is an image of the program I have so far:
I'm sorry if it looks like a mess, I'm still working on it, and I'm still trying to add the new item to the selected tab panel, not by adding an extra tab panel.
Replace the line
tabs.addTab("New Item", null, newItemPanel, "New item panel");
With
JPanel selectedTab = (JPanel) tabs.getSelectedComponent();
selectedTab.add(newItemPanel);
selectedTab.revalidate();
You'll still have to fix the layout and appearance of the new panel, but this will add it to the current tab.

JFrame in a Java desktop application

I am developing a desktop Java application with GUI implemented through Swing. I have made a JFrame and have added three buttons on it - Add, Edit, Delete.
Now I want that whenever a user clicks on any of the button, the content specific to that button appears besides those three buttons.
So how to implement this? Should I need to add a JPanel besides those three buttons and then add the content specific to the button to that JPanel?
So far, I have taken a JFrame and have added 3 buttons on it. That's it.
For the Add button, I want to add some buttons and textfields to add information to the database.
For the Delete button, I want to add some buttons to find records in the database based on the information entered through the user in the textfield that appears when the user clicks on the Delete button.
Similar type of content for Edit button.
So how to implement this. Should I need to add a JPanel besides those three buttons and then add the content specific to the button to that JPane
That would be fine. When you push the button, you can call JPanel.removeAll() to remove all the controls currently in the control, and then just do the layout again, specific to whatever button you pushed.
If you have custom swing controls, just add your custom control the JPanel using a BorderLayout and putting in the center.
Another option would be to use a CardLayout, and flipping between the cards when a user presses one of the buttons. If the layouts for the buttons never change, that would probably be a better way to do it. Obviously if the content changes between button presses, you'll need to redo the layout each time.
Either of Chad's or Alex's answers would be fine. You will probably need to call a combination of revalidate() and repaint() on the panel that you've changed, as in the past I've noticed Swing doesn't always like panels being swapped out.
Also, have you considered using a JTabbedPane instead of manually coding the interaction with the add/edit/delete buttons?
I haven't done a lot of Java programming, but I think using 2-3 different JPanel, and make visible the one you need depending on the button that was clicked would do the trick.
I'm not sure if this is the right approach though.
I was using a JFrame to add all buttons and make a new JFrame for a new window and hide a previous one.
gven way are better. I will do that now.

Categories

Resources