GridBagLayout Problem mit JTable

Status
Nicht offen für weitere Antworten.
D

DuenenTuessy

Gast
Hallo zusammen,
ich habe folgendes Problem, welches ich nicht verstehe. Ich habe gestern schon den ganzen Tag danach gesucht, aber irgendwie keien Lösung gefunden.
Wenn man das Beispiel ausführt sieht man dasss, die zweio Textfelder sehr klein sind, obwohl genü+gend Platz ist. Maximiert man das Fenster so werden sie so groß wie ich mir es vorstelle. Das komische ist, sie werden nicht groß wenn ich das Fenster nach unten ziehe und nicht wenn ich es in die Breite ziehe.
Irgendwo gab es die Lösung setMinimumSize zu benutzen, aber auch dann verhält es sich komisch. Wenn ich diese setze, macht er es bei dem kleinen Fenster, aber wenn ich es maximiere, wird es komischerweise kleiner...

Übersehe ich da irgendetwas?

Wenn ich übrigens in das ScrollPane eine Textarea eingefügt habe hat alles funktioniert, es muss also irgendwie an der Tabelle liegen...

Ich hoffe mir kann jemand helfen.

Grüße Nicole

Code:
import java.awt.*;
import javax.swing.*;

public class Beispiel extends JFrame{					 	 					
	JTable table	= new JTable(50,2);

	public Beispiel() {
		super("Beispiel"); 
		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		setSize(800,600);
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBereiche();	
		setVisible(true);
	}

	protected void setBereiche() {
		JScrollPane scrollPane	= new JScrollPane(table);
		final Container cont	= getContentPane();
		GridBagLayout gbl		= new GridBagLayout();
		cont.setLayout(gbl);

		//erstellt Label/Textfelder/...
		JLabel titel_lb	= new JLabel("Titel:");
		JLabel bez_lb	= new JLabel("Bezeichnung:");
		JTextField titel_tf	= new JTextField("fhfggjh");
		JTextField bez_tf	= new JTextField("fgjsgj");
		
		//Fügt Elemente dem Fenster hinzu(Insets: top, left, bottom, right)
		addComponent(cont, gbl, titel_lb      ,  0, 0, 1, 1, GridBagConstraints.WEST,      new Insets(20,40,  0,10) ,0   ,0  , GridBagConstraints.NONE);
		addComponent(cont, gbl, titel_tf      ,  1, 0, 1, 1, GridBagConstraints.WEST,      new Insets(20, 0,  0,10) ,0   ,0  , GridBagConstraints.NONE);
		addComponent(cont, gbl, bez_lb        ,  2, 0, 1, 1, GridBagConstraints.WEST,      new Insets(20,50,  0,10) ,0   ,0  , GridBagConstraints.NONE);
		addComponent(cont, gbl, bez_tf        ,  3, 0, 1, 1, GridBagConstraints.WEST,      new Insets(20, 0,  0,10) ,0   ,0  , GridBagConstraints.NONE);
		addComponent(cont, gbl, new JLabel(""),  4, 0, 1, 1, GridBagConstraints.WEST,      new Insets(20, 0,  0,40) ,1.0 ,1.0, GridBagConstraints.BOTH);
		addComponent(cont, gbl, scrollPane    ,  0, 1, 5, 1, GridBagConstraints.CENTER,    new Insets(50,40,100,40) ,1.0 ,1.0, GridBagConstraints.BOTH);
						
		tabelleFuellen();
	}

	protected void tabelleFuellen(){
		for(int i = 0; i < table.getRowCount(); i++){
			for(int j = 0; j < table.getColumnCount(); j++){
				table.setValueAt("Test: r"+i+" c"+j,i,j);
			}
		}
	}

