creating a button when another button is clicked in java - java

I am not a java programmer. But i have to solve this problem in java. I have a textbox and a button. after entering some text in the text box , by clicking the button i have to generate a new button whose text is same as that in the textbox. I have to create the buttons and add then to a Jpanel. Can anyone help me with this.
This is the actionPerformed() i wrote, which was not working
public void actionPerformed(ActionEvent e) {
String str=textFeild1.getText();
panel1.add(new JButton(str));
}

Try adding panel1.validate();
public void actionPerformed(ActionEvent e) {
String str=textFeild1.getText();
panel1.add(new JButton(str));
panel1.validate();
}
The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
validate()
In your case you add a component and you have to validate it.

Related

Dynamically adding buttons during runtime in Swing

I have this strange problem when I add buttons to the toolbar. I added action listener to one button added before the frame is shown and it works fine:
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
toolbar.add(new JButton("new button"));
}
});
I also added a piece of code that is supposed to add a new button after some plugins are loaded, and for some reason that piece of code does not work.
#Override
public void handle() {
System.out.println("Button added");
MainFrame frame = Application.getInstance().getMainFrame();
frame.getToolbar().add(new JButton("Plugin button"));
frame.getToolbar().revalidate();
frame.getToolbar().repaint();
System.out.println(frame.getToolbar().getComponents().length); // It is definitely being added, just not shown
}
The button is definitely being added, just not shown.
I would really appreciate any help since this thing is blocking me from progressing any further.
I found out what the problem was. The problem was that I instantiated MainFrame twice, first by calling Application constructor in main and then when calling Application.getInstance(), so all components added to the MainFrame were removed.

Java Swing Call Method Before Focus Change

I am working with a java swing form that contains a JTextField. The text field needs input validation , validation is performed when the field losses focus . i.e clicking anyware apart from the field itself.
The form also contains a JButton to clear(cancel) the form . Now whenever I press the cancel button and the Textfield is the currently focused component the validation function is invoked. This should not happen.
I have tried InputVerifier and Focuslistener
Is there a way I can know what component caused the focus to change ?
This is fine but the form contains a JButton to clear(cancel) the form
You can use:
clearButton.setVerifyInputWhenFocusTarget(false);
to prevent the InputVerifier from being invoked when you click on the button.
You can use a FocusListener to react to a focusLost event. There you can retrieve the destination of the focus via getOppositeComponent() from the FocusEvent. If the destination is the clear/cancel/whatever button do nothing, otherwise validate.
A very basic example for this:
JTextField textField = new JTextField(15);
textField.addFocusListener(new FocusAdapter() {
#Override
public void focusLost(FocusEvent e) {
Component comp = e.getOppositeComponent();
if (comp instanceof JButton) {
JButton button = (JButton) comp;
String buttonText = button.getText();
if (buttonText.equals("Cancel")) {
return;
}
}
// do the validation
System.out.println("validate");
}
});
Actually camickr's solution is a lot simpler than mine above. I had no idea that there was built in solution for this.

How to restart a Java Application launching a specific class of my application?

I have the following situatation:
I have a Java Swing application.
In the class that implement my GUI I have a button named Log Out tath is binding to an event listener that handle the click event, something like it:
JButton logOutButton = new JButton("LogOut");
header.add(logOutButton);
Then in the same class that implement my GUI I have declared the ActionListener that handle this event using an inner class:
logOutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("logOutButton clicked !!!");
System.exit(0);
}
});
In this moment when I click the logOutButton button the program end. I would that instead exit it is restarted by running a specific class called LoginForm (the class that implement the login form GUI)
What can I do to do this thing?
Tnx
Andrea
You don't really need to close/open window junky approach at all. Just use Card Layout:
set Frame's content pane's layout to card layout.
getContentPane().setLayout(new CardLayout());
Put your different Form's content code inside different panel and
add them to the content pane with their corresponding name, for example:
getContetnPane().add(logInFormPanel, "logIn Form");
Now you can simulate the card to appear whenever necessary by calling CardLayout.show(Container parent, String name). For example:
logOutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("logOutButton clicked !!!");
CardLayout cl = (CardLayout)(getContentPane().getLayout());
cl.show(getContentPane(), "logIn Form");
}
});
Check out a CardLayout demo from my another answer.

How action a button to open a new pane in java for a GUI

I am creating a GUI in which my home page has a button labelled "Welcome to the Panel"
The point is that when you press on this button, it will navigate to a new page where I will have other functions. My only problem is that I dont know the syntax or how that when clicking a button, it will navigate to new page.
JButton btn = new JButton("Welcome to the Panel");
btn.setActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// Here you open the other window. You can use JFrame, JOptionPane or JDialog
}
});
button.addActionListener(new ActionListner()
{
public void actionPerformed(ActionEvent ae)
{
//code to show pane
}
});
You need to register an ActionListener on your button and inside that action listener you make that panel (the page) visible.
How you do that depends on your layout, i.e. with a CardLayout you'd show the corresponding card (here's the doc). Using other layouts you might have to replace a component, e.g. if you use a BorderLayout and your content is placed in the center, replace the center component with the panel you want to show.
Note that if you're not familiar with layout managers yet, you should first have a look at those before doing dynamic changes to the ui (like navigation etc.).

Get focus on a JTextField inside a CardLayout

I have a JTextField inside a JPanel A which is a part of CardLayout. When this A gets shown, I want to set the focus automatically to the JTextField (i.e. the cursor is flashing in the text field so the user doesn't need to click on it to enable the input). I tried calling requestFocusInWindow() on the JTextField object at initialization, but that doesn't seem to work. Do I need to call this method every time when A gets displayed? Thanks.
Maybe you can try requestFocusInWindow() when the panel is shown ?
something like this?
jPanel.addComponentListener(new ComponentAdapter() {
#Override
public void componentShown(java.awt.event.ComponentEvent e)
{
jTextField.requestFocusInWindow();
}
});

Categories