1

I work in networking, trying to make it easier to change IP, gateway, and netmask on my laptop when I connect to different VLANs. Using Eclipse Neon trying to make a GUI with just a small window with Jtextfield box that I can input the VLAN that I want, and the program will change the network settings for me.

here is what I have so far, but I am stuck, I need help figuring out how to take the jtextfield 2 digit input (example 01,02,20) and change the IP, gateway, and netmask according to the VLAN input. For example, if user inputs 01 in jtextfield, the program should change the ip address into 10.1.10.5 gateway into 10.1.1.1 and netmask into 255.255.255.0

I have multiple questions:

  1. How can I have the user input only a 2 digit number into jtextfield (no letters)?
  2. How can I take a users input into jtextfield and do something with it?
package ipchanger;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Dialog.ModalExclusionType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Changingip {

private JFrame frmVlanChanger;
private JTextField textField;

public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {Changingip window = new Changingip();
                window.frmVlanChanger.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Changingip() {
    initialize();
}

private void initialize() {
    frmVlanChanger = new JFrame("Vlan Changer");
    frmVlanChanger.setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
    frmVlanChanger.setForeground(Color.BLUE);
    frmVlanChanger.setAlwaysOnTop(true);
    frmVlanChanger.setTitle("          Vlan Changer");
    frmVlanChanger.setResizable(false);
    frmVlanChanger.setBackground(Color.BLACK);
    frmVlanChanger.setBounds(100, 100, 243, 111);
    frmVlanChanger.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmVlanChanger.getContentPane().setLayout(null);

    JLabel lblVlan = new JLabel("VLAN -->");
    lblVlan.setHorizontalAlignment(SwingConstants.CENTER);
    lblVlan.setFont(new Font("Tahoma", Font.BOLD, 25));
    lblVlan.setBounds(10, 11, 116, 61);
    frmVlanChanger.getContentPane().add(lblVlan);

    textField = new JTextField();
    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    textField.setHorizontalAlignment(SwingConstants.CENTER);
    textField.setFont(new Font("Tahoma", Font.BOLD, 24));
    textField.setBounds(136, 11, 91, 61);
    frmVlanChanger.getContentPane().add(textField);
    textField.setColumns(2);
}
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
Elie
  • 11
  • 5
  • 1
    Are you asking how to change your PCs IP address using Java APIs? You can't. --- Are you asking how what command to run on a command prompt to change your PCs IP address? Answer depends on OS (Windows, Linux flavor) and has nothing to do with Java. --- Are you asking how to build and run such a command from Java? There are many answers to that question, you just have to search for it. --- Are you asking something else? --- In short, it is unclear what you're really asking, and/or your question is too broad. – Andreas Aug 21 '18 at 17:05
  • 1
    [I downvoted because the question may appear clear to the poster, but it is not for other readers](http://idownvotedbecau.se/unclearquestion) --- FYI: Don't clarify in a comment. **Edit** the question to clarify it. – Andreas Aug 21 '18 at 17:05
  • Its not a duplicate cause I want only 2 numbers in the textfield, for example 00,01,02,11 and not 100, 222,123. – Elie Aug 21 '18 at 20:47
  • Sorry I meant to add more but I hit enter by accident, just wanted to say thanks to @howlger for sticking up for new java user. – Elie Aug 21 '18 at 20:49

0 Answers0