	protected void addComponent(Container cont, GridBagLayout gbl, JComponent comp, int x, int y, 
								int width, int height, int anchor, Insets in, double wx,double wy,int fill){
		GridBagConstraints c = new GridBagConstraints();

		c.weightx	= wx;		c.weighty	= wy;
		c.fill		= fill;
		c.gridx		= x;		c.gridy		= y;
		c.gridwidth	= width;	c.gridheight= height;
		c.insets	= in;		c.anchor	= anchor;
		
		gbl.setConstraints(comp,c);	cont.add(comp);
	}

	public static void main(String[] args){
		new Beispiel();
	}
}
 

foobar

Top Contributor
Code:
import java.awt.*;
import javax.swing.*;


public class Beispiel extends JFrame
{
    JTable table = new JTable(50, 2);

    public Beispiel()
    {
        super("Beispiel");
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        setSize(800, 600);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBereiche();
        setVisible(true);
    }

    protected void setBereiche()
    {
        JScrollPane scrollPane = new JScrollPane(table);
        final Container cont = getContentPane();
        GridBagLayout gbl = new GridBagLayout();
        cont.setLayout(gbl);

        // erstellt Label/Textfelder/...
        JLabel lblTitle = new JLabel("Titel:");
        JLabel lblDescription = new JLabel("Bezeichnung:");
        JTextField txtTitle = new JTextField("fhfggjh");
        JTextField txtDescription = new JTextField("fgjsgj");

        // Fügt Elemente dem Fenster hinzu(Insets: top, left, bottom, right)
        addComponent(cont, gbl, lblTitle,              0, 0, 1, 1, GridBagConstraints.WEST, new Insets(20, 40, 0, 10), 0.1, 0, GridBagConstraints.HORIZONTAL);
        addComponent(cont, gbl, txtTitle,              1,0, 1, 1, GridBagConstraints.WEST, new Insets(20, 0, 0, 10), 0.1, 0, GridBagConstraints.HORIZONTAL);
        addComponent(cont, gbl, lblDescription,              2,0, 1, 1, GridBagConstraints.WEST, new Insets(20, 50, 0, 10), 0.1, 0, GridBagConstraints.HORIZONTAL);
        addComponent(cont, gbl, txtDescription,              3, 0, 1, 1, GridBagConstraints.WEST, new Insets(20, 0, 0, 10), 0.1, 0, GridBagConstraints.HORIZONTAL);
        
        addComponent(cont, gbl, scrollPane,         0, 1, 5, 1, GridBagConstraints.CENTER, new Insets(50, 40, 100, 40), 1.0, 1.0, GridBagConstraints.BOTH);
        

        tabelleFuellen();
    }

    protected void tabelleFuellen()
    {
        for (int i = 0; i < table.getRowCount(); i++)
        {
            for (int j = 0; j < table.getColumnCount(); j++)
            {
                table.setValueAt("Test: r" + i + " c" + j, i, j);
            }
        }
    }

    protected void addComponent(Container cont, GridBagLayout gbl, JComponent comp, int x, int y, int width, int height, int anchor, Insets in, double wx, double wy, int fill)
    {
        GridBagConstraints c = new GridBagConstraints();

        c.weightx = wx;
        c.weighty = wy;
        c.fill = fill;
        c.gridx = x;
        c.gridy = y;
        c.gridwidth = width;
        c.gridheight = height;
        c.insets = in;
        c.anchor = anchor;

        gbl.setConstraints(comp, c);
        cont.add(comp);
    }

    public static void main(String[] args)
    {
        new Beispiel();
    }
}

So, jetzt sieht es etwas besser aus. Deine Insets sind viel zu groß und uneinheitlich, so wie der Rest des Codes auch.
 

DuenenTuessy

Mitglied
Ich hatte mein Beispiel nur zusammen gekürzt damit es net so lang ist.
Und das ich die offenen Klammern nicht in eine neue Zeile tue liegt daran das ich es so schöner finde und damit besser klar kommen, deswegen ist mein Code ja nicht direkt unstrukturiert!!!

Ich wollte aber gerade nicht das die Textfelder ewig weit weg von den Labels sind!

