Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Array in JTable einfügen

Empfohlene Antworten

Veröffentlicht

Hallo Leute!

Ich sitze jetzt schon ewig daran und hoffe ihr könnt mir helfen.

Habe ein Array mit verschiedenen Werten und diese Werte will ich in einer JTable einfügen. Ich habe das Netz durchsucht und nix gefunden. Habe schon viele Sachen ausprobiert, aber leider hat nix funktioniert.:(

Bitte helft mir!

Gruß

sunnyw86

mal nen ganz simples Beispiel:


import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable; 


public class mitarray {


    /** Creates a new instance of mitarray */

    public mitarray() {

    }


    public static void main( String[] args ){

      String[][] data = new String[][]{

            {"a", "b", "c", "d"},

            {"e", "f", "g", "h"},

            {"i", "j", "k", "l"}

      };



      String[] title = new String[]{

            "A", "B", "C", "D"

      };


      // Das JTable initialisieren

      JTable table = new JTable( data, title );


      JFrame frame = new JFrame( "Demo" );

      frame.getContentPane().add( new JScrollPane( table ) );

      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

      frame.pack();

      frame.setVisible( true );

   } 

Vielen Dank, aber das hilft mir leider nicht weiter. Mir wird das Array übergeben und ich muß es dann in die Tabelle einfügen.

	

public void te(String name, String[] wert) //hier wird mir das Array übergeben

{

       this.setVisible(true);

       textField_Name.setText(name);


       //jetzt soll er die Werte in die Tabelle eintragen, geht aber nicht

       table_Wert.setValueAt(wert,0,0); 

}

So jetzt hab ich es hinbekommen das ein Wert in meiner Tabelle steht-:)


public void te(String name, String[] wert)

{

       this.setVisible(true);

       textField_Name.setText(name);

       String[][] data = new String[][]{{wert[0]}}; //Wert[0]->mein Array

       String[] title = new String[]{""}; 


       tabellenModel = new DefaultTableModel(data, title);

       getJTable().setModel(getTableModel());	

       scrollPane1.setViewportView(getJTable());

}

Mein Problem ist jetzt, dass ich nicht weiß wie ich an die ganzen Daten wert[1] usw. ran komme.

Bitte helft mir.

schreib ne neue klasse (myTableModel z.b.) abgeleitet von AbstractTableModel..

im model haste dann methoden um deine tabelle zu füllen.

Mein Problem ist jetzt, dass ich nicht weiß wie ich an die ganzen Daten wert[1] usw. ran komme.
Und warum list du dir dann nicht erstmal komplett das von kingofbrain gepostete Tutorial durch? Danach sollten in der Regel alle Basisfragen zum Thema JTable erledigt sein.

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.