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:
- How can I have the user input only a 2 digit number into jtextfield (no letters)?
- 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);
}
}