JTabel wird nicht richtig angezeigt aber ich kann etwas hinzufügen?

Code46

Erfahrenes Mitglied
Hallo ich habe ein kleines problem mit meinem JTabel.
Ich habe ein JTabel in einem BoxLayout hinzugefügt jedoch zeigt es nicht richtig an. Wenn ich etwas ins Textfield schreibe und den Button betätige fügt es hinzu und ich kann nur das geschriebene sehen.Das JTabel hat kein Border,Title etc.(es wurde alles definiert jedoch zeigt es nicht an).

Hier mein Code:
Code:
package javaapplication24;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import javax.swing.table.DefaultTableModel;


class Product implements Comparable {
    private String sql_query;

public Product(String n) {
    sql_query = n;
}
public Product() {
    this ("default");
}

public String getName() {
    return sql_query;
}


public int compareTo(Object o) {
    Product temp = (Product) o;


    return (sql_query.compareTo(temp.sql_query));
}
}


public class Project extends JFrame implements ActionListener {

   JButton btnExecute = new JButton("Execute");
   JButton btnDelete = new JButton("Delete");
   JTextField jtfInput = new JTextField(10);
   TextArea jtaOutput = new TextArea();
    ArrayList products = new ArrayList();
    JTable tab = new JTable();


   Product [] initialProducts = {};
   DefaultTableModel tabMod = new DefaultTableModel();
   Product [] initialItem = {new Product("Select * From Staff")};



 public Project(){

        setLayout(new BorderLayout());
        setBounds(500,300,690,300);
        setTitle("Earth Wind & Fire");
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setVisible(true);
        setResizable(true);
         tab.setModel(tabMod);
        tabMod.addColumn("SQL Query");

        JPanel north = new JPanel();
        JPanel south = new JPanel();
        JPanel east = new JPanel();
        JPanel west = new JPanel();
        JPanel center = new JPanel();
        add("North",north);
        add("South",south);
        add("East",east);
        add("West",west);
        add("Center",center);


        center.setBorder(BorderFactory.createTitledBorder("SQL Query"));
        BoxLayout layout = new BoxLayout(center, BoxLayout.X_AXIS);
         center.add(tab);


        east.setBorder(BorderFactory.createTitledBorder("Output"));
        BoxLayout la = new BoxLayout(east, BoxLayout.X_AXIS);
        east.add(jtaOutput);

        south.setBorder(BorderFactory.createTitledBorder("Input"));
        BoxLayout lay = new BoxLayout(east, BoxLayout.X_AXIS);
        south.add(jtfInput);
        south.add(btnExecute);
        south.add(btnDelete);
        btnExecute.addActionListener(this);
        
        loadInitialProducts();

          btnDelete.setToolTipText("Select the records to delete first");
          btnDelete.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
          deleteProduct();
      }
    });

     btnExecute.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
          addProduct();
      }
    });

}


   public void deleteProduct() {
    int [] rows = tab.getSelectedRows();
    for (int i = 0; i < rows.length; i++) {
        tabMod.removeRow(rows[i] - i); // delete from JTable
        products.remove(rows[i] - i); // delete from ArrayList
    }
}

public void sortProducts() {
    Collections.sort(products); // sorts into name order
    loadProductsIntoTable();
}

// Add a new products to the ArrayList and the JTable
public void addProduct() {
    String [] row = new String [2];
    Product newProd = new Product(jtfInput.getText());

    products.add(newProd); // add to the ArrayList
    row[0] = newProd.getName();
    tabMod.addRow(row); // add to the JTable
}
public void loadInitialProducts() {
    for (int i = 0; i < initialProducts.length; i++) {
        products.add(initialProducts[i]);
    }
    loadProductsIntoTable();
}
public void loadProductsIntoTable() {
    tabMod.setRowCount(0);

    String [] row = new String [2];
    for (Iterator i = products.iterator(); i.hasNext();) {
        Product temp = (Product) i.next();
        row[0] = temp.getName();
        tabMod.addRow(row);
    }
}
public static void main(String [] a){
    Project me = new Project();
    me.addWindowListener(new WindowAdapter () {
       public void windowClosing(WindowEvent e) {
          System.exit(0) ;
        }
      });
    me.pack();
    me.setVisible(true);
  }

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
 
Also…
in Zeile 89 erzeugst du das 2. Boxlayout, das du dem JPanel east hinzufügst. du meinst wahrscheinlich das JPanel south ;)

Sorry, deinen eignlichen Fehler finde ich nicht.
 
Zurück