Skip to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[SUGGESTION] String List GUI

Featured Replies

 

1. A description of the suggestion

GUI to prompt for user input; the input is a list of strings.

 

2. How will this suggestion impact scripters and/or botters?

Scripters: very simple to use and can add initial values

dialogUtility.showStringListDialog("Food", true, new String[] { "SHARK", "LOBSTER", "TUNA" })

Botters: very simple to use; is user-friendly (case-insensitive, no duplicates, list is constantly sorted A-Z)

 

3. Post any examples such as code or pictures to supplement your description (optional)

 

LRStringList.java

package com.liverare.api.swing;

import static java.awt.GridBagConstraints.BOTH;
import static java.awt.GridBagConstraints.HORIZONTAL;
import static java.awt.GridBagConstraints.NONE;
import static java.awt.GridBagConstraints.NORTH;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JToolBar;

/**
 * 
 * String list designed to make getting user input simpler.
 * 
 * @author LiveRare
 *
 */
public class LRStringList extends JDialog {
	
	public static final Comparator<String> SORT_BY_STRING = (a, b) -> a.compareToIgnoreCase(b);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public JList<String> list;
	public JScrollPane listScrollPane;
	public JTextField textField;
	public JButton addButton;
	public JButton removeButton;
	public JButton removeAllButton;
	public JToolBar toolBar;
	public JButton submitButton;

	/**
	 * Constructor; initialise view, components, then configure the view and
	 * listeners
	 */
	public LRStringList() {
		super();
		initialiseView();
		initialiseComponents();
		confingureView();
		configureListeners();
	}
	
	/**
	 * Initialise JFrame display
	 */
	private void initialiseView() {
		setSize(350, 400);
		setType(Type.POPUP);
		setLayout(new GridBagLayout());
		setResizable(false);
		setLocationByPlatform(true);
		setModal(true);
		setModalityType(ModalityType.APPLICATION_MODAL);
		setAutoRequestFocus(true);
	}
	
	/**
	 * Initialise components
	 */
	private void initialiseComponents() {
		list = new JList<>(new DefaultListModel<>());
		list.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		list.setFixedCellHeight(20);
		listScrollPane = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		textField = new JTextField();
		addButton = new JButton("Add");
		removeButton = new JButton("Remove");
		removeAllButton = new JButton("Remove all");
		toolBar = new JToolBar();
		submitButton = new JButton("Submit");
	}

