简体   繁体   English

如何在jtextarea中选择一行

[英]How to select a row in jtextarea

I need to select a row in the jtextarea我需要在jtextarea中选择一行

this is my first project which I make a pos system using java,what I need to do here is that when there is a list of products in the jtextarea which are added to cart by the customer and suddenly customer wants to remove some items from the cart then my pos system should allow to select the item from the row and remove it and remove it from the mysql table too这是我的第一个项目,我使用 java 制作了一个 pos 系统,我在这里需要做的是,当 jtextarea 中有一个产品列表被客户添加到购物车时,突然客户想要从中删除一些项目购物车然后我的 pos 系统应该允许从行中选择项目并将其删除并将其也从 mysql 表中删除

textArea1.setText(item1 + "\n" + item2 + "\n" + item3 + "\n");

when there is a list of products in the jtextarea which are added to cart by the customer and suddenly customer whats to remove some items then my pos system should allow to select the item from the row and remove it and remove it from the mysql table too当 jtextarea 中有一个产品列表被客户添加到购物车时,突然客户要删除一些项目然后我的 pos 系统应该允许从行中选择项目并将其删除并将其也从 mysql 表中删除

and if you think there's a better object that jtextarea then please suggest it too.如果您认为有比 jtextarea 更好的对象,那么也请提出建议。

thanks谢谢

Based on one of @MadProgrammer answers, (sadly i can't get the link ) i was able to produce this基于@MadProgrammer 的回答之一,(遗憾的是我无法获得链接)我能够制作这个


import javax.swing.text.BadLocationException;
import javax.swing.text.Utilities;

public class TextLineSelectionTest extends javax.swing.JFrame {

    public TextLineSelectionTest() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextArea1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTextArea1);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>                        

    private void jTextArea1MouseClicked(java.awt.event.MouseEvent evt) {                                        
        int pos = jTextArea1.getCaretPosition();
        try {

            int start =Utilities.getRowStart(jTextArea1, pos);
            int end =Utilities.getRowEnd(jTextArea1, pos);
            jTextArea1.setSelectionStart(start);
            jTextArea1.setSelectionEnd(end);
        } catch (BadLocationException e) {
            //something is messed up
            e.printStackTrace();
        }
    }                                       

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TextLineSelectionTest().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration                   
}

by using the default Utilities .通过使用默认的Utilities

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM