tutorials.de Buch-Aktion 05/2012
ERLEDIGT
JA
ANTWORTEN
1
ZUGRIFFE
431
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Code46 Code46 ist offline Mitglied Gold
    Registriert seit
    Sep 2008
    Beiträge
    184
    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 :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    
    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.");
        }
    }
     

  2. #2
    genodeftest genodeftest ist offline Mitglied Brillant
    Registriert seit
    Jun 2009
    Beiträge
    870
    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.
     
    Code bitte so einfügen: [java]System.out.println("Hallo");[/java] (Analog für andere Programmiersprachen)
    Code java:
    1
    
    System.out.println("Hallo");
    hilfreich zu Java: Really Big Index, Java ist auch eine Insel Band 1 und Band 2.
    ___________
    Ubuntu Bug #1: Microsoft has a majority market share
    Casecon: Projekt leiser Käse

Ähnliche Themen

  1. Von einer anderen klasse etwas in das JTabel hinzufügen?
    Von Code46 im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 2
    Letzter Beitrag: 08.01.11, 09:46
  2. Antworten: 17
    Letzter Beitrag: 27.01.08, 23:09
  3. UTF-8 wird nicht richtig angezeigt
    Von Roland Deschain im Forum PHP
    Antworten: 3
    Letzter Beitrag: 22.11.07, 12:01
  4. Antworten: 3
    Letzter Beitrag: 08.05.04, 16:19
  5. *.css wird nicht richtig angezeigt ....
    Von Branco im Forum CSS
    Antworten: 3
    Letzter Beitrag: 26.01.04, 21:56