Jump to content

More JForm trouble


Paradox68

Recommended Posts

4a78cee743c6f174a0bbc4cf5ad24027.png

 

 

Trying to create a method to send list items back and forth using their respective buttons.

				//---- button1 ----
				button1.setText("> Foes >");
				button1.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						if (!list1.isSelectionEmpty()) {
							list2.add(list1.getSelectedValue(), list1.getComponent(list1.getSelectedIndex()));
							list1.remove(list1.getComponent(list1.getSelectedIndex()));
						}
					}
				});

Tried this method. I know i'm doing something wrong because I have no idea what a component is and I couldn't find a method that didn't use components with Intellisense. Someone helppppp D:

Link to comment
Share on other sites

					//---- list1 ----
					list1.setModel(new DefaultListModel<String>() {
						/**
						 * 
						 */
						private static final long serialVersionUID = 5147152121755459135L;
						String[] values = {
							main.monstersNearby[0],
							main.monstersNearby[1],
							main.monstersNearby[2],
							main.monstersNearby[3],
							main.monstersNearby[4],
							main.monstersNearby[5],
							main.monstersNearby[6],
							main.monstersNearby[7],
							main.monstersNearby[8],
							main.monstersNearby[9],
							main.monstersNearby[10],
							main.monstersNearby[11],
							main.monstersNearby[12],
							main.monstersNearby[13],
							main.monstersNearby[14],
							main.monstersNearby[15],
							main.monstersNearby[16],
							main.monstersNearby[17],
							main.monstersNearby[18],
							main.monstersNearby[19],
						};
						@Override
						public int getSize() { return values.length; }
						@Override
						public String getElementAt(int i) { return values[i]; }
					});
					scrollPane1.setViewportView(list1);
				}

				//---- button1 ----
				button1.setText("> Foes >");
				button1.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						if (!list1.isSelectionEmpty()) {
							list2.setModel
							list1.remove(list1.getComponent(list1.getSelectedIndex()));
						}
					}
				});

 

If you use a GUI creation tool make sure you change all list models to DefaultListModel because most of them generate AbstractListModel which cannot be used properly. Also adding items to a JList is done via adding the respective item to its list model.

 

 

What do you think would be a good method to handle this?

Link to comment
Share on other sites

Proper list code

163595f83c.png

 

 

Properly using it

ea0da19e4b.png

 

 

In your snippet, what is core.removeComplexTask(list.getSelectedIndex());

 

Also why isn't it letting me reference addValues()? Sorry for the needing spoonfeeding I just came back from a long break trying to figure it all out again and I hate GUIs. I still can't seem to get it to add a value to the list.

Edited by Paradox68
Link to comment
Share on other sites

In your snippet, what is core.removeComplexTask(list.getSelectedIndex());

 

Also why isn't it letting me reference addValues()? Sorry for the needing spoonfeeding I just came back from a long break trying to figure it all out again and I hate GUIs. I still can't seem to get it to add a value to the list.

You dont need to change anything to the addValues() method. Use it as I wrote it. Put all values in the String[] above that. Ignore core related code because that snippet is just an implementation of JFrame in my scripts and the core stuff is not related to OSBot API. Was meant to show how the listmodel.add and listmodel.remove methods work.

Link to comment
Share on other sites

 

Tried this method. I know i'm doing something wrong because I have no idea what a component is and I couldn't find a method that didn't use components with Intellisense. Someone helppppp D:

 

I recommend that you learn how to do these basic Swing tasks without a GUI builder, before using a GUI builder

 

 

Just create two JList<String>  intialised with DefaultListModel<String>   and then use model.addElement  and model.remove:

String[] friends = { "Noob1", "Noob2", "Noob3", "Noob4" };

DefaultListModel<String> friendsModel = new DefaultListModel<>();
for(String friend : friends){ friendsModel.addElement(friend); }
JList<String> friendsList = new JList<>(friendsModel);

DefaultListModel<String> foesModel = new DefaultListModel<>();
JList<String> foesList = new JList<>(foesModel);

JButton addFoe = new JButton("> Foes >");
addFoe.addActionListener(e -> {

    for(int i : friendsList.getSelectedIndices())
        foesModel.addElement(friendsModel.remove(i));
});

JButton addFriend = new JButton("< Friends <");
addFriend.addActionListener(e -> {

    for(int i : foesList.getSelectedIndices())
        friendsModel.addElement(foesModel.remove(i));
});
Edited by Explv
  • Like 1
Link to comment
Share on other sites

 

 

 

I recommend that you learn how to do these basic Swing tasks without a GUI builder, before using a GUI builder

 

 

Just create two JList<String>  intialised with DefaultListModel<String>   and then use model.addElement  and model.remove:

String[] friends = { "Noob1", "Noob2", "Noob3", "Noob4" };

DefaultListModel<String> friendsModel = new DefaultListModel<>();
for(String friend : friends){ friendsModel.addElement(friend); }
JList<String> friendsList = new JList<>(friendsModel);

DefaultListModel<String> foesModel = new DefaultListModel<>();
JList<String> foesList = new JList<>(foesModel);

JButton addFoe = new JButton("> Foes >");
addFoe.addActionListener(e -> {

    for(int i : friendsList.getSelectedIndices())
        foesModel.addElement(friendsModel.remove(i));
});

JButton addFriend = new JButton("< Friends <");
addFriend.addActionListener(e -> {

    for(int i : foesList.getSelectedIndices())
        friendsModel.addElement(foesModel.remove(i));
});

 

 

Thank you. I've solved the list building issues but you wouldn't happen to know why my script closes itself instantly after starting? Would you be able to help me for a minute on it because I can't find any explanation. SOMETIMES the script will run and the GUI will build, majority of the time it instantly exits script and says Error at onStart(). I only have the GUI opening method in onStart so I know it's the UI class but I can't imagine why.. 0 errors and 0 warnings.

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...