Aber trotzdem Danke für deine Mühe!
 

foobar

Top Contributor
Ich wollte aber gerade nicht das die Textfelder ewig weit weg von den Labels sind!
Dann mußt du bei den Labels den Wert von WidthX auf 0.1 setzen und bei den Textfeldern auf 1.0. Ausserdem mußt die horizontale Ausrichtung noch auf NORTHWEST gesetzt werden.

Und das ich die offenen Klammern nicht in eine neue Zeile tue liegt daran das ich es so schöner finde und damit besser klar kommen, deswegen ist mein Code ja nicht direkt unstrukturiert!!!
Mit unstrutkuriert meinte ich deine kruden Bezeichner: teileweise CamelCase, teilweise mit Unterstrichen, keine Prefixe etc.
Du machst dir das Leben unnötig schwer.
 

DuenenTuessy

Mitglied
Da ich ich nicht möchte das sich die Komponenten vergrößern, habe ich es wieder auf NONE gesetzt und das alte Problem. Außerdem hätte ich die Zwei Label mit Textfelder gern nicht soweit auseinander, deswegen das leere Label in meinem Beispiel, so waren die dichter beisammen und die Tabelle trotzdem über die ganze Breite!
 

foobar

Top Contributor
Pixelgenaues Positioniere ist mit dem Gbl nicht so einfach. Am besten setzt du die Komponenten auf die gewünschte Größe als set min, max, preferred-Size, etc und dann kannst du auch mit NONE arbeiten.
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
P AWT Problem mit Platzierung (GridBagLayout) AWT, Swing, JavaFX & SWT 2
G GridBagLayout Problem AWT, Swing, JavaFX & SWT 4
T GridBagLayout Problem AWT, Swing, JavaFX & SWT 3
B GridBagLayout Problem AWT, Swing, JavaFX & SWT 3
N GridBagLayout Problem AWT, Swing, JavaFX & SWT 6
D Swing Problem mit Gridbaglayout bzw. Größenanpassung JPanels AWT, Swing, JavaFX & SWT 7
Y LayoutManager Problem mit Gridbaglayout AWT, Swing, JavaFX & SWT 8
E Problem mit meiner GUI/GridbagLayout AWT, Swing, JavaFX & SWT 2
hdi Swing Problem mit GridBagLayout AWT, Swing, JavaFX & SWT 2
aze Problem mit GridBagLayout AWT, Swing, JavaFX & SWT 6
R LayoutManager Problem mit GridBagLayout AWT, Swing, JavaFX & SWT 3
hdi Swing Problem mit simplem GridBagLayout AWT, Swing, JavaFX & SWT 4
A Swing JPanel mit GridBagLayout Resize-problem AWT, Swing, JavaFX & SWT 1
S GridBagLayout Problem mit dem JPanel AWT, Swing, JavaFX & SWT 5
G GridBagLayout Problem AWT, Swing, JavaFX & SWT 4
S Problem beim Anpassen vom GridBagLayout AWT, Swing, JavaFX & SWT 10
N GridBaglayout Problem bei Groessenaenderung AWT, Swing, JavaFX & SWT 3
N Problem mit GridBagLayout und setPrefferedSize AWT, Swing, JavaFX & SWT 7
G JPanel Problem mit GridBagLayout AWT, Swing, JavaFX & SWT 4
N Layout-Problem (JScrollPane, GridBagLayout) AWT, Swing, JavaFX & SWT 3
L Problem mit GridBagLayout AWT, Swing, JavaFX & SWT 7
S GridBagLayout Felder formatieren AWT, Swing, JavaFX & SWT 1
S GridBagLayout - Probleme mit Bilderanzeige AWT, Swing, JavaFX & SWT 3
C GridbagLayout verstehen lernen AWT, Swing, JavaFX & SWT 1
H GridBagLayout macht mich wahnsinnig :-( AWT, Swing, JavaFX & SWT 5
BabySuna darstellungsprobleme mit JTabbedPane und GridBagLayout AWT, Swing, JavaFX & SWT 8
CptK Positionieren von Elementen in GridBagLayout AWT, Swing, JavaFX & SWT 4
A Probleme mit gridheight (GridBagLayout) AWT, Swing, JavaFX & SWT 6
Mario1409 AWT GridBagLayout AWT, Swing, JavaFX & SWT 5
J LayoutManager GridBagLayout, probleme mit Anordnung von Objekten AWT, Swing, JavaFX & SWT 6
DaCrazyJavaExpert Swing Komponenten in GridBagLayout werden Falsch angeordnet AWT, Swing, JavaFX & SWT 1
T LayoutManager Anordnen der Elemente im GridBagLayout AWT, Swing, JavaFX & SWT 11
K GridBagLayout mit reponsive Design AWT, Swing, JavaFX & SWT 2
K GridBagLayout verändert die größe? AWT, Swing, JavaFX & SWT 1
D Swing Größe einer JComboBox im GridBagLayout aufgrund der maximalen Länge der enthaltenen Daten AWT, Swing, JavaFX & SWT 7
B LayoutManager GridBagLayout und JScrollPane AWT, Swing, JavaFX & SWT 5
T GridBagLayout Anfängerprobleme AWT, Swing, JavaFX & SWT 3
Sin137 LayoutManager GridBagLayout Probleme AWT, Swing, JavaFX & SWT 6
L GridBagLayout Anordnung AWT, Swing, JavaFX & SWT 3
M Gridbaglayout Spaltenbreite AWT, Swing, JavaFX & SWT 3
M LayoutManager GridBagLayout passt seine größe nicht an AWT, Swing, JavaFX & SWT 3
V GridBagLayout AWT, Swing, JavaFX & SWT 4
N LayoutManager GridBagLayout - Grundlagen AWT, Swing, JavaFX & SWT 6
Neumi5694 Swing Gridbaglayout - automatische Anpassung verhindern AWT, Swing, JavaFX & SWT 1
F Breite beim GridBagLayout festlegen AWT, Swing, JavaFX & SWT 2
M Swing GridBagLayout Komponentengröße festsetzen AWT, Swing, JavaFX & SWT 1
J GridBagLayout mit Hilfe einer For-Schleife befüllen AWT, Swing, JavaFX & SWT 1
W GridBagLayout Größe geben AWT, Swing, JavaFX & SWT 1
HarleyDavidson Swing Seltsames Verhalten GridBagLayout AWT, Swing, JavaFX & SWT 11
W GridBagLayout mit fester Zellgrösse AWT, Swing, JavaFX & SWT 2
N Swing GridBagLayout: Ein Pixel Versatz AWT, Swing, JavaFX & SWT 2
B Swing Gridbaglayout unterschiedliche Zeilenhöhe AWT, Swing, JavaFX & SWT 6
H LayoutManager GridBagLayout AWT, Swing, JavaFX & SWT 1
N GridBagLayout - was fehlt? AWT, Swing, JavaFX & SWT 8
S Swing rowHeight und rowWeight im GridBagLayout AWT, Swing, JavaFX & SWT 1
N Swing GUI mit GridBagLayout AWT, Swing, JavaFX & SWT 4
A jpanel mit gridbaglayout auf hintergrundbild AWT, Swing, JavaFX & SWT 7
S GridBagLayout-Frage AWT, Swing, JavaFX & SWT 1
G GridBagLayout AWT, Swing, JavaFX & SWT 6
S GridBagLayout "links-rechts-layouten" AWT, Swing, JavaFX & SWT 7
T LayoutManager GridBagLayout / erwartetes Raster fehlt AWT, Swing, JavaFX & SWT 3
X Gridbaglayout gridx + gridy auslesen? AWT, Swing, JavaFX & SWT 7
H GridBagLayout macht Probleme... AWT, Swing, JavaFX & SWT 4
N GridBagLayout - Zeitplan AWT, Swing, JavaFX & SWT 13
N Swing GridbagLayout AWT, Swing, JavaFX & SWT 4
S Swing gridbaglayout AWT, Swing, JavaFX & SWT 8
Java-Insel LayoutManager Ein GridBagLayout-Objekt für mehrere Panels? AWT, Swing, JavaFX & SWT 2
X LayoutManager gridBagLayout wird nicht richtig Dargestellt AWT, Swing, JavaFX & SWT 5
das-mo Probleme mit GridBagLayout AWT, Swing, JavaFX & SWT 6
T LayoutManager GridBagLayout - zwei jTable mit unterschiedlicher Höhe AWT, Swing, JavaFX & SWT 2
N LayoutManager GridBagLayout schummeln erlaubt ? AWT, Swing, JavaFX & SWT 2
D GridBagLayout AWT, Swing, JavaFX & SWT 9
A Swing GridBagLayout - constraints.anchor scheint nicht korrekt zu funktionieren? AWT, Swing, JavaFX & SWT 7
J Swing Terminkalender Wochenansicht mit Gridbaglayout oder JTable AWT, Swing, JavaFX & SWT 16
C LayoutManager GridBagLayout - Anfängerfrage AWT, Swing, JavaFX & SWT 5
Asamandra LayoutManager GridBagLayout - Komponenten (mit fill?) vergrößern aber Proportionen dabei erhalten? AWT, Swing, JavaFX & SWT 3
R GridBagLayout in GridBagLayout AWT, Swing, JavaFX & SWT 2
H Positionierungsprobleme beim GridBagLayout AWT, Swing, JavaFX & SWT 16
Furtano AWT GridBagLayout macht mir Sorgen AWT, Swing, JavaFX & SWT 3
A GridbagLayout positionierungsproblem AWT, Swing, JavaFX & SWT 4
earlgrey_tea GridBagLayout Componenten proportional vergößern AWT, Swing, JavaFX & SWT 12
D JTable im GridBagLayout -> gridwidth AWT, Swing, JavaFX & SWT 6
D Probleme mit GridBagLayout AWT, Swing, JavaFX & SWT 8
J Swing GridBagLayout: Links-nach-rechts Orientierung statt zentriert AWT, Swing, JavaFX & SWT 12
R Größe eines Labels bei GridBagLayout festlegen AWT, Swing, JavaFX & SWT 9
M LayoutManager GridBagLayout AWT, Swing, JavaFX & SWT 11
E LayoutManager GridBagLayout in BorderLayout - Abstand bei Resizing AWT, Swing, JavaFX & SWT 2
Y LayoutManager Keine vollständige Darstellung der Tabelle mit GridBagLayout AWT, Swing, JavaFX & SWT 3
L LayoutManager GridBagLayout leere Zeilen AWT, Swing, JavaFX & SWT 4
H LayoutManager Layout mit GridBagLayout machbar? AWT, Swing, JavaFX & SWT 6
C Swing JTable "zerstört" GridBagLayout AWT, Swing, JavaFX & SWT 9
N LayoutManager GridBagLayout Größe fixieren AWT, Swing, JavaFX & SWT 3
M GridBagLayout AWT, Swing, JavaFX & SWT 7
V Swing Gridbaglayout Leeres Fenster AWT, Swing, JavaFX & SWT 2
R LayoutManager GridBagLayout Fragen AWT, Swing, JavaFX & SWT 10
P LayoutManager Verständnis-Frage GridBagLayout AWT, Swing, JavaFX & SWT 7
M LayoutManager Einige Fragen zum GridBagLayout AWT, Swing, JavaFX & SWT 13
N GridBagLayout AWT, Swing, JavaFX & SWT 11
Y Swing GridbagLayout JTextfield zu klein AWT, Swing, JavaFX & SWT 5
L LayoutManager GridBagLayout spielt verrückt AWT, Swing, JavaFX & SWT 9

Ähnliche Java Themen

Neue Themen


Oben