Java Swing JTable Demo

Sdílet
Vložit
  • čas přidán 9. 01. 2023
  • A short tutorial on how to create a JTable with a JScrollpane and populate it with data from a file or database.

Komentáře • 10

  • @magnadux8123
    @magnadux8123 Před 5 měsíci +1

    Thank You :)

  • @gessocode
    @gessocode Před měsícem

    ❤❤❤❤❤❤❤❤❤❤❤

  • @andreirizea5407
    @andreirizea5407 Před rokem

    I’ve done exactly the same as you but my table will not want to render onto the screen. Any ideas why.l? It is added to the correct panel

    • @alleyb62
      @alleyb62  Před rokem

      The most common problem is the layout. You may try setting your layout to null, or using a different type of layout.

    • @andreirizea5407
      @andreirizea5407 Před rokem

      @@alleyb62Layout is already set to null :(

    • @kevinhart437
      @kevinhart437 Před rokem

      @@alleyb62 is the problem still there? maybe i could help you, but need to see the code, is it
      same as the video?

    • @kevinhart437
      @kevinhart437 Před rokem

      package App.Anmeldung;
      import javax.swing.*;
      import javax.swing.filechooser.FileSystemView;
      import java.awt.*;
      import java.io.BufferedReader;
      import java.io.FileNotFoundException;
      import java.io.FileReader;
      import java.io.IOException;
      import java.util.ArrayList;
      public class AnmeldungsFenster {
      JFrame frame;
      JPanel panel;
      JScrollPane scrollPane;
      JTable table;
      String[] spalten;
      Object[][] data;
      public AnmeldungsFenster() {
      frame = new JFrame();
      panel = new JPanel();
      spalten = new String[]{"Interne Bezeichnung", "elektr. Bezeichnung", "Zulassung", "Artikelnummer EK", "Artikelnummer VK", "Warencode", "Verkaufart", "Packungsgrößen", "EU-NUmmer", "Packmittel Bez.", "elektr. Packmittel Bez.", "Zielland", "PPN/PZN", "Dormant", "GTIN", "Packmittel", "Material Bez."};
      data = getData();
      table = new JTable(data, spalten);
      scrollPane = new JScrollPane(table);
      panel.setLayout(new BorderLayout());
      panel.add(scrollPane, BorderLayout.CENTER);
      frame.add(panel);
      frame.setSize(1290, 860);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      }
      Object[][] getData() {
      String path = FileSystemView.getFileSystemView().getDefaultDirectory().getPath();
      try {
      BufferedReader br = new BufferedReader(new FileReader(path + "/Mappe1.csv")); //change to you name the expetion that will come maybe show your whole data path mine is : C:\Users\instadmin\OneDrive\Dokumente + "\YOURFILE"
      ArrayList list = new ArrayList();
      String str = "";
      while ((str = br.readLine()) != null) {
      String[] rowData = str.split(",");
      list.add(rowData);
      System.out.println(str);
      }
      int n = spalten.length;
      Object[][] data = new Object[list.size()][n];
      for (int i = 0; i < list.size(); i++) {
      String[] rowData = list.get(i);
      if (rowData.length != n) {
      rowData = new String[n];
      }
      data[i] = rowData;
      }
      br.close();
      return data;
      } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
      } catch (IOException e) {
      throw new RuntimeException(e);
      }
      }
      public static void main(String[] args) {
      SwingUtilities.invokeLater(() -> new AnmeldungsFenster());
      }
      }

    • @malshisathsarani5196
      @malshisathsarani5196 Před 6 měsíci

      true
      :(