Question

I have been playing with DesignGridLayout as a layout manager. For the most part it works rather well. However, I have an issue in this sample class:

import net.java.dev.designgridlayout.DesignGridLayout;

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

public class ConsolePanel extends JPanel {
    private ConButton oneCon = new ConButton(">");
    private ConButton twoCon = new ConButton(">");
    private ConButton threeCon = new ConButton(">");
    private ConButton fourCon = new ConButton("<");
    private ConButton fiveCon = new ConButton("<");
    private ConButton sixCon = new ConButton("<");
    private JTextArea console = new JTextArea(10,20);

    private Dimension dim = new Dimension(430,250);

    private DesignGridLayout layout = new DesignGridLayout(this);

    public ConsolePanel () {
        buildConsole();

    }

    private void buildConsole () {

        layout.row().grid().add(oneCon).add(console,3).add(fourCon);
        layout.row().grid().add(twoCon).spanRow().add(fiveCon);
        layout.row().grid().add(threeCon).spanRow().add(sixCon);

        console.setBackground(Color.BLACK);
        console.setForeground(Color.GREEN);
        console.setColumns(20);
        console.setRows(10);
        console.setLineWrap(true);
        console.setWrapStyleWord(true);
        console.setEditable(false);
    }

    @Override
    public Dimension getPreferredSize() {
        // comply to contract if set
        if(isPreferredSizeSet())
            return super.getPreferredSize();
        // do whatever we want
        return new Dimension(dim);
    }

    public JPanel getConsoleLayout() {
        return this;
    }

    private class ConButton extends JButton {
        private Dimension dim = new Dimension(10,25);

        public ConButton (String text) {
            super(text);
        }

        @Override
        public Dimension getPreferredSize() {
            // comply to contract if set
            if(isPreferredSizeSet())
                return super.getPreferredSize();
            // do whatever we want
            return new Dimension(dim);
        }
    }
}

I need to be able to resize the "<" and ">" to some smaller size. Then what the layout manager defaults to. I have tried to override the getPreferredSize() method as noted here #21052894. Though, this isn't work quite right.

Also, this is a second JPanel inside of a JFrame/JPanel such as

--------------------------------------------- <-- JPanel inside of a JFrame | | | JPanel2 | JPanel3 | | | one I can't get | | | right |

And the JFrame/JPanel is setup as follows:

_frame = new JFrame();
_frame.setName(getClass().getSimpleName());
_frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


Font titleFont = new Font(title.getFont().getName(), title.getFont().getStyle(), 16);
title.setFont(titleFont);

JPanel top = new JPanel();
top.setName("TOP");
top.setLayout(new BorderLayout());
top.add(title,BorderLayout.PAGE_START);
top.add(<Panel2>, BorderLayout.LINE_START);
top.add(<Panel3>, BorderLayout.LINE_END);
addTopPanel(top);
prePack();
_frame.pack();
_frame.setLocationRelativeTo(null);
_frame.setResizable(false);
_frame.setVisible(true);
Was it helpful?

Solution

This doesn't seem to work for me. I changed from JTextArea to JTextPane and it's using the proper layout now. Here's the current code:

import net.java.dev.designgridlayout.DesignGridLayout;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

public class ConsolePanel extends JPanel {
    private JButton oneCon = new JButton(">");
    private JButton twoCon = new JButton(">");
    private JButton threeCon = new JButton(">");
    private JButton fourCon = new JButton("<");
    private JButton fiveCon = new JButton("<");
    private JButton sixCon = new JButton("<");
    private ConJTextPane console = new ConJTextPane();

    private Dimension dim = new Dimension(430,250);

    private DesignGridLayout layout = new DesignGridLayout(this);
    private AbstractDocument doc;

    public ConsolePanel () {
        buildConsole();

    }

    private void buildConsole () {
        StyledDocument styledDoc = console.getStyledDocument();
        if (styledDoc instanceof AbstractDocument) {
            doc = (AbstractDocument)styledDoc;
            doc.setDocumentFilter(new DocumentSizeFilter());
        }

        layout.row().grid().add(oneCon).add(console,3).add(fourCon);
        layout.row().grid().add(twoCon).spanRow().add(fiveCon);
        layout.row().grid().add(threeCon).spanRow().add(sixCon);

        console.setBackground(Color.BLACK);
        console.setForeground(Color.GREEN);
        console.setEditable(false);
    }

    @Override
    public Dimension getPreferredSize() {
        // comply to contract if set
        if(isPreferredSizeSet())
            return super.getPreferredSize();
        // do whatever we want
        return new Dimension(dim);
    }

    public JPanel getConsoleLayout() {
        return this;
    }

    private class ConJTextPane extends JTextPane {
        private Dimension dim = new Dimension(120,100);

        public ConJTextPane () { super(); }

        @Override
        public Dimension getPreferredSize() {
            // comply to contract if set
            if(isPreferredSizeSet())
                return super.getPreferredSize();
            // do whatever we want
            return new Dimension(dim);
        }

    }
    private class DocumentSizeFilter extends DocumentFilter {
        public DocumentSizeFilter() {

        }

        public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {
            System.out.println(str);
            if (str.equals("y")) {
                System.out.println("You have pressed y.");
            }
        }

        public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)  throws BadLocationException {

        }
    }
}

Also, removed the custom JButtons because I didn't need them any more. Not sure why the JTextArea just doesn't format properly using this layout-manager.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top