JTable Probleme



  • Hi zusammen,

    ich habe ein Jpanel in einem Jpanel und möchte in das innere Jpanel eine Jtable einbauen. Aber irgendwie klappt es nicht, entweder ich habe keinen Header (JScrollpane nicht eingebunden) oder die Tabelle geht über das gesamte Frame (JScrollPane mit eingebunden). hab schon im inet gesucht, hab keine lösung gefunden. Wäre für jeden tipp dankbar 🙂

    Mein Code

    package table;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.table.*;
    import javax.swing.event.*;
    
    public class main extends JFrame {
    
      String[] columnNames = {"Feld1","Feld2","Feld3","Feld4","Feld5","Feld6","Feld7","Feld8"};
    
      String[][] data = {{"1","1","1","1","1","1","1","1"},
                          {"2","2","2","2","2","2","2","2"},
                          {"2","2","2","2","2","2","2","2"},
                          {"2","2","2","2","2","2","2","2"},
                          {"2","2","2","2","2","2","2","2"}
                          };
    
      JTable jTable1= new JTable(data, columnNames);
    
      JPanel jPanel1 = new JPanel();
      JPanel jPanel2 = new JPanel();
    
      JButton Option1 = new JButton();
      JButton Option2 = new JButton();
      JButton b3 = new JButton();
    
      public main() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
    
      public static void main(String[] args) {
       main mains = new main();
       mains.setVisible(true);
       mains.setResizable(false);
       mains.jPanel1.setVisible(true);
       mains.jPanel2.setVisible(false);
     }
    
      private void jbInit() throws Exception {
    
        this.setSize(640,480);
        this.setDefaultCloseOperation(3);
        this.setTitle("JTABLE-TESTGEBIET");
        this.getContentPane().add(jPanel1);
    
        jPanel1.setLayout(null);
        jPanel1.setBackground(SystemColor.activeCaptionBorder);
        jPanel1.setEnabled(true);
        jPanel1.add(Option1, null);
        jPanel1.add(jPanel2, null);
        jPanel1.add(Option2, null);
    
        jPanel2.setLayout(null);
        jPanel2.setBackground(SystemColor.activeCaptionBorder);
        jPanel2.setBounds(new Rectangle(0, 50, 640, 480));
        jPanel2.add(jTable1, null);
        jPanel2.add(b3, null);
    
        Option1.setBackground(SystemColor.activeCaptionBorder);
        Option1.setBounds(new Rectangle(15, 15, 90, 30));
        Option1.setBorder(BorderFactory.createRaisedBevelBorder());
        Option1.setActionCommand("Option1");
        Option1.setFocusPainted(false);
        Option1.setText("Option1");
    
        Option2.setBackground(SystemColor.activeCaptionBorder);
        Option2.setBounds(new Rectangle(124, 15, 95, 30));
        Option2.setBorder(BorderFactory.createRaisedBevelBorder());
        Option2.setActionCommand("Option2");
        Option2.setFocusPainted(false);
        Option2.setText("Option2");
    
        b3.setBackground(SystemColor.activeCaptionBorder);
        b3.setBounds(new Rectangle(527, 367, 88, 27));
        b3.setBorder(BorderFactory.createRaisedBevelBorder());
        b3.setActionCommand("b3");
        b3.setFocusPainted(false);
        b3.setText("Button");
    
        jTable1.setSelectionMode(1);
        jTable1.setBorder(BorderFactory.createLoweredBevelBorder());
        jTable1.setRequestFocusEnabled(false);
        jTable1.setVerifyInputWhenFocusTarget(false);
        jTable1.setAutoCreateColumnsFromModel(false);
        jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jTable1.setGridColor(Color.lightGray);
        jTable1.setSelectionBackground(SystemColor.activeCaption);
        jTable1.setSelectionForeground(new Color(254, 255, 255));
        jTable1.setShowHorizontalLines(false);
        jTable1.setShowVerticalLines(false);
        jTable1.setBackground(SystemColor.activeCaptionBorder);
        jTable1.setForeground(new Color(1, 0, 0));
        jTable1.setBounds(new Rectangle(15, 20, 590, 330));
    
    /************************************************************************/
    // TESTEN DER JTABLE
    
        this.getContentPane().add((new JScrollPane(jTable1)),BorderLayout.CENTER);
    
    /***************************************************************************/
    
        Option1.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            Option1_actionPerformed(e);
          }
        });
    
        Option2.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            Option2_actionPerformed(e);
          }
        });
      }
    
      void Option1_actionPerformed(ActionEvent e) {
        if(e.getActionCommand() == "Option1"){
          this.jPanel2.setVisible(true);
        }
      }
    
      void Option2_actionPerformed(ActionEvent e) {
        if(e.getActionCommand() == "Option2"){
          this.jPanel2.setVisible(false);
    
        }
      }
    }
    

    Besten Dank im voraus 🙂



  • Bei mir hat es geholfen, dass ich dem JScrollPane mit .setPreferredSize(...) eine Größe zugeteilt habe.


Anmelden zum Antworten