rangga_hrdme

sort_text_swing

Oct 28th, 2022
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.73 KB | Source Code | 0 0
  1. /*
  2.  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  3.  * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
  4.  */
  5. package onlyFrame;
  6.  
  7. import java.util.Arrays;
  8. import java.util.Collections;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11.  
  12. /**
  13.  *
  14.  * @author User
  15.  */
  16. public class MainForm extends javax.swing.JFrame {
  17.  
  18.     /**
  19.      * Creates new form MainForm
  20.      */
  21.     public MainForm() {
  22.         initComponents();
  23.     }
  24.  
  25.     /**
  26.      * This method is called from within the constructor to initialize the form.
  27.      * WARNING: Do NOT modify this code. The content of this method is always
  28.      * regenerated by the Form Editor.
  29.      */
  30.     @SuppressWarnings("unchecked")
  31.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  32.     private void initComponents() {
  33.  
  34.         jPanel1 = new javax.swing.JPanel();
  35.         jScrollPane1 = new javax.swing.JScrollPane();
  36.         txtEntry = new javax.swing.JTextArea();
  37.         jScrollPane2 = new javax.swing.JScrollPane();
  38.         txtResult = new javax.swing.JTextArea();
  39.         btnProses = new javax.swing.JButton();
  40.  
  41.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  42.  
  43.         txtEntry.setColumns(20);
  44.         txtEntry.setFont(new java.awt.Font("DialogInput", 0, 18)); // NOI18N
  45.         txtEntry.setLineWrap(true);
  46.         txtEntry.setRows(5);
  47.         jScrollPane1.setViewportView(txtEntry);
  48.  
  49.         txtResult.setColumns(20);
  50.         txtResult.setFont(new java.awt.Font("DialogInput", 0, 18)); // NOI18N
  51.         txtResult.setLineWrap(true);
  52.         txtResult.setRows(5);
  53.         jScrollPane2.setViewportView(txtResult);
  54.  
  55.         btnProses.setText("Proses");
  56.         btnProses.addActionListener(new java.awt.event.ActionListener() {
  57.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  58.                 btnProsesActionPerformed(evt);
  59.             }
  60.         });
  61.  
  62.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  63.         jPanel1.setLayout(jPanel1Layout);
  64.         jPanel1Layout.setHorizontalGroup(
  65.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  66.             .addComponent(jScrollPane1)
  67.             .addComponent(jScrollPane2)
  68.             .addGroup(jPanel1Layout.createSequentialGroup()
  69.                 .addGap(233, 233, 233)
  70.                 .addComponent(btnProses)
  71.                 .addContainerGap(353, Short.MAX_VALUE))
  72.         );
  73.         jPanel1Layout.setVerticalGroup(
  74.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  75.             .addGroup(jPanel1Layout.createSequentialGroup()
  76.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
  77.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  78.                 .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
  79.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  80.                 .addComponent(btnProses)
  81.                 .addContainerGap())
  82.         );
  83.  
  84.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  85.         getContentPane().setLayout(layout);
  86.         layout.setHorizontalGroup(
  87.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  88.             .addGroup(layout.createSequentialGroup()
  89.                 .addContainerGap()
  90.                 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  91.                 .addContainerGap())
  92.         );
  93.         layout.setVerticalGroup(
  94.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  95.             .addGroup(layout.createSequentialGroup()
  96.                 .addContainerGap()
  97.                 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  98.                 .addContainerGap())
  99.         );
  100.  
  101.         pack();
  102.         setLocationRelativeTo(null);
  103.     }// </editor-fold>                        
  104.  
  105.     private void btnProsesActionPerformed(java.awt.event.ActionEvent evt) {                                          
  106.         // TODO add your handling code here:
  107.         Map<String, Integer> duplicatesV3 = countDuplicateCharactersVCP1(txtEntry.getText().replaceAll("\\s", "").toUpperCase());
  108.        
  109. //        Object[] arrString = duplicatesV3.entrySet().toArray();
  110. //        Arrays.sort(arrString);
  111. //        System.out.println(Arrays.toString(arrString));
  112.        
  113.         txtResult.setText(Arrays.toString(duplicatesV3.entrySet().toArray()));
  114.  
  115.     }                                        
  116.    
  117.     public static Map<String, Integer> countDuplicateCharactersVCP1(String word) {
  118.        
  119.         if (word == null || word.isEmpty()) {
  120.             // or throw IllegalArgumentException            
  121.             return Collections.emptyMap();
  122.         }
  123.        
  124.         Map<String, Integer> result = new HashMap<>();
  125.        
  126.         for (int x = 0; x < word.length(); x++) {
  127.            
  128.             int cp = word.codePointAt(x);
  129.             String ch = String.valueOf(Character.toChars(cp));
  130.             if (Character.charCount(cp) == 2) { // 2 means a surrogate pair
  131.                 x++;
  132.             }
  133.            
  134.             result.compute(ch, (k, v) -> (v == null) ? 1 : ++v);
  135.         }
  136.        
  137.         return result;
  138.     }
  139.  
  140.     /**
  141.      * @param args the command line arguments
  142.      */
  143.     public static void main(String args[]) {
  144.         /* Set the Nimbus look and feel */
  145.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  146.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  147.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  148.          */
  149.         try {
  150.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  151.                 if ("Nimbus".equals(info.getName())) {
  152.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  153.                     break;
  154.                 }
  155.             }
  156.         } catch (ClassNotFoundException ex) {
  157.             java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  158.         } catch (InstantiationException ex) {
  159.             java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  160.         } catch (IllegalAccessException ex) {
  161.             java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  162.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  163.             java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  164.         }
  165.         //</editor-fold>
  166.  
  167.         /* Create and display the form */
  168.         java.awt.EventQueue.invokeLater(new Runnable() {
  169.             public void run() {
  170.                 new MainForm().setVisible(true);
  171.             }
  172.         });
  173.     }
  174.  
  175.     // Variables declaration - do not modify                    
  176.     private javax.swing.JButton btnProses;
  177.     private javax.swing.JPanel jPanel1;
  178.     private javax.swing.JScrollPane jScrollPane1;
  179.     private javax.swing.JScrollPane jScrollPane2;
  180.     private javax.swing.JTextArea txtEntry;
  181.     private javax.swing.JTextArea txtResult;
  182.     // End of variables declaration                  
  183. }
  184.  
Tags: Java
Advertisement
Add Comment
Please, Sign In to add comment