Jtable qui prend les dimensions de la jframe

Contenu du snippet

C'est une table qui prend les dimensions de la fenetre
quels que soient les moyens utilises pour
les modifier (maximiser, minimiser, etirer vers le bas
ou contracter vers le haut, etirer vers la droite
ou contraxter vers la gauche - mais a droite
et a gauche c'est standard. -)
Une condition cependant : ne pas avoir trop de cellules ...

Source / Exemple :


/**
  *

  • Une table qui prend les dimensions de la fenetre
  • quels que soient les moyens utilises pour
  • les modifier (maximiser, etirer vers le bas
  • ou le haut, vers la droite ou la gauche - mais a droite
  • et a gauche c'est standard.)
*
    • /
import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import java.awt.Container; import java.awt.*; import java.awt.event.*; import java.io.*; import java.lang.String; public class TabVarDemo { static JFrame frame; static JTable table; // le tableau static Dimension screen; static JScrollPane scrollpane; static int heightfram = 100; static int heightframinit; static int widthframinit; static int widthframmax; static int widthframscreen; static int widthfram = 100; static int cols = 10, lignes = 12; static int heightRw; static int heightRwtot; static int widthRw; public static void main(String [] args) { frame = new JFrame(cols + "X" + lignes); table = new JTable(lignes,cols); //Recupereration des dimensions de l'ecran utilise: screen = Toolkit.getDefaultToolkit().getScreenSize(); System.out.println("screen = " + screen); frame.setSize(screen); widthframscreen = frame.getSize().width; table.setPreferredScrollableViewportSize(screen); //scroller JScrollPane scrollpane = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); frame.getContentPane().add(scrollpane); heightRw = table.getRowHeight(); widthRw = 16; heightRwtot = heightRw * (lignes +1); heightfram = heightRwtot + 38; // avec cadre // heightfram = heightRwtot + 3; // sans cadre mais taille table figee heightframinit = heightfram; widthfram = (widthRw * cols); widthframinit = widthfram; frame.setSize(widthfram + 16,heightfram);// + 16 pour le scroll // frame.setUndecorated(true); // Pour supprimer le cadre frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); { ComponentListener listener = new ComponentAdapter() { public void componentResized(ComponentEvent evt) { Component c = (Component)evt.getSource(); // nouvelles dimensions Dimension newSize = c.getSize(); widthfram = frame.getSize().width; heightfram = frame.getSize().height; table.setPreferredScrollableViewportSize(newSize); widthfram = frame.getSize().width; heightfram = frame.getSize().height; if (heightfram < heightframinit) { heightfram = heightframinit; } if (widthfram <= widthframinit) { widthfram = widthframinit; } widthRw = widthfram / cols; heightRw = (heightfram - 54) / lignes; if (heightRw < 1) { heightRw = 16; } if (widthfram < (widthframscreen + 8)) // si pas maximise { int tailleframeajustee = ( (heightRw * lignes) + 54 ); heightfram = tailleframeajustee; } frame.setSize(widthfram,heightfram); table.setRowHeight(heightRw); frame.setVisible(true); } }; frame.addComponentListener(listener); } } }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (cs_danimo)