Have a Button Group in Java where all buttons can be deselected? - java

I want to have a Button Group in which either only one option is selected or none of them are. At the moment, I can get it to have no options ticked by default, and then if one of them is ticked only one of them can be ticked, but I also want to be able to untick the button that was selected. Is this possible?
EDIT: Ideally without having a clear all button, as it would ruin the symmetry of my GUI. Also, here is my code thus far:
ButtonGroup option = new ButtonGroup();
for(int i = 0; i < n; i++) {
JCheckBox check = new JCheckBox("", false);
option.add(check);
row3b.add(check);
}

Just use the clearSelection() method of ButtonGroup :
ButtonGroup.clearSelection()
Clears the selection such that none of the buttons in the ButtonGroup
are selected.

This snippet demonstrates how you can clear selections using ButtonGroup.clearSelection():
//The buttons
JFrame frame = new JFrame("Button test");
JPanel panel = new JPanel();
JRadioButton btn1 = new JRadioButton("Button1");
JRadioButton btn2 = new JRadioButton("Button2");
JButton clearbutton = new JButton("Clear");
panel.add(btn1);
panel.add(btn2);
panel.add(clearbutton);
frame.add(panel);
frame.setVisible(true);
frame.pack();
//The Group, make sure only one button is selected at a time in the group
ButtonGroup btngroup = new ButtonGroup();
btngroup.add(btn1);
btngroup.add(btn2);
btn1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Do whatever you want here
}
});
btn2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Do whatever you want here
}
});
clearbutton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Clear all selections
btngroup.clearSelection();
}
});
As you can see, this creates two JRadioButtons and adds them to group then makes a button that clears selections. Really simple. Or you could create your own radio button class that allows for the unchecking of the button, which is also doable relatively easily.

Related

Add object for every user input

I am trying to create a new button every time the user presses a button.
So, if I click "add" button, then a new panel/button/thing would be added to the JPanel.
Not sure if this is the same thing I'm asking for
Don't know what
guiButtons[0]
is in
if(buttonClick.getSource().equals(guiButtons[0]))
Would be nice if someone could explain
You can try as bellow code, may it help you
JPanel panel = new JPanel(new FlowLayout());
JButton button = new JButton("Add");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JButton newButton = new JButton("New Added Bnt");
panel.add(newButton);
}
});

Java JmenuItem do something when clicked not working

I am currently trying to make a GUI with a menu that has 2 options you can select from. One being "Default Settings" and one being "Custom Settings." When you click on either one, it will take you to the new jPanel that will display the proper windows, text boxes, etc for that panel. However, I cannot seem to get the mouseClicked action to actually switch between the panels. As a test, I have a simple jLabel on each panel that says "Default" for the default panel and "custom" for the custom panel, and each menu item, when clicked respectively, should switch between them. Here is my current code:
frmLegitServerAdder = new JFrame();
frmLegitServerAdder.setTitle("Legit Server Adder 5 Million");
frmLegitServerAdder.setBounds(100, 100, 546, 468);
frmLegitServerAdder.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frmLegitServerAdder.setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("Settings");
menuBar.add(mnNewMenu);
JMenuItem menuItemDefaultSettings = new JMenuItem("Default Settings");
mnNewMenu.add(menuItemDefaultSettings);
JMenuItem menuItemCustomSettings = new JMenuItem("Custom Logon Settings");
mnNewMenu.add(menuItemCustomSettings);
frmLegitServerAdder.getContentPane().setLayout(new CardLayout(0, 0));
final JPanel defaultSettingsPanel = new JPanel();
frmLegitServerAdder.getContentPane().add(defaultSettingsPanel, "name_416522810155567");
defaultSettingsPanel.setLayout(null);
JLabel lblDefaultArea = new JLabel("Default Area");
lblDefaultArea.setBounds(217, 11, 90, 14);
defaultSettingsPanel.add(lblDefaultArea);
final JPanel customSettingsPanel = new JPanel();
frmLegitServerAdder.getContentPane().add(customSettingsPanel, "name_416549691176064");
customSettingsPanel.setLayout(null);
JLabel lblCustomArea = new JLabel("Custom Area");
lblCustomArea.setBounds(235, 21, 46, 14);
customSettingsPanel.add(lblCustomArea);
menuItemDefaultSettings.addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent e)
{
defaultSettingsPanel.setVisible(true);
customSettingsPanel.setVisible(false);
}
});
menuItemCustomSettings.addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent e)
{
defaultSettingsPanel.setVisible(false);
customSettingsPanel.setVisible(true);
}
});
The code runs and the GUI displays just fine, but nothing actually happens when I click on either menu items, as it should. Any ideas?
You should NOT be using a MouseListener. Instead you should be adding an ActionListener to the menu item. Read the section from the Swing tutorial on How to Use Menus for more information.
You should be using a CardLayout when you want to swap components. See How to Use Card Layout from the same tutorial.
You need ActionListener
menuItemDefaultSettings.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
defaultSettingsPanel.setVisible(true);
customSettingsPanel.setVisible(false);
}
});
Hope this helps.

ActionListener Anonymous class between two JPanel

