Swing Probleme mit MigLayout

sirbender

Top Contributor
Hallo,

ich habe eine JList in einer JScrollpane, ein Button-Panel und eine JTextArea in einer JScrollPane.

Alles ist vertical durch das MigLayout untereinander angeordnet. Ich will am liebsten nur mit Component-Constraints arbeiten, da ich im DEBUG Modus ein JLabel einfuegen will. Dadurch wuerden im Row-Column-Constraints nicht mehr stimmen.

Mein Problem: JList und JTextArea sollen immer die gleiche Groesse haben egal welche Dimensionen das Fenster hat. Leider klappt es nicht wie ich es gemacht habe.

Java:
package main;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import net.miginfocom.layout.CC;
import net.miginfocom.layout.LC;
import net.miginfocom.swing.MigLayout;

public class MigExample {
   
    private static final boolean DEBUG = false;

    private static final Dimension PREFERRED_SIZE = new Dimension(600, 800);

    public static void main(String[] args) {
        JTextArea textArea = new JTextArea();
        textArea.setLineWrap(true);
       
        DefaultListModel<String> model = new DefaultListModel<>();
        JList<String> list = new JList<>(model);
        list.setFont(new Font("monospaced", Font.PLAIN, 14));
       
        for (int i = 0; i < 100; i++) {
            String element = "Element #" + i + "\n";
            model.addElement(element);
            textArea.append(element);
        }
       
        LC lc = new LC().insets("15");
        if (DEBUG) lc.debug();
        JPanel cp = new JPanel(new MigLayout(lc));
        if (DEBUG) {
            WatchDogTask.DEBUG = true;
            JLabel debugInfo = new JLabel("<html><font color='red'>[DEBUG MODE!]</font></html>");
            cp.add(debugInfo, new CC().alignX("center").wrap("15"));
        }
        cp.add(new JScrollPane(list), new CC().push(10f, 50f).grow().wrap());
        cp.add(GuiUtils.getButtonsPanel(getbutton("+"), getbutton("-"), getbutton("paste"), getbutton("exit")),
                new CC().alignX("center").wrap());
        cp.add(new JScrollPane(textArea), new CC().newline().push(10f, 50f).grow().wrap());
       
        getJFrame(cp);
    }
   
    private static JButton getbutton(String text) {
        return new JButton(text);
    }

    static JPanel getButtonsPanel(JButton... buttons) {
        JPanel buttonpanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 12, 8));
        for (JButton button : buttons) buttonpanel.add(button);
        return buttonpanel;
    }
   
    static JFrame getJFrame(JPanel cp) {
        JFrame frame = new JFrame("Watch List");
        frame.setContentPane(cp);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(PREFERRED_SIZE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        return frame;
    }
}
 
Java:
        LC lc = new LC().insets("15");
        AC rc = new AC().sizeGroup();
        JPanel cp = new JPanel(new MigLayout(lc, null, rc));
        cp.add(new JScrollPane(list), new CC().push(10f, 50f).grow().wrap());
        cp.add(new JScrollPane(textArea), new CC().push(10f, 50f).grow());
 

Neue Themen


Zurück
Oben