Jump to content

GUI combobox update


alkku15

Recommended Posts

hi im back here again with another problem, so im doing a little thiever, my gui looks like this:

ess.PNG.9e51ac5e11807983ce5c74792b39be32.PNG

alright so all my actual thieving code is ready, but the problem is in the comboboxes... So for example (if i choose index 1 from the first combobox, i want the options change in the combobox2)... if you didnt get it heres another example ( i have 3 different items in the upper combobox1, items are "pickpocket", "stalls", "chests"... if i choose "pickpocket on the upper combobox, i want to get these items to show up on combobox2 ("man", "dog", "guard")...

this is my actionlistener code

	public String[] showoptionsforPickpocket = {"man", "woman", "xddd"};
	public String[] showoptionsforStalls = {"cake stall", "stall number 2", "jfefofajfjeofjs stall"};
	public String[] showoptionsforChest = {"ardy chest", "chestinmyass", "urmom"};
	

comboBox.addItemListener(new ItemListener() {
			  public void itemStateChanged(ItemEvent arg0) {
		            if (comboBox.getSelectedItem().equals("Pickpocket")) {
		            	JComboBox<String> comboBox_1 = new JComboBox<String>(showoptionsforPickpocket);
		        		comboBox_1.setBounds(10, 78, 164, 20);
		        		ScriptName.add(comboBox_1);
		            } else if (comboBox.getSelectedItem().equals("Stalls")) {
		            	JComboBox<String> comboBox_1 = new JComboBox<String>(showoptionsforStalls);
		        		comboBox_1.setBounds(10, 78, 164, 20);
		        		ScriptName.add(comboBox_1);
		            } else if (comboBox.getSelectedItem().equals("Chest")) {
		            	JComboBox<String> comboBox_1 = new JComboBox<String>(showoptionsforChest);
		        		comboBox_1.setBounds(10, 78, 164, 20);
		        		ScriptName.add(comboBox_1);
		            }  
		        }
	      
	    });

right now im only listening to the combobox1, do i need another listener? and im trying to do this probs the hard way (just changing the strings in the combobox2)

now if i load this up in osbot  & ingame ... at start it doesnt show anything which its supposed to be, then i choose "pickpocket" it shows the stringsforpicckpocket... then after that ii choose any other option in combobox1 it doesnt change the strings in combobox2... how do i get this working

Edited by alkku15
Link to comment
Share on other sites

You're creating a new combo box object every time you change the selection of the first one! That's unnecessary.

Since you haven't posted your full code, I can't accurately give you a solution, but try modifying the current selected item in the combo box (google 'combobox set selected item' or something along those lines)

Apa

Link to comment
Share on other sites

@Apaec ty ill look into it rn but heres the whole gui code + ty for the fast reply lol

	public void JFrame() {
		JFrame ScriptName;
		ScriptName = new JFrame();
		ScriptName.setTitle("Thiever");
		ScriptName.setBounds(100, 100, 200, 230);
		ScriptName.getContentPane();
		ScriptName.setLayout(null);
		ScriptName.setResizable(false);
		
		//LABEL AKA TITLE
		JLabel lblWhatToThiev = new JLabel("What to thiev?");
		lblWhatToThiev.setBounds(54, 1, 78, 14);
		ScriptName.add(lblWhatToThiev);
		//COMBOBOX
		JComboBox<String> comboBox = new JComboBox<String>(new String[] { "None", "Pickpocket", "Stalls", "Chests"});
		comboBox.setBounds(10, 22, 164, 20);
		ScriptName.add(comboBox);
		//LABEL AKA TITLE
		JLabel lblOptions = new JLabel("Option");
		lblOptions.setHorizontalAlignment(SwingConstants.CENTER);
		lblOptions.setBounds(54, 53, 78, 14);
		ScriptName.add(lblOptions);
		//COMBOBOX

		//CHECKBOX
		JCheckBox chckbxNewCheckBox = new JCheckBox("Eat food?");
		chckbxNewCheckBox.setBounds(10, 108, 164, 23);
		ScriptName.add(chckbxNewCheckBox);
		//START BUTTON
		JButton btnNewButton = new JButton("START");
		btnNewButton.setBounds(10, 158, 164, 23);
		ScriptName.add(btnNewButton);
		
		btnNewButton.addActionListener(new ActionListener() { //this actionlistener checks if start button is pressed
			public void actionPerformed(ActionEvent e) { //and then sets the visibility of the gui to false (closes gui)
				ScriptName.setVisible(false); //disappear when clicking on the button
				ScriptName.dispose();
			}
		});
		comboBox.addItemListener(new ItemListener() {
			  public void itemStateChanged(ItemEvent arg0) {
		            if (comboBox.getSelectedItem().equals("Pickpocket")) {
		            	JComboBox<String> comboBox_1 = new JComboBox<String>(showoptionsforPickpocket);
		        		comboBox_1.setBounds(10, 78, 164, 20);
		        		ScriptName.add(comboBox_1);
		            } else if (comboBox.getSelectedItem().equals("Stalls")) {
		            	JComboBox<String> comboBox_1 = new JComboBox<String>(showoptionsforStalls);
		        		comboBox_1.setBounds(10, 78, 164, 20);
		        		ScriptName.add(comboBox_1);
		            } else if (comboBox.getSelectedItem().equals("Chest")) {
		            	JComboBox<String> comboBox_1 = new JComboBox<String>(showoptionsforChest);
		        		comboBox_1.setBounds(10, 78, 164, 20);
		        		ScriptName.add(comboBox_1);
		            }  
		        }
	      
	    });
		
		btnNewButton.addActionListener(e -> npcname = textField.getText()); //gets the text from npc textfield part
		btnNewButton.addActionListener(e -> stallId = Integer.parseInt(textField.getText())); //gets the text from npc textfield part
		btnNewButton.addActionListener(e -> EatOrNo = chckbxNewCheckBox.getText()); //gets the text from npc textfield part
		comboBox.addActionListener(e -> whatoption = (String) comboBox.getSelectedItem());
		ScriptName.setVisible(true);
		
	}

 

Edited by alkku15
Link to comment
Share on other sites

@Apaec lol got it working this way but this is really bad code L0L

 

JComboBox<String> comboBox_1 = new JComboBox<String>(showoptionsforPickpocket);
		comboBox_1.setBounds(10, 78, 164, 20);
		ScriptName.add(comboBox_1);
		
		JComboBox<String> comboBox_2 = new JComboBox<String>(showoptionsforStalls);
		comboBox_2.setBounds(10, 78, 164, 20);
		ScriptName.add(comboBox_2);
		
		JComboBox<String> comboBox_3 = new JComboBox<String>(showoptionsforChest);
		comboBox_3.setBounds(10, 78, 164, 20);
		ScriptName.add(comboBox_3);
		
		comboBox.addItemListener(new ItemListener() {
			  public void itemStateChanged(ItemEvent arg0) {
		            if (comboBox.getSelectedItem().equals("Pickpocket")) {
		                comboBox_1.setVisible(true);
		                comboBox_2.setVisible(false);
		                comboBox_3.setVisible(false);
		            } else if (comboBox.getSelectedItem().equals("Stalls")) {
		            	  comboBox_1.setVisible(false);
			              comboBox_2.setVisible(true);
			              comboBox_3.setVisible(false);
		            } else if (comboBox.getSelectedItem().equals("Chests")) {
		            	  comboBox_1.setVisible(false);
			              comboBox_2.setVisible(false);
			              comboBox_3.setVisible(true);
		            }  
		        }
	      
	    });

 

  • Sad 1
Link to comment
Share on other sites

You could use an enum to make it a little cleaner

public enum ThievingMethods {
	STALLS("Stalls", "Cake stall", "Silk stall", "Fruit stall"),
	PICKPOCKET("Pickpocketing", "Man", "Knight of Ardougne", "Elf"),
	CHESTS("Chests", "Ardougne chest", "Some other chest");
  
	private final string description;
	private final String[] availableMethods;
	private ThievingMethods(String description, String... availableMethods) {
		this.description = description;
		this.availableMethods = availableMethods; 
	}
  
	public String[] getThievingMethods() {
		return availableMethods; 
	}
  
	@override
	public String toString() {
		return description; 
	}
}

and then populate the combobox with your enum values + set an actionListener

comboBox.setModel(new DefaultComboBoxModel<>(ThievingMethods.values()));
comboBox.addActionListener(e -> comboBox_1.setModel(new DefaultComboBoxModel<>(((ThievingMethods)comboBox.getSelectedItem()).getThievingMethods())));

By default if you populate a combobox with Objects, it will display the toString value of the objects in the drop-down.

Edited by FrostBug
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...