	/**
	 * Configure components and add to display
	 */
	private void confingureView() {
		
		toolBar.setFloatable(false);
		toolBar.add(addButton);
		toolBar.addSeparator();
		toolBar.add(removeButton);
		toolBar.add(new JPanel(null));
		toolBar.add(removeAllButton);
		
		getRootPane().setDefaultButton(submitButton);
		
		add(listScrollPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, NORTH, BOTH, new Insets(5, 5, 0, 5), 0, 200));
		add(toolBar, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, NORTH, HORIZONTAL, new Insets(0, 5, 5, 5), 0, 0));
		add(textField, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0, NORTH, HORIZONTAL, new Insets(0, 5, 5, 5), 0, 0));
		add(new JSeparator(), new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0, NORTH, HORIZONTAL, new Insets(10, 0, 0, 0), 0, 0));
		add(submitButton, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, NORTH, NONE, new Insets(15, 15, 15, 15), 150, 0));
	}

	/**
	 * Add listeners to components
	 */
	private void configureListeners() {
		
		final ActionListener addValueListener = new AddValueListener();
		final ActionListener removeValueListener = new RemoveValueListener();
		
		removeAllButton.setActionCommand("ALL");
		
		textField.addActionListener(addValueListener);
		addButton.addActionListener(addValueListener);
		
		removeButton.addActionListener(removeValueListener);
		removeAllButton.addActionListener(removeValueListener);
	}
	
	/*
	 * OTHER
	 */

	/**
	 * 
	 * @return List model
	 */
	public DefaultListModel<String> getModel() {
		return (DefaultListModel<String>) list.getModel();
	}

	public void clearTextField() {
		textField.setText("");
	}
	
	public String getText() {
		return textField.getText();
	}

	/**
	 * 
	 * @return All strings in the list
	 */
	public List<String> getValues() {
		return Collections.list(getModel().elements());
	}

	/**
	 * Add value to list
	 * 
	 * @param value
	 *            - String object
	 * @return <tt>Value added to list</tt>
	 */
	public boolean addValue(String value) {

		boolean result = false;

		if (value != null && !value.isEmpty() && !containsValue(value)) {

			value = value.toUpperCase();

			getModel().addElement(value);
			
			sortValues();
			
			result = true;
		}

		return result;
	}
	
	/**
	 * Check if list contains value
	 * 
	 * @param value
	 *            - String object
	 * @return <tt>List contains value</tt>
	 */
	public boolean containsValue(String value) {
		
		boolean result = false;

		if (value != null && !value.isEmpty()) {

			value = value.toUpperCase();

			result = getModel().contains(value);
		}

		return result;
	}

	/**
	 * Remove all values from list
	 */
	public void removeAllValues() {
		getModel().removeAllElements();
	}

	/**
	 * 
	 * @return Selected values
	 */
	public List<String> getSelectedValues() {
		return list.getSelectedValuesList();
	}

	/**
	 * Remove all selected values from list
	 */
	public synchronized void removeAllSelectedValues() {
		getSelectedValues().forEach(getModel()::removeElement);
	}
	
	/**
	 * Sort values
	 */
	public synchronized void sortValues() {
		
		List<String> values = getValues();
		
		if (values != null && !values.isEmpty()) {
			
			values.sort(SORT_BY_STRING);
			
			removeAllValues();
			
			values.forEach(getModel()::addElement);
		}
	}
	
	/**
	 * Add value
	 */
	private class AddValueListener implements ActionListener {

		@Override
		public synchronized void actionPerformed(ActionEvent e) {
			
			addValue(getText());
			
			clearTextField();
			
			textField.requestFocus();
		}
		
	}
	
	/**
	 * Remove value
	 */
	private class RemoveValueListener implements ActionListener {

		@Override
		public synchronized void actionPerformed(ActionEvent e) {
			
			final String actionCommand = e.getActionCommand();

			if (actionCommand != null) {
				
				if (actionCommand.equalsIgnoreCase("ALL")) {
					
					removeAllValues();
					
				} else {
					
					removeAllSelectedValues();
				}
			}
		}
	}
}

 

DialogUtility.java

package com.liverare.api.swing;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.JDialog;

import org.osbot.rs07.script.API;
import org.osbot.rs07.script.Script;

/**
 * 
 * @author LiveRare
 *
 */
public class DialogUtility extends API {
	
	private Script currentScript;
	
	public DialogUtility() {
	}
	
	@Override
	public void initializeModule() {
		currentScript = bot.getScriptExecutor().getCurrent();
	}
	
	/**
	 * Show an input for a list of Strings
	 * 
	 * @param title
	 *            - Title for the frame
	 * @param required
	 *            - stop script if form dismissed; focus text field if form
	 *            submitted with no content
	 * @param initialValues
	 *            - Initial values to add to the list
	 * @return List of user-inputted strings
	 */
	public List<String> showStringListDialog(String title, boolean required, String[] initialValues) {

		List<String> results = new ArrayList<>();
		LRStringList view = new LRStringList();
		
		view.setTitle(title);
		view.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		view.setLocationRelativeTo(bot.getCanvas());
		
		if (initialValues != null && initialValues.length > 0) {
			
			for (String initialValue : initialValues) {
				
				view.addValue(initialValue);
			}
		}
		
		view.submitButton.addActionListener(event -> {
			results.addAll(view.getValues());
			view.dispose();
			currentScript.resume();
		});
		
		if (required) {
			view.addWindowListener(new WindowAdapter() {

				@Override
				public void windowClosing(WindowEvent e) {

					currentScript.stop(false);
				}
			});
		}
		
		view.setVisible(true);

		currentScript.pause();

		return results;
	}
	
	/**
	 * Show an input for a list of Strings
	 * 
	 * @param title
	 *            - Title for the frame
	 * @param required
	 *            - stop script if form dismissed; focus text field if form
	 *            submitted with no content
	 * @param initialValues
	 *            - Initial values to add to the list
	 * @return List of user-inputted strings
	 * @see {@link DialogUtility#showStringListDialog(String, boolean, Collection)}
	 */
	public List<String> showStringListDialog(String title, boolean required) {
		return showStringListDialog(title, required, null);
	}
	
}

 

  • Alek locked this topic
Guest
This topic is now closed to further replies.

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.