New Posts  All Forums:Forum Nav:

jTextArea Problems?

post #1 of 3
Thread Starter 
I am finishing up my word scrambler, however I have one simple problem and that is with the JtextArea.
No matter how much I type it will continuously keep scrolling to the right unless I press enter. How can I make it so that it does not scroll horizontally, and once it reaches the right border it will automatically keep going on the next line.

I'm using netBeans's GUI builder, so if this is a simple problem I am sorry.

And for those of you who want to use my word scrambler, here is the code :]



Code:

/*
 * ScrambleWordsView.java
 */

package scramblewords;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 * The application's main frame.
 */
public class ScrambleWordsView extends FrameView {

    public ScrambleWordsView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = ScrambleWordsApp.getApplication().getMainFrame();
            aboutBox = new ScrambleWordsAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        ScrambleWordsApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        mainPanel.setName("mainPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(scramblewords.ScrambleWordsApp.class).getContext().getResourceMap(ScrambleWordsView.class);
        jTextArea1.setText(resourceMap.getString("jTextArea1.text")); // NOI18N
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jTextArea2.setName("jTextArea2"); // NOI18N
        jScrollPane2.setViewportView(jTextArea2);

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 382, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(203, 203, 203))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE))
                        .addContainerGap())))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        fileMenu.add(jMenuItem1);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(scramblewords.ScrambleWordsApp.class).getContext().getActionMap(ScrambleWordsView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_K, java.awt.event.InputEvent.CTRL_MASK));
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 868, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addGap(389, 389, 389)
                .addComponent(jLabel1)
                .addContainerGap(383, Short.MAX_VALUE))
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel))
                .addGap(11, 11, 11))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        
        String[] result = jTextArea1.getText().split("\\\\s");
        
        jTextArea2.setText("");
        
        for ( int i = 0; i < result.length; i++) {
            ArrayList<Character> chars = new ArrayList<Character>(result[i].length());
            
                for ( char c : result[i].toCharArray() ) {
                    chars.add(c);
                }
                
                char a = chars.get(0);
                
                char x = chars.get(chars.size()-1);
                
                char b = 0;
                
                String xb = null;
                                
                if (!Character.isLetter(x)) {
                    x = chars.get(chars.size()-1);
                    String s1 = Character.toString(x);
                    
                    b = chars.get(chars.size()-2);
                    String s2 = Character.toString(b);
                    
                    xb = (s2 + s1);
                    
                    chars.remove(0);
                    chars.remove(chars.size()-1);
                    chars.remove(chars.size()-1);
                }
                
                else if (Character.isLetter(x)) {
                    chars.remove(0);
                    chars.remove(chars.size()-1);
                }
                
                if (xb == null) {
                    System.out.println(chars + " First Letter: " + a + " " + "Last Letter: " + x);
                }
                
                else if (xb != null)    {
                    System.out.println(chars + " First Letter: " + a + " " + "Last Letter: " + xb);
                }
                            
                if (chars.size() == 2)  {
                    System.out.println("Size is 2, lets reverse this");
                    Collections.reverse(chars);
                    System.out.println("New combination" + chars);
                }
                else if (chars.size() != 2) {
                    Collections.shuffle(chars);
                }               
                
                char[] shuffled = new char[chars.size()];
                    for ( int j = 0; j < shuffled.length; j++ ) {
                        shuffled[j] = chars.get(j);
                }
                    
            String shuffledWord = new String(shuffled);
            
            if (xb == null) {
                jTextArea2.append(a + shuffledWord + x + " ");
            }
            
            else if (xb != null)    {
                jTextArea2.append(a + shuffledWord + xb + " ");
                xb = null;
            }
        }
    }                                     

        private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
            try {
                Writer output = null;
                String text = jTextArea2.getText();
                File file = new File("Scramble Words.txt");
                output = new BufferedWriter(new FileWriter(file));
                output.write(text);
                output.close();
                System.out.println("Your file has been saved"); 
    }
            catch (IOException ex) {
                Logger.getLogger(ScrambleWordsView.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}


All the actual work is under the jButton1.
Supra
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-2600K 4.0GHz @ 1.160V MSI P67A-GD65 (B3) XFX Radeon HD 6950 2GB Unlocked @ 950/1450 XFX Radeon HD 6950 2GB Unlocked @ 950/1450 
RAMHard DriveHard DriveHard Drive
Mushkin Enhanced Silverline 8GB (2 x 4GB) Corsair Nova SSD 32GB 1TB Seagate 6.0GB/s SATA 1TB Seagate 6.0GB/s SATA 
Optical DriveCoolingOSMonitor
Lite-On DVD Burner Stock Windows 7 Ultimate x64 Acer S231HL LED Monitor 
MonitorMonitorKeyboardPower
Acer S231HL LED Monitor Acer A23IH LCD Monitor eMachines Generic Antec 750W Modular 
CaseMouseMouse Pad
Antec 1200 Gigabyte Ghost 6000 dpi Desk 
  hide details  
Reply
Supra
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-2600K 4.0GHz @ 1.160V MSI P67A-GD65 (B3) XFX Radeon HD 6950 2GB Unlocked @ 950/1450 XFX Radeon HD 6950 2GB Unlocked @ 950/1450 
RAMHard DriveHard DriveHard Drive
Mushkin Enhanced Silverline 8GB (2 x 4GB) Corsair Nova SSD 32GB 1TB Seagate 6.0GB/s SATA 1TB Seagate 6.0GB/s SATA 
Optical DriveCoolingOSMonitor
Lite-On DVD Burner Stock Windows 7 Ultimate x64 Acer S231HL LED Monitor 
MonitorMonitorKeyboardPower
Acer S231HL LED Monitor Acer A23IH LCD Monitor eMachines Generic Antec 750W Modular 
CaseMouseMouse Pad
Antec 1200 Gigabyte Ghost 6000 dpi Desk 
  hide details  
Reply
post #2 of 3
jTextArea1.setLineWrap(true);
jTextArea1.setWrapStyleWord(true);
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
post #3 of 3
Thread Starter 
+2
Supra
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-2600K 4.0GHz @ 1.160V MSI P67A-GD65 (B3) XFX Radeon HD 6950 2GB Unlocked @ 950/1450 XFX Radeon HD 6950 2GB Unlocked @ 950/1450 
RAMHard DriveHard DriveHard Drive
Mushkin Enhanced Silverline 8GB (2 x 4GB) Corsair Nova SSD 32GB 1TB Seagate 6.0GB/s SATA 1TB Seagate 6.0GB/s SATA 
Optical DriveCoolingOSMonitor
Lite-On DVD Burner Stock Windows 7 Ultimate x64 Acer S231HL LED Monitor 
MonitorMonitorKeyboardPower
Acer S231HL LED Monitor Acer A23IH LCD Monitor eMachines Generic Antec 750W Modular 
CaseMouseMouse Pad
Antec 1200 Gigabyte Ghost 6000 dpi Desk 
  hide details  
Reply
Supra
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-2600K 4.0GHz @ 1.160V MSI P67A-GD65 (B3) XFX Radeon HD 6950 2GB Unlocked @ 950/1450 XFX Radeon HD 6950 2GB Unlocked @ 950/1450 
RAMHard DriveHard DriveHard Drive
Mushkin Enhanced Silverline 8GB (2 x 4GB) Corsair Nova SSD 32GB 1TB Seagate 6.0GB/s SATA 1TB Seagate 6.0GB/s SATA 
Optical DriveCoolingOSMonitor
Lite-On DVD Burner Stock Windows 7 Ultimate x64 Acer S231HL LED Monitor 
MonitorMonitorKeyboardPower
Acer S231HL LED Monitor Acer A23IH LCD Monitor eMachines Generic Antec 750W Modular 
CaseMouseMouse Pad
Antec 1200 Gigabyte Ghost 6000 dpi Desk 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming