I have this program where I need to randomly disable some buttons so that the user is not able to select all of them. Is there a way to disable random buttons in java?
I was thinking of using Math.random, but I don't know how to start or refer to it.....
These are the buttons.
JButton but1 = new JButton();
JButton but2 = new JButton();
JButton but3 = new JButton();
JButton but4 = new JButton();
JButton but5 = new JButton();
JButton but6 = new JButton();
JButton but7 = new JButton();
Array/Vararg Verson:
public JButton getRandomButton(JButton... buttons) {
int index = (int) (Math.random() * buttons.length);
return buttons[index];
}
List Version:
public JButton getRandomButton(List<JButton> buttons) {
int index = (int) (Math.random() * buttons.size());
return buttons.get(index);
}
Both methods will return a random JButton for you to do stuff with.
You can put them all in a list, then shuffle the list and disable the element at index 0 for example...
but note that:
it could happen that you are disabling an already disabled button, so you need to do some checks
shuffling a list is an operation that will require more time, and it depends on how many buttons you have in the list.
List<JButton> items = Arrays.asList(new JButton(), new JButton(), new JButton());
System.out.println(items);
Collections.shuffle(items);
items.get(0).setEnabled(true);
Put your buttons into a List and iterate over them. Use Random.nextBoolean to determine whether to enable or disable the button. This will give you a 50/50 distribution. If you require something else, use nextInt and a modulus.
List<JButton> myButtons = /*whatever*/;
final Random generator = new Random();
for (JButton button : myButtons)
{
button.setEnabled(generator.nextBoolean());
}
This won't guarantee that any number of the buttons are enabled at any one time. You weren't clear whether that was important.
Related
I'm trying to put multiple JButtons on a frame, using ButtonGroup.
I have to put 3 groups of buttons on a frame, seperated form each other.
The following code does create the buttons, but instead of being seperated, they seem to be "deleting" each other.
I'm expecting to see there seperate groups of buttons, 4 buttons each.
The method accepts an ArrayList of a "Question" Objects, which looks like that:
public class Question {
private String _question;
private String _option1;
private String _option2;
private String _option3;
private String _option4;
}
Here's the method. It's inside a class which inherits a frame from another class, "DisplayOnPanel" to set the Panels on the same frame. "this" refers to a frame:
public void addButtonstoFrame(ArrayList<Question> q){
ArrayList<ButtonGroup> BG = new ArrayList<>();
ArrayList<JPanel> JP = new ArrayList<>();
JLabel question1 = new JLabel();
int j=0;
for (int i = 0; i <q.size() ; i++) {
BG.add(new ButtonGroup());
JP.add(new JPanel());
JRadioButton option1 = new JRadioButton(q.get(i).get_option1());
JRadioButton option2 = new JRadioButton(q.get(i).get_option2());
JRadioButton option3 = new JRadioButton(q.get(i).get_option3());
JRadioButton option4 = new JRadioButton(q.get(i).get_option4());
BG.get(j).add(option1);
BG.get(j).add(option2);
BG.get(j).add(option3);
BG.get(j).add(option4);
JP.get(j).add(option1);
JP.get(j).add(option2);
JP.get(j).add(option3);
JP.get(j).add(option4);
this.setLayout(new BorderLayout()); //this referes to a frame
this.add(JP.get(j)); //this referes to a frame
JP.get(i).setLocation(j+10,j+10);
JP.get(j).setVisible(true);
this.setVisible(true);//this referes to a frame
j++;
}
}
And the result is just 4 butoons, instead of 12:
Thank you!
First of all, variable names should NOT start with an upper case character.
JRadioButton option1 = new JRadioButton(q.get(i).get_option1());
JRadioButton option2 = new JRadioButton(q.get(i).get_option2());
JRadioButton option3 = new JRadioButton(q.get(i).get_option3());
JRadioButton option4 = new JRadioButton(q.get(i).get_option4());
BG.get(j).add(option1);
BG.get(j).add(option2);
BG.get(j).add(option3);
BG.get(j).add(option4);
JP.get(j).add(option1);
JP.get(j).add(option2);
JP.get(j).add(option3);
JP.get(j).add(option4);
A component can only belong to a single parent container.
When you add the button to the "JP" contain, you remove it from the "BG" container.
So you need to create 4 more instances of each radio button.
I am making a basic soundboard in java and i want it so that the every 2 buttons are on a different line like:
(button) (button)
(button) (button)
This is my code as of now
JPanel p = new JPanel();
JButton one = new JButton(sound1);
JButton two = new JButton(sound2);
JButton three = new JButton(sound3);
JButton four = new JButton(sound4);
JButton five = new JButton(sound5);
p.add(one);
p.add(two);
p.add(three);
p.add(four);
p.add(five);
int n = JOptionPane.showConfirmDialog(null, p, "Test", JOptionPane.OK_CANCEL_OPTION, -1);
What would be the easiest way to do it? If i have to switch to a JFrame let me know i wont mind if that is the only option.
Just use GridLayout in your JPanel:
JPanel p = new JPanel(new GridLayout(0, 2));
First argument is the number of rows, second one is the number of columns. If you specify number of rows as 0 you are telling the layout manager that you just want to have two columns and don't know how many rows you're going to need - the rows will be created dynamically.
You can use the GridLayout, it presents components in a table. like below, 3 is the number of rows, 2 is the number of columns.
p.setLayout(new GridLayout(3, 2));
p.add(one);
p.add(two);
p.add(three);
p.add(four);
p.add(five);
I'm trying to make a GUI for that simulates elevators in a building (really to test threading/c-scan), but when generating the buttons for the elevator control panel and the buttons for each individual floor, I'm kind of stuck. I thinking about trying to generate a new pair of buttons for each floor, and generating a new control panel per elevator. Also there's the difficulty of having a variable amount of floors. Anyway my question what is this best way to go about doing this? Perhaps it's not necessary to generate new buttons for everything and just use one set and change what the actions do per floor/elevator? I'm asking because I'm not very familiar with GUIs. Thanks for the help
If all the elevators, and the control panel are the same, you can use a singular method and pass in the elevator or the control panel. CustomPanel extends JPanel and has a method foo.
public void createElevatorButtons(final CustomPanel panel) {
ArrayList<JButton> buttons = new ArrayList<>(); //arraylist of buttons we can keep track of
JPanel buttonPanel = new JPanel(); //the visible component
for(int i = 1; i <= numberOfFloors;i++) {
JButton button = new JButton(String.valueOf(i)); //creates buttons for floors 1 to max
buttons.add(button);
buttonPanel.add(button);
}
panel.add(buttonPanel);
//add the action listeners
for(JButton button : buttons) {
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton floor = (JButton) e.getSource();
int floorNumber = Integer.parseInt(floor.getText());
panel.foo(floorNumber); //we tell the elevator/panel/whatever to do something, you will have to extend JPanel to do foo
}
});
}
}
In this case that the number of floors is variable you can create an array of buttons:
JButton[] buttons = new JButton[MAX_NUMBER_OF_FLOORS];
Then when you determine the exact of number of floors at runtime, you can go to instantiate and add the buttons:
for(int i=0; i<numberOfFloors; i++) {
buttons[i] = new JButton();
controlPanel.add(buttons[i]);
}
Something like this should work.
Assign MAX_NUMBER_OF_FLOORS a big number like 100, there should be a possible limit given by the problem.
I'm creating a simple Minesweeper game in Java. Size 9x9.
I create an array of JPanels and an array of buttons; I add each button to its respective JPanel. then i add the JPanels to the JFrame.
How do i distinguish between each button on the action event?
Here's some of my code:
int gridx = 9;
int gridy = 9;
JButton[] buttons = new JButton[gridx*gridy];
JPanel[] jpanels = new JPanel[gridx*gridy];
public Minesweeper(){
super("Minesweeper");
setLayout(new GridLayout(9,9));
JPanel panel = new JPanel();
int i = 0;
for(i = 0; i<gridx*gridy; i++){
jpanels[i] = new JPanel();
buttons[i] = new JButton();
buttons[i].addActionListener(buttonEvent);
jpanels[i].setLayout(new GridLayout(1,1));
jpanels[i].add(buttons[i]);
add(jpanels[i]);
}
//buttons[67].setEnabled(false);
setSize(300,300);
setVisible(true);
}
The only way i can think about doing this is adding text to the button like so:
buttons[i] = new JButton(i);
Then calling getActionCommand() but i dont want text to show up on the button. Any other ideas?
You can use AbstractButton#setActionCommand.
In your loop:
buttons[i].setActionCommand(i+"");
Then you'll get i back when you use getActionCommand
Note I did mention in a comment on another answer that I would create a new class Mine which extends JButton which I believe to be a better and more complete solution. This however gets the job done rather quickly.
I am dynamically generating a list of name boxes, sliders and labels, and am trying to figure out how to access the values of the sliders and change the labels. Other posts suggest using an array, but I have no idea where to begin with that.
My code is as such:
public Tailoring(int n) {
/*initComponents();*/
JPanel containerPanel = new JPanel();
containerPanel.setLayout(new BoxLayout(containerPanel, BoxLayout.PAGE_AXIS));
this.add(containerPanel);
JLabel Title = new JLabel("Tailoring:");
containerPanel.add(Title);
for(int i = 0; i < n; i++){
JPanel rowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTextField NameBox = new JTextField("Guest " + (i+1));
JSlider TipSlider = new JSlider();
JLabel TipCost = new JLabel();
rowPanel.add(NameBox);
rowPanel.add(TipSlider);
rowPanel.add(TipCost);
containerPanel.add(rowPanel);
}
}
You can create a new class YourPanel which extends JPanel.
Instead of the statement
JPanel rowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
you can use
YourPanel rowPanel = new YourPanel(new FlowLayout(FlowLayout.LEFT));
Define textfield, slider and label as the properties of this YourPanel class.
Provide getters/setters for each field. Then use an array or ArrayList of YourPanel objects in your application. You will be able to reach the nth panel's label with a call like:
panels.get(n).getJLabel();
It appears that you'd like to change the value displayed in the JLabel when the associated JSlider is modified, right? The best way to associate pairs of objects in Java is with a Map structure:
Map<Component, JSlider> sliderToLabel = new HashMap<Component, JSlider>();
for (int i = 0; i < n; i++) {
// after your loop code
sliderToLabel.put(TipSlider, TipCost); // map the slider to its label
}
You will be able to get a reference to the JSlider in the code that listens for changes on that component.
JLabel updateLabel = sliderToLabel.get(targetedSlider);
updateLabel.setText("updated text");
Notes
As a matter of convention, variable names should begin with lower case letters
The event listener I alluded to should also be attached in the loop. See Writing Event ListenersOracle