简体   繁体   English

使用Java在文本区域中并排显示字符串数组中的对象

[英]Show objects in string array side by side in a text Area using java

I wanted to read specific columns written in my text file and show this specific columns onto my text Area side by side. 我想阅读写在文本文件中的特定列,并将这些特定列并排显示在我的文本区域中。 I manage to read the desired columns and show them to my text area using the codes below: 我设法读取所需的列,并使用以下代码将其显示在我的文本区域:

try
    {
        ArrayList<String> totalResult1 = new ArrayList<String>();
        ArrayList<String> totalResult2 = new ArrayList<String>();
        [enter image description here][1]ArrayList<String> totalResult3 = new ArrayList<String>();
                try
                {
                    FileInputStream fStream = new FileInputStream("hubo\\" + "table" + ".txt");
                    DataInputStream in = new DataInputStream(fStream);
                    BufferedReader br = new BufferedReader (new InputStreamReader(in));
                    String strLine;

                    while((strLine = br.readLine()) != null)
                    {
                        strLine = strLine.trim();

                            if((strLine.length()!=0) && (strLine.charAt(0) !='#')) 
                            {
                                String[] employee = strLine.split("\\s+");
                                totalResult1.add(employee[0]);
                                totalResult2.add(employee[1]);
                                totalResult3.add(employee[2]);
                            }   

                    }

                    for(String s1 : totalResult1)
                    {   
                        showArea.append(s1.toString() + "\n");                  
                    }   

                    for(String s2 : totalResult2)
                    {   
                        showArea.append("\t" + "\t" + s2.toString() + "\n");                    
                    }

                    in.close();
                    }           
                    catch (Exception e1)
                    {

                    }                           

            }
            catch(Exception e1)
            {

            }   

This is my result 这是我的结果

   Alex Santos
   Troy Smith
   John Love

                Married
                Single
                Married

My desired results are this: 我想要的结果是这样的:

   Alex Santos   Married
   Troy Smith    Single
   John Love     Married

I want to show to my text Area both of my columns side by side, can anyone point me to the right direction. 我想并排显示两个区域的文本区域,任何人都可以将我指向正确的方向。

Your solution is close, but not quite. 您的解决方案很接近,但不太完全。 When you append the employe names from totalResult1 you go to a new line each time. 当您从totalResult1追加totalResult1姓名时, totalResult1都转到新行。 So when you add values from the second list, you are already bellow the names. 因此,当您从第二个列表中添加值时,您已经在使用名称了。 To create a table like display, you would need to add values from each list at the same time: 要创建一个类似display的表格,您需要同时从每个列表中添加值:

for(int i = 0; i < totalResult1.size(); i++){
      showArea.append(totalResult1.get(i) + "\t\t");  
      showArea.append(totalResult2.get(i) + "\n");
}

The should do the trick. 应该做的把戏。 But in general, when you want a table you shouldn't use a text area, you can use a table control instead. 但是总的来说,当您想要一个表时,不应使用文本区域,而可以使用表控件。

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

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