Jump to content

More JForm trouble


Recommended Posts

Posted

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:

Posted
					//---- 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?

Posted (edited)

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
Posted

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.

Posted (edited)
 

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
Posted

 

 

 

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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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