将数据从左上方的单元格移至第一列的每一行 [英] Moving the data from the upper left cell to each row of the first column

查看:77
本文介绍了将数据从左上方的单元格移至第一列的每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个显示数据的小应用程序.每个数字用空格分隔,但是所有数字都出现在表格的左上列中,而不是第一列的每一行中.我努力工作了好一个小时才设法使它正常工作,但是不能-有人可以帮忙吗

I have created a small app that displays data. Each number is seperated by a space, however all the numbers appear in the upper left column of the table rather than each row of the first column. I have struggled for a good hour tried to get this to work but cant - can someone please help

package stackoverflow;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import javax.swing.JTextArea;
import com.sun.java.swing.*;
import javax.swing.table.*;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Learningfromscrach extends JFrame {

//declare all the parts that make up the GUI    
    private JLabel textJLabel;
    private JPanel PanelJlabel;//JLabel is actually a parameter in JAVA
    private TitledBorder PanelJborder;
    private JButton callsystem;
    private JTextArea txtarea;
//private JTable table;

    DefaultTableModel model;
    JTable table;
    String col[] = {"SBP", "SSP", "Period"};

    public Learningfromscrach() //tell java to initiate the create interface
    {
        createUserInterface();//create method private void createUserInterface//aframe is parameter this has to match the private void
    }

    //private Learningfromscrach() {
    //  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    // }
    private void createUserInterface() //all the parts to create the userinterface      
    {//from here
        Container contentPane = getContentPane();
        contentPane.setLayout(null);// i am responsible for setting positioning and size of components
        setTitle("Cashout Prices");//setTitle is also a JAVA Parameter

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setSize((int) (screenSize.width / 6), (int) (screenSize.height / 1.1));//cast int for width
        setVisible(true);    //makes the java application show

        PanelJborder = new TitledBorder("Cashout Prices");
        PanelJlabel = new JPanel(); //define new component
        PanelJlabel.setBounds(16, 50, 400, 2000);
        PanelJlabel.setLayout(null);

        contentPane.add(PanelJlabel); //add to Pane

        String url = "http://bmreports.com/bsp/additional/soapfunctions.php?element=SYSPRICE&dT=NRT";
        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException ex) {
            Logger.getLogger(Learningfromscrach.class.getName()).log(Level.SEVERE, null, ex);
        }
        Elements Periodparagraphs;
        Elements SSPparagraphs;
        Elements SBPparagraphs;

        Periodparagraphs = doc.select("SP");//counts the number of SSP Paragraphs in the entire document
        SSPparagraphs = doc.select("SSP");//counts the number of SSP Paragraphs in the entire document
        SBPparagraphs = doc.select("SBP");//counts the number of SBP Paragraphs in the entire document

        String[] numbers0 = Periodparagraphs.text().toString().split(" ");
        String[] numbers = SSPparagraphs.text().toString().split(" ");//takes the paragraph which contains all the SSP paragraphs and splits where the space occurs, stored in the array numbers
        String[] numbers1 = SBPparagraphs.text().split(" ");

        model = new DefaultTableModel(col, 90);//50 is number of rows    
        table = new JTable(model) {
            @Override

            public boolean isCellEditable(int arg0, int arg1) {
                return false;
            }
        };

        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.yellow);
        contentPane.add(table);

        table.setValueAt(SBPparagraphs.text(), 1, 0); //first number is moves placing down by 2 rows//2nd number is next cclumn and so on
        //table.setValueAt("fgfg",0,0);

        table.setSize(screenSize.width / 4, (int) (screenSize.height / 1.1));
        table.setBounds(16, 50, 400, 2000);
        table.setLayout(null);
        table.setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        callsystem = new JButton();//creates new button
        callsystem.setText("Call System Prices");
        callsystem.setBounds(150, 16, 150, 24);
        contentPane.add(callsystem);
        callsystem.addActionListener(//created buttton that now Java listens for being pressed

                new ActionListener()//new listener event created by action
                {
                    public void actionPerformed(ActionEvent event) //action peformed action event event
                    {
                        callsystemActionPerformed(event);//call private void

                    }
                }
        );
    }

    private void callsystemActionPerformed(ActionEvent event) {

    }

//to hear this all refers to the contentpane

    public static void main(String[] args) {
        // TODO code application logic here
        Learningfromscrach application = new Learningfromscrach();//call learningfromscrach application

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//without this the userinterface cant be called

    }

    //next step to understand how to get data into the panel
}

推荐答案

您似乎没有将数据添加到表中.

You don't seem to be adding your data to the table.

然后,尝试添加以下内容:

Then, try adding this:

  ...
    String[] numbers0 = Periodparagraphs.text().toString().split(" ");
    String[] numbers = SSPparagraphs.text().toString().split(" ");//takes the ...
    String[] numbers1 = SBPparagraphs.text().split(" ");

    int tableRows;

    if (numbers0.length > numbers.length && numbers0.length > numbers1.length)
    {
        tableRows = numbers0.length;
    }
    else if (numbers.length > numbers0.length && numbers.length > numbers1.length)
    {
        tableRows = numbers.length;
    }
    else
    {
        tableRows = numbers1.length;
    }
    //model = new DefaultTableModel(col, 90);//50 is number of rows --You don't seem to need this

    Object[][] data = new Object[tableRows][3];

    for (int x = 0; x < tableRows; x++ )
    {
        try
        {
            data[x][0] = numbers1[x];
        }
        catch (IndexOutOfBoundsException e)
        {
            data[x][0] = "-";
        }

        try
        {
            data[x][1] = numbers[x];
        }
        catch (IndexOutOfBoundsException e)
        {
            data[x][1] = "-";
        }

        try
        {
            data[x][2] = numbers0[x];
        }
        catch (IndexOutOfBoundsException e)
        {
            data[x][2] = "-";
        }
    }



    table = new JTable(data,col) 
    {
        @Override

        public boolean isCellEditable(int arg0, int arg1) 
        {
            return false;
        }
    };

此外,我无法使您的项目正确显示(实际上是完全).您可能想探索Flow Layout(我更喜欢spring)以外的其他布局选项.此外,JScrollPane确实会有所帮助.

Also, I wasn't able to get your project to display correctly (at all, really). You may want to explore some other layout options besides Flow Layout (I prefer spring). In addition, a JScrollPane would really help.

这篇关于将数据从左上方的单元格移至第一列的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