将数据从左上角单元格移动到第一列的每一行

时间:2014-10-15 20:57:14

标签: java swing jtable

我创建了一个显示数据的小应用程序。每个数字都由一个空格分隔,但是所有数字都显示在表格的左上栏而不是第一列的每一行。我已经努力工作了一个小时试图让这个工作,但不能 - 有人可以请帮助

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
}

1 个答案:

答案 0 :(得分:0)

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

然后,尝试添加:

  ...
    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确实会有所帮助。