无法在动态创建的JTable中看到列

时间:2014-02-07 09:00:42

标签: java swing jtable jscrollpane

我有一个动态创建和填充的表。单击其他表单中的按钮时会显示此动态表单。这是代码:

package ExaData;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.NumberFormat;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RefineryUtilities;

public class ShowGraph extends javax.swing.JFrame {


String[][] data;
String[] columns={"Tanım","Değer"};

 Sessionn obj;
  String SGA="SGA";
  String PGA="PGA";
  String JAVAPOOL="JAVAPOOL";
   JCheckBox SGA_CHECK;
    JCheckBox PGA_CHECK;
    JCheckBox JAVA_POOL_CHECK;
     JTable table;
     JPanel btnPanel,tblPanel;
    ChartPanel CP;
     JFreeChart chart;
     DefaultCategoryDataset dataset;
    boolean sgaflag=true;
    boolean pgaflag=true;
    boolean javapoolflag=true;
    JScrollPane scroller;
     JTableHeader header ;
 JCheckBox[] checkboxes;
 boolean[] chkboxflag;
  public ShowGraph()
  {

   //   super();

      SGA_CHECK=new JCheckBox("SGA");
         PGA_CHECK=new JCheckBox("PGA");
         JAVA_POOL_CHECK=new JCheckBox("JAVA POOL");
         SGA_CHECK.setSelected(true);
         PGA_CHECK.setSelected(true);
         JAVA_POOL_CHECK.setSelected(true);
           btnPanel= new JPanel(new FlowLayout());
           tblPanel=new JPanel(new FlowLayout());
        btnPanel.add(SGA_CHECK);
        btnPanel.add(PGA_CHECK);
        btnPanel.add(JAVA_POOL_CHECK);
        this.add(btnPanel, BorderLayout.CENTER);
        this.add(tblPanel,BorderLayout.SOUTH);
           this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

          scroller = new JScrollPane(tblPanel); 

this.getContentPane().add(scroller, BorderLayout.CENTER);  

            PGA_CHECK.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {                   
                pgaflag=e.getStateChange() == ItemEvent.SELECTED;
              update();
            }
        });
        SGA_CHECK.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                 sgaflag=e.getStateChange() == ItemEvent.SELECTED;
              update();
            }
        });
         JAVA_POOL_CHECK.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                 javapoolflag=e.getStateChange() == ItemEvent.SELECTED;
               update();
            }
        });

  }


     private CategoryDataset createDataset() {

        dataset = new DefaultCategoryDataset();
        //BURADA ANA TANIMLAMA BİTTİ


        if(sgaflag){
        dataset.addValue(32.4, SGA, "Node 1");
          dataset.addValue(43.2, SGA, "Node 2");
          dataset.addValue(23.0, SGA, "Node 3");
            dataset.addValue(13.0, SGA, "Node 4");
        }
            if(pgaflag){
        dataset.addValue(17.8, PGA, "Node 1");
          dataset.addValue(15.6, PGA, "Node 2");
           dataset.addValue(111.3, PGA, "Node 3");
           dataset.addValue(11.8, PGA, "Node 4");
            }
            if(javapoolflag){
        dataset.addValue(27.7, JAVAPOOL, "Node 1");   
        dataset.addValue(18.3, JAVAPOOL, "Node 2");      
        dataset.addValue(25.5, JAVAPOOL, "Node 3");      
        dataset.addValue(29.5, JAVAPOOL, "Node 4");
            }
        return dataset;
    }
 private JFreeChart createChart(CategoryDataset dataset) {

         chart = ChartFactory.createStackedBarChart(
            "EXADATA Görünümü",  // chart title
            "Node'lar",                  // domain axis label
            "Yüzde Dağılım",                     // range axis label
            dataset,                     // data
            PlotOrientation.VERTICAL,    // the plot orientation
            true,                        // legend
            true,                        // tooltips
            false                        // urls
        );
        CategoryPlot plot = (CategoryPlot) chart.getPlot();


        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance());
        StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
        renderer.setRenderAsPercentages(true);
        renderer.setDrawBarOutline(false);
        renderer.setBaseItemLabelsVisible(true);
        renderer.setSeriesPaint(0,Color.LIGHT_GRAY);
        renderer.setSeriesPaint(2, Color.LIGHT_GRAY);
        renderer.setBaseItemLabelGenerator(
        new StandardCategoryItemLabelGenerator());
        return chart;

    }

public void veriCek(String[][] data)
    {
    this.data=data;
    }

 public void run()
{  

       chart = createChart(createDataset());
      table =new JTable(data,columns);
      table.getColumnModel().getColumn(0).setPreferredWidth(200);
       table.getColumnModel().getColumn(1).setPreferredWidth(200);
   CP=new ChartPanel(chart);  
           this.add(CP, BorderLayout.NORTH);
         header = table.getTableHeader();
table.add(header,BorderLayout.NORTH);
           tblPanel.add(table,BorderLayout.CENTER);

          this.setSize(600,600);
    RefineryUtilities.centerFrameOnScreen(this);     
    this.setVisible(true);

}
 public void update()
 {
 this.remove(CP);
       chart = createChart(createDataset());
   CP=new ChartPanel(chart);  
           this.add(CP, BorderLayout.CENTER);
    this.setVisible(true);
 }


  public static void main(final String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

            }
        });
    }
}

当我运行它时,我可以正确地看到每个对象,我的表有数据。但我看不到我的表列标题。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

您必须将表格添加到滚动窗格

JScrollPane scroller = new JScrollPane(table);

然后将滚动窗格添加到您的gui。