5 Replies - 329 Views - Last Post: 07 February 2013 - 07:22 PM Rate Topic: -----

#1 ILoveJava  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Help with layout.

Posted 07 February 2013 - 06:37 AM

Hey guys, I'm creating a Text to HTML generator, but before I get too deep into it, I want the layout sorted. But I have no idea on what layout to use on this. What would you suggest? I want the JTextArea for Basic to be on it's own row, then buttons all on the same, below it. Then for the advanced, I want a JLabel & JTextField pair on the same row, then JTextArea by itself, then all buttons on the same row.

One last question, would I be able to use just 3 buttons for both tabs? Or would I need to make new ones for ActionListener purposes?

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

public class Main1 {
    final static String BUTTONPANEL = "Basic";
    final static String TEXTPANEL = "Advanced";
    
    JButton gen = new JButton("Generate");
    JButton clear = new JButton("Clear");
    JButton exit = new JButton("Exit");
    
    JTextArea text = new JTextArea(20, 40);
    JTextArea advText = new JTextArea(20, 40);
    
    JTextField header = new JTextField(20);
    JTextField footer = new JTextField(20);
    

    public void addComponentToPane(Container pane) {
        JTabbedPane tabbedPane = new JTabbedPane();

        JPanel basic = new JPanel();
        basic.add(text);
        basic.add(gen);
        	gen.addActionListener(new GenerateListener());
        basic.add(clear);
        	clear.addActionListener(new ClearListener());
        basic.add(exit);
        	exit.addActionListener(new ExitListener());

        JPanel adv = new JPanel();
        adv.add(header);
        adv.add(footer);
        adv.add(advText);

        tabbedPane.addTab(BUTTONPANEL, basic);
        tabbedPane.addTab(TEXTPANEL, adv);

        pane.add(tabbedPane, BorderLayout.CENTER);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Text to HTML Generater");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Main1 html = new Main1();
        html.addComponentToPane(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
    }
    
    private class GenerateListener implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		// Generate .html file.
    	}
    }
    
    private class ClearListener implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		// Clear text area.
    	}
    }
    
    private class ExitListener implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		System.exit(0);
    	}
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Help with layout.

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Help with layout.

Posted 07 February 2013 - 07:26 AM

I would consider GridBagLayout for what you described. Just draw yourself a picture of what you want in each row, and add them to the rows. You can span more then one column for text fields and such, and make the rows for the text areas span wide.

As far as the buttons go, as long as they're going to do the same thing you don't need to make a second set, but if they're going to act on a different textarea, you can write your listener classes to accept a text area in the constructor, and create 2 sets of buttons with different text area parameters.
Was This Post Helpful? 0
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 862
  • Posts: 2,651
  • Joined: 29-July 11

Re: Help with layout.

Posted 07 February 2013 - 03:47 PM

I suggest you use JavaFX with a gridLayout, called a GridPane...I am serious. :)

This post has been edited by farrell2k: 07 February 2013 - 03:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 ILoveJava  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Re: Help with layout.

Posted 07 February 2013 - 05:31 PM

Thanks for the suggestion, I decided to put it into effect and added all the code for the GridBagLayout, but now I just keep getting an Illegal exception. My code & error are as below if anyone can help.
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint
	at java.awt.GridBagLayout.addLayoutComponent(GridBagLayout.java:702)
	at java.awt.Container.addImpl(Container.java:1120)
	at java.awt.Container.add(Container.java:966)
	at Main1.addComponentToPane(Main1.java:79)
	at Main1.createAndShowGUI(Main1.java:87)
	at Main1.access$300(Main1.java:5)
	at Main1$1.run(Main1.java:114)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
	at java.awt.EventQueue.access$200(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:682)
	at java.awt.EventQueue$3.run(EventQueue.java:680)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

Process completed.

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

public class Main1 {
    final static String BUTTONPANEL = "Basic";
    final static String TEXTPANEL = "Advanced";
    
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;
    
    JButton gen = new JButton("Generate");
    JButton clear = new JButton("Clear");
    JButton exit = new JButton("Exit");
    
    JTextArea text = new JTextArea(20, 40);
    JTextArea advText = new JTextArea(20, 40);
    
    JTextField header = new JTextField(20);
    JTextField footer = new JTextField(20);
    

    public void addComponentToPane(Container pane) {
    	if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }
        
        JTabbedPane tabbedPane = new JTabbedPane();
        
        pane.setLayout(new GridBagLayout());
    	GridBagConstraints c = new GridBagConstraints();
    	
    	if (shouldFill) {
    		c.fill = GridBagConstraints.HORIZONTAL;
    	}

        JPanel basic = new JPanel();
        basic.add(text);
            c.fill = GridBagConstraints.HORIZONTAL;
		    c.weightx = 0.0;
		    c.gridwidth = 3;
		    c.gridx = 0;
		    c.gridy = 1;
		    tabbedPane.add(text, c);
        basic.add(gen);
        	gen.addActionListener(new GenerateListener());
        	c.fill = GridBagConstraints.HORIZONTAL;
		    c.weightx = 0.0;
		    c.gridwidth = 0;
		    c.gridx = 0;
		    c.gridy = 2;
		    tabbedPane.add(gen, c);
        basic.add(clear);
        	clear.addActionListener(new ClearListener());
        	c.fill = GridBagConstraints.HORIZONTAL;
		    c.weightx = 0.0;
		    c.gridwidth = 0;
		    c.gridx = 1;
		    c.gridy = 2;
		    tabbedPane.add(clear, c);
        basic.add(exit);
        	exit.addActionListener(new ExitListener());
        	c.fill = GridBagConstraints.HORIZONTAL;
		    c.weightx = 0.0;
		    c.gridwidth = 0;
		    c.gridx = 2;
		    c.gridy = 2;
		    tabbedPane.add(exit, c);

        JPanel adv = new JPanel();
        adv.add(header);
        adv.add(footer);
        adv.add(advText);

        tabbedPane.addTab(BUTTONPANEL, basic);
        tabbedPane.addTab(TEXTPANEL, adv);

        pane.add(tabbedPane, BorderLayout.CENTER);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Text to HTML Generater");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Main1 html = new Main1();
        html.addComponentToPane(frame.getContentPane());

        frame.pack();
        frame.setVisible(true);
    }
    
    private class GenerateListener implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		// Generate .html file.
    	}
    }
    
    private class ClearListener implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		// Clear text area.
    	}
    }
    
    private class ExitListener implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		System.exit(0);
    	}
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Was This Post Helpful? 0
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 862
  • Posts: 2,651
  • Joined: 29-July 11

Re: Help with layout.

Posted 07 February 2013 - 05:32 PM

JavaFX and a GridPane would be so much easier...
Was This Post Helpful? 0
  • +
  • -

#6 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Help with layout.

Posted 07 February 2013 - 07:22 PM

You are adding your components to the local scope JPanel "basic", which is using the default layout. Either add them directly to the content pane, or make basic the one with the GridBagLayout, and just use the default for the content pane, which is BorderLayout.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1