zeroter5 Posted July 29, 2017 Share Posted July 29, 2017 Hello Osbot community, I am currently working on a script where you enter a mob name in an J-input dialog window, and select an item to eat from a dropdown(JComboBox) menu and the bot will kill the any nearby mobs with the name you entered and when your character drops below a certain hp mark your character will eat the food. At the current moment I have the script set up such that each of these 2 actions is accomplished in separate jframes/windows. Can anyone point me in the right direction on combining the windows into one? Any help would be much appreciated! i've included pictures of part of my code in these imgur links below (how the 2 windows are displayed when program is run) the createGUI method call creates the jcombobox in this image while Joptionpane is the input box Quote Link to comment Share on other sites More sharing options...
Chris Posted July 29, 2017 Share Posted July 29, 2017 add a JTextField to the gui mobName = JTextField#getText Quote Link to comment Share on other sites More sharing options...
Apaec Posted July 29, 2017 Share Posted July 29, 2017 (edited) Heyo, Firstly, instead of doing this, perhaps you could just make the script heal up with any items in the inventory with an eat option? That way, you wouldn't even need this extra setup step! If you still want to do it, I would suggest creating your own frame. JOptionPane is fine for small things where you need one or two settings to be saved, but when you start working with more complicated configurations, having multiple JOptionPanes is a bit messy. (You could perhaps extend panel and attach this to your option pane, but I personally prefer creating a whole new frame). You could do this by creating a new class which extends JFrame, and perhaps create and position text fields within this frame in the constructor of the class. There will be plenty of swing tutorials online as it's a pretty standard thing, and you can even get window builders to help you generate the code (often as plugins to IDEs). I would recommend using a layout other than absolute though, since 4k and other higher resolution screens are becoming more widely used so re sizeable windows are very preferable (swing issues :/). 26 minutes ago, Chris said: add a JTextField to the gui mobName = JTextField#getText You cannot do this directly to the JOptionPane! Edit: it looks like you've already created your own frame, do as Chris said! (apologies, I misread your post) Edit2: You can also (I believe) get rid of that potentially infinite sleep after the mobName initialisation, since I think JOptionPane does that for you. Just be sure to run some string analysis on that variable to make sure it's valid. Edited July 29, 2017 by Apaec Quote Link to comment Share on other sites More sharing options...
Alek Posted July 29, 2017 Share Posted July 29, 2017 JOptionPane is some convenient special type of JFrame. You cannot actually combine the two. Luckily, JOptionPane is made simply of a JLabel ("Enter Mob Name"), and a JTextField component. Simply add these two components to your GUI. 1 Quote Link to comment Share on other sites More sharing options...
zeroter5 Posted July 30, 2017 Author Share Posted July 30, 2017 Thanks for the suggestions guys, i tried adding in the label and textfield into a new panel and then adding that new panel to the gui as a whole. So i have the panel that contains the dropdown box/label and then the textfield one. Here's what I end up with private void createGUI(){ final int guiWidth = 350, guiHieght = 150; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // Calculating x and y coordinates final int gX = (int) (screenSize.getWidth() / 2) - (guiWidth / 2); final int gY = (int) (screenSize.getHeight() / 2) - (guiHieght / 2); // create a new JFrame with the title "GUI" gui = new JFrame("GUI"); // set the x coordinate, y coordinate, width and height of the GUI gui.setBounds(gX, gY, guiWidth, guiHieght); gui.setResizable(true); // Create a sub container JPanel JPanel panel = new JPanel(); JPanel panel1 = new JPanel(); gui.add(panel); gui.add(panel1); JLabel label = new JLabel("Select a food type:"); // create a label JLabel label1 = new JLabel("Enter Mob Name"); label.setForeground(Color.white); label1.setForeground(Color.white); panel.add(label); // add it to the JPanel panel1.add(label1); // create a select box for food options JComboBox<String> foodSelector = new JComboBox<>(new String[]{"Shrimps", "Trout", "Salmon","Lobster", null}); JTextField mobName = new JTextField(); // add an action listener, to listen for user's selections, assign to a variable called selectedFood on selection. foodSelector.addActionListener(e -> selectedFood = foodSelector.getSelectedItem().toString()); // add the select/text-field box to the JPanel panels panel.add(foodSelector); panel1.add(mobName); JButton startButton = new JButton("Start"); // add an action listener to the button startButton.addActionListener(e -> { // This code is called when the user clicks the button started = true; // Set the boolean variable started to true gui.setVisible(false); // Hide the GUI }); panel.add(startButton); // Add the button to the panel gui.setVisible(true); //makes GUI visible } Here is my code for the GUI, I think my problem may just be the panels are on top of each other but im not sure? Quote Link to comment Share on other sites More sharing options...