Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.16 KB | None | 0 0
  1. package org.rsbot.gui;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Frame;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.GridBagLayout;
  7. import java.awt.Insets;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseListener;
  12. import java.io.BufferedReader;
  13. import java.io.BufferedWriter;
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.FileReader;
  17. import java.io.FileWriter;
  18. import java.io.IOException;
  19. import java.io.UnsupportedEncodingException;
  20. import java.net.InetAddress;
  21. import java.net.NetworkInterface;
  22. import java.security.MessageDigest;
  23. import java.security.NoSuchAlgorithmException;
  24. import java.util.Map;
  25. import java.util.TreeMap;
  26.  
  27. import javax.naming.NamingException;
  28. import javax.swing.JButton;
  29. import javax.swing.JCheckBox;
  30. import javax.swing.JDialog;
  31. import javax.swing.JLabel;
  32. import javax.swing.JList;
  33. import javax.swing.JOptionPane;
  34. import javax.swing.JPanel;
  35. import javax.swing.JPasswordField;
  36. import javax.swing.JScrollPane;
  37. import javax.swing.JSpinner;
  38. import javax.swing.JTextField;
  39. import javax.swing.ListSelectionModel;
  40. import javax.swing.SpinnerNumberModel;
  41. import javax.swing.SwingConstants;
  42.  
  43. import org.rsbot.util.GlobalConfiguration;
  44.  
  45. /**
  46.  * This will handle the management of Accounts for the Bot.<br>
  47.  * Information will be stored in a file specified by fileName.<br>
  48.  * Format for accounts should be:<br>
  49.  * &nbsp;&nbsp; Name:Password:Member[:Pin] <br>
  50.  * Pin may be omitted if no Pin on Account
  51.  *
  52.  * @author Fusion89k, Jorniman
  53.  */
  54. // The brackets [] are there to denote optional.
  55. public class AccountManager extends JDialog implements ActionListener {
  56.     private static final long serialVersionUID = 6401178388485322197L;
  57.     private JList names;
  58.     private GridBagConstraints c;
  59.     public final static String sepChar = ":", fileName = GlobalConfiguration.Paths.getAccountsFile();
  60.     private final String emptyName = "No Accounts Found";
  61.     static String key;
  62.     private static Map<String, String> accounts;
  63.     private static AccountManager instance;
  64.  
  65.     static {
  66.         try {
  67.             final InetAddress address = InetAddress.getLocalHost();
  68.             final NetworkInterface ni = NetworkInterface.getByInetAddress(address);
  69.             AccountManager.key = new String(ni.getHardwareAddress());
  70.         } catch (final Exception e) {
  71.             AccountManager.key = System.getProperty("user.name") + System.getProperty("user.language");
  72.         }
  73.         try {
  74.             AccountManager.accounts = AccountManager.loadAccounts();
  75.         } catch (final NamingException e) {
  76.             e.printStackTrace();
  77.         }
  78.     }
  79.  
  80.     /**
  81.      * Allows addition of an account
  82.      *
  83.      * @param info
  84.      *            Account info in format of name:pass:member[:pin]
  85.      */
  86.     public static void addAccount(final String info) throws NamingException {
  87.         if (!info.contains(AccountManager.sepChar) || (!info.contains("true") && !info.contains("false")))
  88.             throw new NamingException(info + " is not in the correct format.");
  89.         final String[] parts = info.split(AccountManager.sepChar);
  90.         AccountManager.accounts.put(AccountManager.fixName(parts[0]), AccountManager.crypt(parts[1], true) + AccountManager.sepChar + parts[2]);
  91.         if (parts.length > 3) {
  92.             AccountManager.accounts.put(AccountManager.fixName(parts[0]), AccountManager.crypt(parts[1], true) + AccountManager.sepChar + parts[2] + AccountManager.sepChar + parts[3]);
  93.         }
  94.         AccountManager.saveAccounts();
  95.     }
  96.  
  97.     /**
  98.      * Encrypts/Decrypts a string using a SHA1 hash of (String) key from other
  99.      * users computers
  100.      *
  101.      * @author Jacmob
  102.      */
  103.     private static String crypt(final String start, final boolean en) {
  104.         final String delim = "a";
  105.         if (start == null)
  106.             return null;
  107.         byte[] hashedkey;jake barber4
  108.         byte[] password;houch2
  109.         int i;
  110.         try {
  111.             hashedkey = AccountManager.SHA1(AccountManager.key);
  112.         } catch (final NoSuchAlgorithmException e) {
  113.             e.printStackTrace();
  114.             return start;
  115.         } catch (final UnsupportedEncodingException e) {
  116.             e.printStackTrace();
  117.             return start;
  118.         }
  119.         if (en) {
  120.             String end = "";
  121.             password = start.getBytes();
  122.             for (i = 0; i < hashedkey.length; i++) {
  123.                 if (i < start.length()) {
  124.                     end += hashedkey[i] + password[i] + delim;
  125.                 } else {
  126.                     end += hashedkey[i] + delim;
  127.                 }
  128.             }
  129.             return end.substring(0, end.length() - delim.length());
  130.         } else {
  131.             final String[] temp = start.split(delim);
  132.             password = new byte[temp.length];
  133.             for (i = 0; i < hashedkey.length; i++) {
  134.                 final int temp2 = Integer.parseInt(temp[i]);
  135.                 if (hashedkey[i] == temp2) {
  136.                     break;
  137.                 } else {
  138.                     password[i] = (byte) (temp2 - hashedkey[i]);
  139.                 }
  140.             }
  141.             return new String(password, 0, i);
  142.         }
  143.     }
  144.  
  145.     /**
  146.      * Will convert account files from the old format to the new format
  147.      *
  148.      * Old Format - key=value, key=value,[key=value,]
  149.      *
  150.      * New Format - name:pass[:pin]
  151.      *
  152.      * @return A mapping of name password/pin pairs
  153.      */
  154.     private static Map<String, String> fixAccounts() throws NamingException {
  155.         String endResult = "";
  156.         try {
  157.             final BufferedReader in = new BufferedReader(new FileReader(AccountManager.fileName));
  158.             String temp;
  159.             while ((temp = in.readLine()) != null) {
  160.                 if (temp.contains("USERNAME")) {
  161.                     String pass = null, name = null, pin = null;
  162.                     // Old Format key=value,
  163.                     for (final String part : temp.split(",")) {
  164.                         // Splits Keys and Values
  165.                         final String[] pieces = part.split("=", 2);
  166.                         if (pieces[0].equals("PASSWORD")) {
  167.                             pass = AccountManager.crypt(pieces[1], true);
  168.                         } else if (pieces[0].equals("USERNAME")) {
  169.                             name = pieces[1];
  170.                         } else if (pieces[0].equals("PIN")) {
  171.                             pin = pieces[1];
  172.                         }
  173.                     }
  174.                     try {
  175.                         // Converts to new format name:pass:member[:pin]
  176.                         endResult += name + AccountManager.sepChar + pass + AccountManager.sepChar + "false" + (pin != null ? AccountManager.sepChar + pin : "");
  177.                     } catch (final NullPointerException e) {
  178.                         // Will only happen if one of the keys is not found
  179.                         try {
  180.                             return AccountManager.loadAccounts();
  181.                         } catch (final NamingException e1) {
  182.                             e1.printStackTrace();
  183.                         }
  184.                     }
  185.                     endResult += System.getProperty("line.separator");
  186.                 } else {
  187.                                         String pass = null, name = null, pin = null;
  188.                                         final String[] parts = temp.split(AccountManager.sepChar, 3);
  189.                                         pass = AccountManager.crypt(parts[1], true);
  190.                                         name = parts[0];
  191.                                         if (parts.length > 2) { //username:password:pin
  192.                                             pin = parts[2];
  193.                                         }
  194.                                         try {
  195.                         // Converts to new format name:pass:member[:pin]
  196.                         endResult += name + AccountManager.sepChar + pass + AccountManager.sepChar + "false" + (pin != null ? AccountManager.sepChar + pin : "");
  197.                     } catch (final NullPointerException e) {
  198.                         // Will only happen if one of the keys is not found
  199.                         try {
  200.                             return AccountManager.loadAccounts();
  201.                         } catch (final NamingException e1) {
  202.                             e1.printStackTrace();
  203.                         }
  204.                     }
  205.                     endResult += System.getProperty("line.separator");
  206.                                 }
  207.             }
  208.             in.close();
  209.             // Writes the new format to the file
  210.             final BufferedWriter out = new BufferedWriter(new FileWriter(AccountManager.fileName));
  211.             out.append(endResult);
  212.             out.close();
  213.         } catch (final FileNotFoundException e) {
  214.             e.printStackTrace();
  215.         } catch (final IOException e) {
  216.             e.printStackTrace();
  217.         }
  218.         // Reload the accounts
  219.         return AccountManager.loadAccounts();
  220.     }
  221.  
  222.     /**
  223.      * Capitalizes the first character and replaces spaces with underscores
  224.      * Purely esthetic
  225.      *
  226.      * @param name
  227.      *            Name to fix
  228.      * @return Fixed name
  229.      */
  230.     private static String fixName(String name) {
  231.         if (name.charAt(0) > 91) {
  232.             name = (char) (name.charAt(0) - 32) + name.substring(1);
  233.         }
  234.         while (name.contains(" ")) {
  235.             name = name.substring(0, name.indexOf(" ")) + "_" + name.substring(name.indexOf(" ") + 1);
  236.         }
  237.         return name;
  238.     }
  239.  
  240.     /**
  241.      * Access the list of names for loaded accounts
  242.      *
  243.      * @return Array of the Names
  244.      */
  245.     public static String[] getAccountNames() {
  246.         return AccountManager.accounts.keySet().toArray(new String[0]);
  247.     }
  248.  
  249.     /**
  250.      * Enables AccountManager to be a Singleton
  251.      *
  252.      * @return The instance of the AccountManager
  253.      */
  254.     public static AccountManager getInstance() {
  255.         if (AccountManager.instance == null)
  256.             return AccountManager.instance = new AccountManager();
  257.         else
  258.             return AccountManager.instance;
  259.     }
  260.  
  261.     /**
  262.      * Access the password of the given account
  263.      *
  264.      * @param name
  265.      *            Name of account to access password of
  266.      * @return Unencrypted Password
  267.      */
  268.     public static String getPassword(final String name) {
  269.         String password = AccountManager.accounts.get(name);
  270.         if ((password != null) && password.contains(AccountManager.sepChar)) {
  271.             password = password.split(AccountManager.sepChar, 3)[0];
  272.         }
  273.         return AccountManager.crypt(password, false);
  274.     }
  275.  
  276.     /**
  277.      * Access the pin for the given account
  278.      *
  279.      * @param name
  280.      *            Name of the account
  281.      * @return Pin or -1 if no pin
  282.      */
  283.     public static String getPin(final String name) {
  284.         final String all = AccountManager.accounts.get(name);
  285.         if (all.contains(AccountManager.sepChar))
  286.             try {
  287.                             return all.split(AccountManager.sepChar, 3)[2];
  288.                         } catch (Exception e) {
  289.                             return "-1";
  290.                         }
  291.         return "-1";
  292.     }
  293.  
  294.         /**
  295.          * Whether the account is member or not
  296.          *
  297.          * @param name
  298.          *            Name of the account
  299.          * @return true if account is a member, false if not
  300.          */
  301.         public static boolean isMember(final String name) {
  302.                 final String all = AccountManager.accounts.get(name);
  303.                 if (all.contains(AccountManager.sepChar))
  304.                     return (all.split(AccountManager.sepChar, 3)[1].equalsIgnoreCase("true") ? true : false);
  305.                 return false;
  306.         }
  307.  
  308.     /**
  309.      * Loads the accounts from the file
  310.      */
  311.     private static Map<String, String> loadAccounts() throws NamingException {
  312.         final TreeMap<String, String> accounts = new TreeMap<String, String>();
  313.         final File accountFile = new File(AccountManager.fileName);
  314.         if (accountFile.exists()) {
  315.             try {
  316.                 final BufferedReader in = new BufferedReader(new FileReader(accountFile));
  317.                 String temp;// Used to store the lines from the file
  318.                 while ((temp = in.readLine()) != null) {
  319.                     if (temp.isEmpty()) {
  320.                         continue;
  321.                     }
  322.                     // Means that it is in old format (USERNAME OR username:pw[:pin])
  323.                     if (temp.contains("USERNAME") || (!temp.contains("false") && !temp.contains("true")))
  324.                         return AccountManager.fixAccounts();
  325.                     // If for some reason the format is not as expected
  326.                     if (!temp.contains(AccountManager.sepChar))
  327.                         throw new NamingException("Invalid Storage of: " + temp);
  328.                     final String[] parts = temp.split(AccountManager.sepChar, 3);
  329.                     for (final String s : parts) {
  330.                         if (s.isEmpty())
  331.                             throw new NamingException("Invalid Storage of: " + temp);
  332.                     }
  333.                     // Formats the name
  334.                     parts[0] = AccountManager.fixName(parts[0]);
  335.                     // Checks Pin Validity
  336.                     try {
  337.                         if ((parts.length > 3) && ((parts[3].length() != 4) || (Integer.parseInt(parts[3]) >= 0)))
  338.                             throw new NamingException("Invalid Pin: " + parts[3] + " On Account: " + parts[0]);
  339.                     } catch (final NumberFormatException e) {
  340.                         throw new NamingException("Invalid Pin: " + parts[3] + " On Account: " + parts[0]);
  341.                     }
  342.                     accounts.put(parts[0], parts[1] + AccountManager.sepChar + parts[2]);
  343.                 }
  344.                 in.close();
  345.             } catch (final FileNotFoundException e) {// From File Loading
  346.                 e.printStackTrace();
  347.             } catch (final IOException e) {// From Reader traversal
  348.                 e.printStackTrace();
  349.             }
  350.         }
  351.         return accounts;
  352.     }
  353.  
  354.     /**
  355.      * Writes the accounts to the file
  356.      */
  357.     private static void saveAccounts() {
  358.         final File accountFile = new File(AccountManager.fileName);
  359.         try {
  360.             final BufferedWriter out = new BufferedWriter(new FileWriter(accountFile));
  361.             for (final String s : AccountManager.accounts.keySet()) {
  362.                 if (AccountManager.accounts.get(s).isEmpty()) {
  363.                     continue;
  364.                 }
  365.                 out.append(s + AccountManager.sepChar + AccountManager.accounts.get(s));
  366.                 out.newLine();
  367.             }
  368.             out.close();
  369.         } catch (final IOException e) {
  370.             e.printStackTrace();
  371.         }
  372.     }
  373.  
  374.     /**
  375.      * Returns SHA1 hash of a String.
  376.      */
  377.     private static byte[] SHA1(final String in) throws NoSuchAlgorithmException, UnsupportedEncodingException {
  378.         MessageDigest md;
  379.         md = MessageDigest.getInstance("SHA-1");
  380.         byte[] sha1hash = new byte[40];
  381.         md.update(in.getBytes("iso-8859-1"), 0, in.length());
  382.         sha1hash = md.digest();
  383.         return sha1hash;
  384.     }
  385.  
  386.     private AccountManager() {
  387.         super(Frame.getFrames()[0], "Account Manager", true);
  388.         if (AccountManager.accounts.size() < 1) {
  389.             AccountManager.accounts.put(emptyName, "");
  390.         }
  391.     }
  392.  
  393.     /**
  394.      * Controls all of the button and checkBox actions of the program
  395.      */
  396.     public void actionPerformed(final ActionEvent event) {
  397.         JDialog parentFrame;
  398.         Component comp = ((Component) event.getSource()).getParent();
  399.         while (!(comp instanceof JDialog)) {
  400.             comp = comp.getParent();
  401.         }
  402.         parentFrame = (JDialog) comp;
  403.         if (event.getSource() instanceof JButton) {
  404.             switch (((JButton) event.getSource()).getText().charAt(0)) {
  405.                 case 'A':// Add Accounts
  406.                     if (event.getActionCommand().contains("dd")) {
  407.                         addAccount();
  408.                     } else {// Update the List with the new Account
  409.                         final JPanel pane = (JPanel) ((JButton) event.getSource()).getParent();
  410.                         String name = null, pass = null, pin = "", member = "false";
  411.                         for (final Component c : pane.getComponents()) {
  412.                             if (c instanceof JPasswordField) {
  413.                                 pass = ((JTextField) c).getText();
  414.                             } else if (c instanceof JTextField) {
  415.                                 name = ((JTextField) c).getText();
  416.                             } else if (c instanceof JSpinner) {
  417.                                 pin += ((JSpinner) c).getValue();
  418.                             } else if (c instanceof JCheckBox) {
  419.                                                                 member = (((JCheckBox) c).isSelected() ? "true" : "false");
  420.                                                         }
  421.                         }
  422.                         if (name.isEmpty() || pass.isEmpty()) {
  423.                             JOptionPane.showMessageDialog(null, "Empty Fields");
  424.                             return;
  425.                         }
  426.                         AccountManager.accounts.put(AccountManager.fixName(name), AccountManager.crypt(pass, true) + AccountManager.sepChar + member + (pin.isEmpty() ? "" : AccountManager.sepChar + pin));
  427.                         refreshList();
  428.                         names.validate();
  429.                         parentFrame.dispose();
  430.                     }
  431.                     break;
  432.                 case 'D':// Delete Accounts
  433.                     if (AccountManager.accounts.get(names.getSelectedValue()).isEmpty())
  434.                         return;
  435.                     final int yes = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete " + names.getSelectedValue() + "?", "Delete", JOptionPane.YES_NO_OPTION);
  436.                     if (yes == 0) {
  437.                         AccountManager.accounts.remove(names.getSelectedValue());
  438.                         refreshList();
  439.                         names.validate();
  440.                     }
  441.                     break;
  442.                 case 'S':// Save Accounts
  443.                     AccountManager.saveAccounts();
  444.                     break;
  445.             }
  446.         } else if (event.getSource() instanceof JCheckBox) {
  447.             // CheckBox for Pins add and remove JSpinners
  448.             final JCheckBox jcb = (JCheckBox) event.getSource();
  449.             if (jcb.isSelected()) {
  450.                 // Add Spinners
  451.                 c = new GridBagConstraints();
  452.                 c.gridy = 2;
  453.                 c.gridx = 1;
  454.                 c.weightx = 1;
  455.                 c.insets = new Insets(0, 0, 5, 5);
  456.                 final JSpinner[] spins = new JSpinner[4];
  457.                 for (int i = 0; i < spins.length; i++) {
  458.                     spins[i] = new JSpinner(new SpinnerNumberModel(0, 0, 9, 1));
  459.                     jcb.getParent().add(spins[i], c);
  460.                     c.gridx = GridBagConstraints.RELATIVE;
  461.                 }
  462.                 parentFrame.pack();
  463.             } else {
  464.                 // Remove Spinners
  465.                 for (final Component c : jcb.getParent().getComponents()) {
  466.                     if (c instanceof JSpinner) {
  467.                         jcb.getParent().remove(c);
  468.                     }
  469.                 }
  470.                 parentFrame.validate();
  471.             }
  472.         }
  473.     }
  474.  
  475.     /**
  476.      * Gets the necessary information from the user to make a new account
  477.      */
  478.     private void addAccount() {
  479.         final JDialog addFrame = new JDialog(this, "New", true);
  480.         final JPanel pane = new JPanel(new GridBagLayout());
  481.         c = new GridBagConstraints();
  482.         final JTextField userName = new JTextField(8);
  483.         final JPasswordField userPass = new JPasswordField(8);
  484.         userPass.setEchoChar('*');
  485.         c.gridwidth = 2;
  486.         c.fill = GridBagConstraints.HORIZONTAL;
  487.         c.insets = new Insets(5, 5, 0, 7);
  488.         pane.add(new JLabel("User Name: ", SwingConstants.CENTER), c);
  489.         c.gridx = GridBagConstraints.RELATIVE;
  490.         c.gridwidth = 3;
  491.         c.insets = new Insets(5, 0, 0, 5);
  492.         pane.add(userName, c);
  493.         c.insets = new Insets(5, 5, 5, 7);
  494.         c.gridwidth = 2;
  495.         c.gridx = 0;
  496.         c.gridy = 1;
  497.         pane.add(new JLabel("Password: ", SwingConstants.CENTER), c);
  498.         c.insets = new Insets(5, 0, 5, 5);
  499.         c.gridwidth = 3;
  500.         c.gridx = GridBagConstraints.RELATIVE;
  501.         pane.add(userPass, c);
  502.         final JCheckBox pinCheck = new JCheckBox("Pin");
  503.         pinCheck.addActionListener(this);
  504.         c = new GridBagConstraints();
  505.         c.gridy = 2;
  506.         c.insets = new Insets(0, 5, 5, 5);
  507.         pane.add(pinCheck, c);
  508.                 final JCheckBox memberCheck = new JCheckBox("Member");
  509.                 c = new GridBagConstraints();
  510.                 c.gridy = 3;
  511.                 c.insets = new Insets(0, 5, 5, 5);
  512.                 pane.add(memberCheck, c);
  513.  
  514.         final JButton save = new JButton("Add");
  515.         save.setActionCommand("Update");
  516.         save.addActionListener(this);
  517.         c.gridy = 4;
  518.         c.gridwidth = GridBagConstraints.REMAINDER;
  519.         pane.add(save, c);
  520.  
  521.         addFrame.setResizable(false);
  522.         addFrame.add(pane);
  523.         addFrame.pack();
  524.         addFrame.setLocationRelativeTo(this);
  525.         addFrame.setVisible(true);
  526.     }
  527.  
  528.     /**
  529.      * Resets the Model for the List to reflect changes made to the accounts Map
  530.      */
  531.     private void refreshList() {
  532.         if ((AccountManager.accounts.keySet().size() > 1) && AccountManager.accounts.keySet().contains(emptyName)) {
  533.             AccountManager.accounts.remove(emptyName);
  534.         } else if (AccountManager.accounts.keySet().size() < 1) {
  535.             AccountManager.accounts.put(emptyName, "");
  536.         }
  537.         names.setListData(AccountManager.accounts.keySet().toArray(new String[0]));
  538.         names.getParent().validate();
  539.     }
  540.  
  541.     /**
  542.      * Creates and Displays the main GUI
  543.      *
  544.      * This GUI has the list and the main buttons
  545.      */
  546.     public void showGUI() {
  547.         getContentPane().removeAll();
  548.         c = new GridBagConstraints();
  549.         // Main Panel with everything
  550.         final JPanel pane = new JPanel(new GridBagLayout());
  551.         // Makes the List
  552.         names = new JList(AccountManager.accounts.keySet().toArray(new String[0]));
  553.         // Only one selection at a time
  554.         names.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  555.         // Will display the password and pin when double clicked or right
  556.         // clicked
  557.         names.addMouseListener(new MouseListener() {
  558.             public void mouseClicked(final MouseEvent click) {
  559.                 if ((click.getClickCount() > 1) || (click.getButton() == MouseEvent.BUTTON3)) {
  560.                     final String clicked = AccountManager.accounts.get(((JList) click.getComponent()).getSelectedValue());
  561.                     if (clicked.isEmpty())
  562.                         return;
  563.                     String pass = clicked.contains(AccountManager.sepChar) ? clicked.substring(0, clicked.indexOf(AccountManager.sepChar)) : clicked;
  564.                     pass = AccountManager.crypt(pass, false);
  565.                     String info = "Password: " + pass;
  566.                     if (clicked.contains(AccountManager.sepChar)) {
  567.                                                 info += "\nMember: " + clicked.split(AccountManager.sepChar)[1];
  568.                         try {
  569.                                                     info += "\nPin: " + clicked.split(AccountManager.sepChar)[2];
  570.                                                 } catch (Exception e) {
  571.                                                 }
  572.                     }
  573.                     JOptionPane.showMessageDialog(null, info);
  574.                 }
  575.             }
  576.  
  577.             public void mouseEntered(final MouseEvent click) {
  578.             }
  579.  
  580.             public void mouseExited(final MouseEvent click) {
  581.             }
  582.  
  583.             public void mousePressed(final MouseEvent click) {
  584.             }
  585.  
  586.             public void mouseReleased(final MouseEvent click) {
  587.             }
  588.         });
  589.  
  590.         // Enables scrolling through the List
  591.         final JScrollPane scroll = new JScrollPane(names);
  592.  
  593.         final JButton add = new JButton("Add");// Button 1
  594.         final JButton del = new JButton("Delete");// Button 2
  595.         final JButton save = new JButton("Save");// Button 3
  596.  
  597.         add.addActionListener(this);
  598.         del.addActionListener(this);
  599.         save.addActionListener(this);
  600.         // Positions Everything Correctly on the panel
  601.         c.gridy = 0;
  602.         c.gridwidth = GridBagConstraints.REMAINDER;
  603.         c.fill = GridBagConstraints.BOTH;
  604.         pane.add(scroll, c);
  605.         c.gridwidth = 1;
  606.         c.gridy = 1;
  607.         c.gridx = 0;
  608.         c.fill = GridBagConstraints.NONE;
  609.         c.insets = new Insets(5, 5, 5, 5);
  610.         c.anchor = GridBagConstraints.CENTER;
  611.         pane.add(add, c);
  612.         c.gridx = GridBagConstraints.RELATIVE;
  613.         pane.add(del, c);
  614.         pane.add(save, c);
  615.         names.setSelectedIndex(0);
  616.         add(pane);
  617.         pack();
  618.         setLocationRelativeTo(getOwner());
  619.         setVisible(true);
  620.         setResizable(false);
  621.     }
  622. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement