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.

JTable, Reihe hinzufügen

Empfohlene Antworten

Servus!

habe ein TableModel extends AbstractTableModel

und ein JFrame mit der Tabelle und einem JButton.

ich möchte bei einem Klick auf den Button eine neue Tabellenreihe anfügen.

wie kann ich das realisieren?


public class myTableModel extends AbstractTableModel

{

   String[] columnNames = {"Nr","Name","Preis"};

   Object[][] data = {{

      new Integer(1),

      new String("was weiss ich"),

      new Double(2.78)

   }};


   public String getColumnName(int column){

      return columnNames[column];

   }


   public int getColumnCount() {

      return columnNames.length;

   }


   public int getRowCount() {

      return data.length;

   }


   public Object getValueAt(int row, int column) {

      return data[row][column];

   }


   public void setValueAt(Object obj, int row, int column){

      data[row][column]=obj;

      fireTableDataChanged();

   }

   public Class getColumnClass (int column){

      if(column == 1)return String.class;

      else return Double.class;

   }

}

ein JFrame zur Anzeige der Tabelle :

public class testFrame extends JFrame{


	testFrame()

	{

		super("Tabelle");

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


		Container p = getContentPane();

		p.setLayout(new BorderLayout());


		myTableModel model = new myTableModel();

		JTable table = new JTable(model);

		JScrollPane scrollPane = new JScrollPane();

		scrollPane.getViewport().add(table);


		JButton addData = new JButton("+");

		p.add(BorderLayout.NORTH, addData);

		p.add(BorderLayout.CENTER, scrollPane);


		pack();

		show();

	}


	public static void main(String[] args) {

		new testFrame();

	}

}


- einen actionListener auf den Button legen

- in dieser Action einfach einen Vector mit der Anzahl der Spalten zusammen bauen

- diesen Vector mit der Methode "addRow(Vector row)" dem TableModel hinzufügen.

- fertig.

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.