I'm trying to get to panels to work with each other RECURSIVELY.
When I'm on the main GUI I have the first JPanel with a Button Add Client,
Once clicked it brings me to the JPanel with a Form and then I recuperate those values,
and send them away in a JTable in the first JPanel the Main GUI.
When I then try to insert a second record. I get a blank GUI. I'm not too sure what I am doing wrong. How can I implement multiple time the same action to repeat ? Which is Load up the form, enter the information, push it on the Table, and the process repeats as much as I need it.
This is the Add Client button declaration in the MAIN GUI
Button btn_AddClient = new Button("Add Client");
btn_AddClient.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.setVisible(false);
contentPane.setVisible(false);
setContentPane(contentPaneClient);
}
});
btn_AddClient.setBounds(259, 12, 70, 22);
contentPane.add(btn_AddClient);
This is the Add Button of the Form in the second Panel
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setContentPane(contentPaneClient);
panel.setVisible(true);
contentPane.setVisible(true);
contentPaneClient.setVisible(false);
LigneJTab l = new LigneJTab(textFieldPrenomClient.getText(),textFieldNomClient.getText(), textFieldAdresseClient.getText(), chckbxHomme.isSelected(), Sport.FOOTBALL);
myModel.addLine(l);
setContentPane(contentPane);
}
});
btnAdd.setBounds(263, 40, 117, 29);
contentPaneClient.add(btnAdd);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//setContentPane(contentPaneClient);
//panel.setVisible(true);
//contentPane.setVisible(true);
//contentPaneClient.setVisible(false);
LigneJTab l = new LigneJTab(textFieldPrenomClient.getText(),textFieldNomClient.getText(), textFieldAdresseClient.getText(), chckbxHomme.isSelected(), Sport.FOOTBALL);
myModel.addLine(l);
panel.setVisible(true);
contentPane.setVisible(true);
setContentPane(contentPane);
}
});
btnAdd.setBounds(263, 40, 117, 29);
contentPaneClient.add(btnAdd);
Commented the top part and added the setContentPane(contentPane); and that worked !
Thanks !
Another idea: You don't need to swap out the content panes to ask for data. A much more elegant way is using a modal dialog box. To make one, first create a dialog class:
public class MyDialog extends JDialog {
public MyDialog(Frame parent) {
super(parent);
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
// add components to getContentPane()
// to close dialog, use setVisible(false) in listeners
}
public OutputData getData() {
OutputData data = new OutputData();
show();
// show only returns after a setVisible(false)
data.field = textField.getText();
// for example, repeat as many times as necessary
return data;
}
}
To invoke this dialog from the JFrame, use the following code:
MyDialog dialog = new MyDialog(this);
OutputData data = dialog.getData()
// now retrieve fields from data

Java- How to add more textfields by clicking a button?

I have created a frame in Java which has some textfields and buttons in it. Assuming that user wants more textfields (for example to add more data), I want to put a button and when a user clicks the button, then a new textfield should appear. then user can fill data in it and again by clicking that button another textfield should appear.
How can I do this ? What code I need to write for the button to show more and more text fields by clicking button?
Thank you !
It would be wise that instead of adding components to your JFrame directly, you add them to a JPanel. Though related to your problem, have a look at this small example, hopefully might be able to give you some hint, else ask me what is out of bounds.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JFrameExample
{
private JFrame frame;
private JButton button;
private JTextField tfield;
private String nameTField;
private int count;
public JFrameExample()
{
nameTField = "tField";
count = 0;
}
private void displayGUI()
{
frame = new JFrame("JFrame Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0, 1, 2, 2));
button = new JButton("Add JTextField");
button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
tfield = new JTextField();
tfield.setName(nameTField + count);
count++;
frame.add(tfield);
frame.revalidate(); // For JDK 1.7 or above.
//frame.getContentPane().revalidate(); // For JDK 1.6 or below.
frame.repaint();
}
});
frame.add(button);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new JFrameExample().displayGUI();
}
});
}
}
Supposing that you have a main container called panel and a button variable button which is already added to panel, you can do:
// handle the button action event
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// create the new text field
JTextField newTextField = new JTextField();
// add it to the container
panel.add(newTextField);
panel.validate();
panel.repaint();
}
});
When adding the new text field, you may need to mention some layout related characteristics, depending on the layout manager you are using (for instance if you use GridBagLayout, you will need to specify the constraints).

Select JTabbedPane Panel with JMenuItem

I have a menu-bar and a tabbed pane in a frame, and i want that if i select a menuitem, then the requested tab will open. Please help me with this, thanks!!!
In the ActionListener of the JMenuItem, you can call JTabbedPane#setSelectedIndex.
like SoboLAN said:
final JTabbedPane tabs = new JTabbedPane();
JPanel panel = new JPanel();
tabs.add("title", panel);
//add more tabs...
// here the important part starts
JMenuItem item = new JMenuItem("open tab 1");
item.addActionListener(new ActionListener() {
//this function get called when you click the item.
#Override
public void actionPerformed(ActionEvent e) {
//insert the index you want to select
tabs.setSelectedIndex(0);
}
});

Categories

Resources