7 Replies - 2240 Views - Last Post: 09 April 2010 - 10:56 AM Rate Topic: -----

#1 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

How do I combine two classes to make a GUI?

Posted 29 March 2010 - 07:28 AM

hmm... I think I asked something like this last year, but that was for two different windows, and this time I want it to be within the same window.

So, BuildAssessmentWindow is supposed to have a space in it for adding another JPanel in the center of the BorderLayout.
I wanted that panel to be made in a different file. What am I supposed to do within that other file to make it work correctly? Right now it's giving me a bunch of errors.

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
//    imports for window components
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JComboBox;

public class QuestionBlock extends JFrame
{
    private JPanel container;
    private JPanel number;
    private JPanel questionBlock;
    private JPanel questionPanel;
    private JPanel answer;
    private JComboBox numberDropDown, answerDropDown;
    private JTextArea question;

    JPanel container = new JPanel();
    container.setLayout(new BorderLayout());

    JPanel number = new JPanel();
    numberDropDown = new JComboBox;
    numberDropDown.addItem("1.");
    numberDropDown.addItem("a.");
    number.add(numberDropDown);
    container.add(number, BorderLayout.LINE_START);

    JPanel questionBlock = new JPanel();
    questionBlock.setLayout(new BorderLayout());
    container.add(questionBlock, BorderLayout.CENTER);

    JPanel questionPanel = newJPanel();
    question = new JTextArea(4, 10);
    questionBlock.add(questionPanel, BorderLayout.PAGE_START);

    JPanel answer = new JPanel();
    answerDropDown = new JComboBox;
    answerDropDown.addItem("--Select--");
    questionBlock.add(answer, BorderLayout.PAGE_END);

    public JPanel getPanel()
    {
        return container;
    }
}


This is the main file:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
//    imports for window components
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
//    imports for listeners
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
//    imports for background colors
import java.awt.Color;
//import java.awt.Container;
//    imports for component alignments
import java.awt.Component;
// //    imports for text editability
//import javax.swing.text.StyledEditorKit;
// //    imports for component focus control
//import java.awt.KeyboardFocusManager;



public class BuildAssessmentWindow extends JFrame implements ActionListener, 

FocusListener
{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 350;

    private JPanel titlePanel;
    private JPanel titleTopPanel;
    private JPanel titleBottomPanel;
    private JPanel questionsPanel;
    private JPanel savePanel;

    private boolean changedTitle = false;

    private JLabel introLabel;
    private JTextArea leftExtra, centerExtra, rightExtra;
    private JTextField assessmentTitle;
    private JButton questionButton, saveButton;

    public JTextArea question;

    //private Container contentPane;

    public BuildAssessmentWindow()
    {
        super("Build Assessment");
        setSize(WIDTH, HEIGHT);

        getContentPane().setBackground(Color.WHITE);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        titlePanel = new JPanel();
        titlePanel.setBackground(Color.WHITE);
        titlePanel.setLayout(new GridLayout(2, 1));

        titleTopPanel = new JPanel();
        titleTopPanel.setBackground(Color.WHITE);
        titleTopPanel.setLayout(new FlowLayout());

        titleBottomPanel = new JPanel();
        titleBottomPanel.setBackground(Color.WHITE);
        titleBottomPanel.setLayout(new FlowLayout());

        leftExtra = new JTextArea(2, 10);
        leftExtra.setBackground(new Color(250, 250, 250));
//        leftExtra.setHorizontalAlignment(JTextArea.LEFT);
        leftExtra.setAlignmentX(Component.LEFT_ALIGNMENT);
        titleTopPanel.add(leftExtra);

        centerExtra = new JTextArea("type whatever you like here", 2, 10);
        centerExtra.setBackground(new Color(250, 250, 250));
//        centerExtra.setHorizontalAlignment(JTextArea.CENTER);
        centerExtra.addFocusListener(this);
        centerExtra.setAlignmentX(Component.CENTER_ALIGNMENT);
        titleTopPanel.add(centerExtra);

        rightExtra = new JTextArea(2, 10);
        rightExtra.setBackground(new Color(250, 250, 250));
//        rightExtra.setHorizontalAlignment(JTextArea.RIGHT);
        rightExtra.setAlignmentX(Component.RIGHT_ALIGNMENT);
        titleTopPanel.add(rightExtra);

        assessmentTitle = new JTextField("Assessment Title", 10);
        assessmentTitle.addFocusListener(this);

        titleBottomPanel.add(assessmentTitle);
        titlePanel.add(titleTopPanel);
        titlePanel.add(titleBottomPanel);
        add(titlePanel, BorderLayout.PAGE_START);

        savePanel = new JPanel();
        savePanel.setLayout(new FlowLayout());
        saveButton = new JButton("Save assessment");
        saveButton.addActionListener(this);
        savePanel.add(saveButton);
        add(savePanel, BorderLayout.PAGE_END);

//        requestFocusInWindow();
    }

    public static void main(String[] args)
    {
        BuildAssessmentWindow gui = new BuildAssessmentWindow();
 //       getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
        gui.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        getQandAPanel();
    }

    public void focusGained(FocusEvent e)
    {
        if(!changedTitle)
        {
            assessmentTitle.setText(""); 
            changedTitle=true;
        }
    }

    public void focusLost(FocusEvent e)
    {
    }

public void getQandAPanel()
{ 
    JPanel block = new JPanel
    QuestionBlock questionPanel = new QuestionBlock();
    block=questionPanel.getPanel();
    add(block, BorderLayout.CENTER);
//    JPanel questionPanel = new JPanel(); 
//    JPanel answerPanel = new JPanel(); 
//    JPanel questionBoxPanel = new JPanel(); 
//    question=new JTextArea(4, 10);
//    questionPanel.add(question);
    
//    questionsPanel.add(questionPanel); 
//    questionsPanel.add(answerPanel); 
//    questionsPanel.revalidate(); 
//    questionsPanel.repaint();
    
}

}


Is This A Good Question/Topic? 0
  • +

Replies To: How do I combine two classes to make a GUI?

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: How do I combine two classes to make a GUI?

Posted 29 March 2010 - 07:32 AM

So when the user clicks a button or something, you want everything in the first frame to disappear and be replaced by everything in the second frame?

Just a rule of thumb, you should only really have one class extending a JFrame per application. All other classes should extend a second tier container such as a JPanel. If you do this, then its an easy thing to sort out for you.
Was This Post Helpful? 1
  • +
  • -

#3 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: How do I combine two classes to make a GUI?

Posted 29 March 2010 - 07:39 AM

Well, I was wanting to insert a JPanel into the middle of the window, but I wanted the user to tell how many to have. So when they press a button, a new one appears under the old one. (I probably didn't put a containing JPanel into the layout yet... I'd need that)

Edit: I changed it to JPanel, added the "public QuestionBlock()" part that I'd forgotten, and now it seems to work, except that "QuestionBlock questionPanel = new QuestionBlock();" is giving me an error in the main program. What's wrong with it? It's in the same folder...

This post has been edited by asj127: 29 March 2010 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: How do I combine two classes to make a GUI?

Posted 29 March 2010 - 07:54 AM

What error are you getting? You are doing this differently to how i would do it.

Let me go eat and then I will take a proper look at this.

This post has been edited by nick2price: 29 March 2010 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

#5 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: How do I combine two classes to make a GUI?

Posted 29 March 2010 - 08:07 AM

This is what I have for the QuestionBlock class now. The other class is the same as I posted earlier. This class doesn't give me any errors now, but the main one says there's one error.
QuestionBlock questionPanel = new QuestionBlock();
There's an arrow pointing to the first Q. It says a ( or [ is expected.

How would you do it? I'm just trying to get something to work right now, but I want it to be understandable by the time I'm finished. Kind'a doesn't help that I learned about Java about a year ago, in a month, and don't remember much... But, what I did wasn't exactly like this.

import javax.swing.JPanel;
import java.awt.BorderLayout;
//    imports for window components
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JComboBox;

public class QuestionBlock extends JPanel
{
    private JPanel container;
    private JPanel number;
    private JPanel questionBlock;
    private JPanel questionPanel;
    private JPanel answer;
    private JComboBox numberDropDown, answerDropDown;
    private JTextArea question;

    public QuestionBlock()
    {
        JPanel container = new JPanel();
        container.setLayout(new BorderLayout());

        JPanel number = new JPanel();
        numberDropDown = new JComboBox();
        numberDropDown.addItem("1.");
        numberDropDown.addItem("a.");
        number.add(numberDropDown);
        container.add(number, BorderLayout.LINE_START);

        JPanel questionBlock = new JPanel();
        questionBlock.setLayout(new BorderLayout());
        container.add(questionBlock, BorderLayout.CENTER);

        JPanel questionPanel = new JPanel();
        question = new JTextArea(4, 10);
        questionBlock.add(questionPanel, BorderLayout.PAGE_START);

        JPanel answer = new JPanel();
        answerDropDown = new JComboBox();
        answerDropDown.addItem("--Select--");
        questionBlock.add(answer, BorderLayout.PAGE_END);
    }

    public JPanel getPanel()
    {
        return container;
    }
}

Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: How do I combine two classes to make a GUI?

Posted 29 March 2010 - 08:24 AM

Ok. You had a few errors that needed fixing. First of all, I removed your focus listeners, so you will have to add these back.
I changed QuestionBlock to be a JPanel. A problem you were having here is you were trying to do everything in the class scope, and not within a method. So I gave it a constructor which calls up a method with all your work in.
I then changed the way you call up the panel in the JFrame class. Take a look and you should see what has changed. You can see I set the panel to be black, so you can see it works when you type something.

import javax.swing.*;
import java.awt.*; 
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.*;

public class QuestionBlock extends JPanel
{
    private JPanel container;
    private JPanel number;
    private JPanel questionBlock;
    private JPanel questionPanel;
    private JPanel answer;
    private JComboBox numberDropDown, answerDropDown;
    private JTextArea question;

    public QuestionBlock() {
		super(new GridLayout(1, 1));
		
		setLocation(new Point(0, 0));  
		setVisible(true); 			
		setSize(800, 610);			
        setBackground(new Color(0, 0, 0)); 
		createGUI();
	}
	
	public void createGUI(){
	
    JPanel container = new JPanel();
    container.setLayout(new BorderLayout());

    JPanel number = new JPanel();
    numberDropDown = new JComboBox();
    numberDropDown.addItem("1.");
    numberDropDown.addItem("a.");
    number.add(numberDropDown);
    container.add(number, BorderLayout.LINE_START);

    JPanel questionBlock = new JPanel();
    questionBlock.setLayout(new BorderLayout());
    container.add(questionBlock, BorderLayout.CENTER);

    JPanel questionPanel = new JPanel();
    question = new JTextArea(4, 10);
    questionBlock.add(questionPanel, BorderLayout.PAGE_START);

    JPanel answer = new JPanel();
    answerDropDown = new JComboBox();
    answerDropDown.addItem("--Select--");
    questionBlock.add(answer, BorderLayout.PAGE_END);
    }
}


import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
//    imports for window components
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
//    imports for listeners
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
//    imports for background colors
import java.awt.Color;
//import java.awt.Container;
//    imports for component alignments
import java.awt.Component;
// //    imports for text editability
//import javax.swing.text.StyledEditorKit;
// //    imports for component focus control
//import java.awt.KeyboardFocusManager;



public class BuildAssessmentWindow extends JFrame implements ActionListener 

{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 350;

    private JPanel titlePanel;
    private JPanel titleTopPanel;
    private JPanel titleBottomPanel;
    private JPanel questionsPanel;
    private JPanel savePanel;

    private boolean changedTitle = false;

    private JLabel introLabel;
    private JTextArea leftExtra, centerExtra, rightExtra;
    private JTextField assessmentTitle;
    private JButton questionButton, saveButton;

    public JTextArea question;

    //private Container contentPane;

    public BuildAssessmentWindow()
    {
        super("Build Assessment");
        setSize(WIDTH, HEIGHT);

        getContentPane().setBackground(Color.WHITE);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        titlePanel = new JPanel();
        titlePanel.setBackground(Color.WHITE);
        titlePanel.setLayout(new GridLayout(2, 1));

        titleTopPanel = new JPanel();
        titleTopPanel.setBackground(Color.WHITE);
        titleTopPanel.setLayout(new FlowLayout());

        titleBottomPanel = new JPanel();
        titleBottomPanel.setBackground(Color.WHITE);
        titleBottomPanel.setLayout(new FlowLayout());

        leftExtra = new JTextArea(2, 10);
        leftExtra.setBackground(new Color(250, 250, 250));
//        leftExtra.setHorizontalAlignment(JTextArea.LEFT);
        leftExtra.setAlignmentX(Component.LEFT_ALIGNMENT);
        titleTopPanel.add(leftExtra);

        centerExtra = new JTextArea("type whatever you like here", 2, 10);
        centerExtra.setBackground(new Color(250, 250, 250));
//        centerExtra.setHorizontalAlignment(JTextArea.CENTER);

        centerExtra.setAlignmentX(Component.CENTER_ALIGNMENT);
        titleTopPanel.add(centerExtra);

        rightExtra = new JTextArea(2, 10);
        rightExtra.setBackground(new Color(250, 250, 250));
//        rightExtra.setHorizontalAlignment(JTextArea.RIGHT);
        rightExtra.setAlignmentX(Component.RIGHT_ALIGNMENT);
        titleTopPanel.add(rightExtra);

        assessmentTitle = new JTextField("Assessment Title", 10);


        titleBottomPanel.add(assessmentTitle);
        titlePanel.add(titleTopPanel);
        titlePanel.add(titleBottomPanel);
        add(titlePanel, BorderLayout.PAGE_START);

        savePanel = new JPanel();
        savePanel.setLayout(new FlowLayout());
        saveButton = new JButton("Save assessment");
        saveButton.addActionListener(this);
        savePanel.add(saveButton);
        add(savePanel, BorderLayout.PAGE_END);

//        requestFocusInWindow();
    }

    public static void main(String[] args)
    {
        BuildAssessmentWindow gui = new BuildAssessmentWindow();
 //       getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
        gui.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        getQandAPanel();
    }

public void getQandAPanel()
{ 
    add(new QuestionBlock(), BorderLayout.CENTER);
//    JPanel questionPanel = new JPanel(); 
//    JPanel answerPanel = new JPanel(); 
//    JPanel questionBoxPanel = new JPanel(); 
//    question=new JTextArea(4, 10);
//    questionPanel.add(question);
    
//    questionsPanel.add(questionPanel); 
//    questionsPanel.add(answerPanel); 
//    questionsPanel.revalidate(); 
//    questionsPanel.repaint();
    
}

}

Was This Post Helpful? 0
  • +
  • -

#7 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: How do I combine two classes to make a GUI?

Posted 08 April 2010 - 07:34 AM

Can I please know why I can't get it to work? The little dropdown menu that lets you pick between 1. and a. comes up when the window's resized, but nothing else comes up. And I can't get more than one of those to come up.
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.*;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
//    imports for window components
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JComboBox;

public class QuestionBlock extends JPanel
{
    private JPanel container;
    private JPanel number;
    private JPanel questionBlock;
    private JPanel questionPanel;
    private JPanel answer;
    private JComboBox numberDropDown, answerDropDown;
    private JTextArea question;

    public QuestionBlock()
    {

        super(new FlowLayout());
        //setLocation(new Point(0, 0));
        setVisible(true);
        //setSize(800, 610);
        //setBackground(new Color(0, 0, 0));
        createGUI();
    }

    public void createGUI()
    {

        JPanel container = new JPanel();
        container.setLayout(new BorderLayout());

        JPanel number = new JPanel();
        numberDropDown = new JComboBox();
        numberDropDown.addItem("1.");
        numberDropDown.addItem("a.");
        number.add(numberDropDown);
        //container.add(number, BorderLayout.LINE_START);
        container.add(number, BorderLayout.LINE_START);

        JPanel questionBlock = new JPanel();
        questionBlock.setLayout(new BorderLayout());
        container.add(questionBlock, BorderLayout.CENTER);

        JPanel questionPanel = new JPanel();
        question = new JTextArea(4, 10);
        questionBlock.add(questionPanel, BorderLayout.PAGE_START);

        JPanel answer = new JPanel();
        answerDropDown = new JComboBox();
        answerDropDown.addItem("--Select--");
        questionBlock.add(answer, BorderLayout.PAGE_END);
        add(container);
    }
}

Was This Post Helpful? 0
  • +
  • -

#8 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: How do I combine two classes to make a GUI?

Posted 09 April 2010 - 10:56 AM

I wanted to set all the JPanels' backgrounds to white, so I tried making a new class to do that. It works except for a gray ring around the outside of each QuestionBlock. Anyone know why that's there?


BuildAssessmentWindow class:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
//    imports for window components
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
//    imports for listeners
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
//    imports for background colors
import java.awt.Color;
//import java.awt.Container;
//    imports for component alignments
import java.awt.Component;
// //    imports for text editability
//import javax.swing.text.StyledEditorKit;
// //    imports for component focus control
//import java.awt.KeyboardFocusManager;



public class BuildAssessmentWindow extends JFrame implements ActionListener, FocusListener
{
    public static final int WIDTH = 400;
    public static final int HEIGHT = 350;

    private JPanel headersPanel; //contains extraInfoPanel and title
    private JPanel extraInfoPanel;
    private JPanel questionContainerPanel;
    private JPanel innerQuestionContainerPanel;

    private boolean changedTitle = false;

    //private JLabel introLabel;
    private JTextArea leftExtra, centerExtra, rightExtra; //similates left-, right-, and center-align
    private JTextField assessmentTitle;
    private JButton questionButton, saveButton;
    private JPanelTools panelTools;

    public BuildAssessmentWindow()
    {
        super("Build Assessment");
        setSize(WIDTH, HEIGHT);

        panelTools = new JPanelTools();

        getContentPane().setBackground(Color.WHITE);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        headersPanel = panelTools.addNewJPanel();
        headersPanel.setLayout(new GridLayout(2, 1)); //contains two JPanels

        extraInfoPanel = panelTools.addNewJPanel();
        extraInfoPanel.setLayout(new FlowLayout());

        leftExtra = new JTextArea(2, 10);
        leftExtra.setBackground(new Color(250, 250, 250)); //light gray color for noticeability
//        leftExtra.setHorizontalAlignment(JTextArea.LEFT);
        leftExtra.setAlignmentX(Component.LEFT_ALIGNMENT);
        extraInfoPanel.add(leftExtra);

        centerExtra = new JTextArea("type whatever you like here", 2, 10);
        centerExtra.setBackground(new Color(250, 250, 250)); //light gray color for noticeability
//        centerExtra.setHorizontalAlignment(JTextArea.CENTER);
        centerExtra.addFocusListener(this);
        centerExtra.setAlignmentX(Component.CENTER_ALIGNMENT);
        extraInfoPanel.add(centerExtra);

        rightExtra = new JTextArea(2, 10);
        rightExtra.setBackground(new Color(250, 250, 250)); //light gray color for noticeability
//        rightExtra.setHorizontalAlignment(JTextArea.RIGHT);
        rightExtra.setAlignmentX(Component.RIGHT_ALIGNMENT);
        extraInfoPanel.add(rightExtra);

        assessmentTitle = new JTextField("Assessment Title", 10);
        assessmentTitle.addFocusListener(this);

        headersPanel.add(extraInfoPanel);
        headersPanel.add(panelTools.addThisComponent(assessmentTitle));
        add(headersPanel, BorderLayout.PAGE_START);

        questionContainerPanel = panelTools.addNewJPanel();
        questionContainerPanel.setLayout(new BorderLayout());

        innerQuestionContainerPanel = panelTools.addNewJPanel();
        innerQuestionContainerPanel.setLayout(new FlowLayout());
        innerQuestionContainerPanel.add(new QuestionBlock());
        questionContainerPanel.add(innerQuestionContainerPanel, BorderLayout.CENTER);
        questionButton = new JButton("Add new question block");
        questionButton.addActionListener(this);
        questionContainerPanel.add(panelTools.addThisComponent(questionButton), BorderLayout.PAGE_END);
        add(questionContainerPanel, BorderLayout.CENTER);

        saveButton = new JButton("Save assessment");
        saveButton.addActionListener(this);
        add(panelTools.addThisComponent(saveButton), BorderLayout.PAGE_END);

//        requestFocusInWindow();
    }

    public static void main(String[] args)
    {
        BuildAssessmentWindow gui = new BuildAssessmentWindow();
 //       getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
        gui.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        getQandAPanel();
    }

    public void focusGained(FocusEvent e)
    {
        if(!changedTitle)
        {
            assessmentTitle.setText(""); 
            changedTitle=true;
        }
    }

    public void focusLost(FocusEvent e)
    {
    }

    public void getQandAPanel()
    { 
        innerQuestionContainerPanel.add(new QuestionBlock());
        innerQuestionContainerPanel.revalidate();
        innerQuestionContainerPanel.repaint();
    }

}


QuestionBlock class:
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.*;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
//    imports for window components
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
//    imports for background colors
import java.awt.Color;

public class QuestionBlock extends JPanel
{
    private JPanel container;
    private JPanel questionsBlock;
    private JComboBox numberDropDown, answerDropDown;
    private JTextArea question;
    private JPanelTools panelTools;

    public QuestionBlock()
    {

        super(new FlowLayout());
        //setLocation(new Point(0, 0));
        setVisible(true);
        //setSize(800, 610);
        //setBackground(new Color(0, 0, 0));
        createGUI();
    }

    public void createGUI()
    {
        panelTools = new JPanelTools();

        container = panelTools.addNewJPanel();
        container.setLayout(new BorderLayout());

        numberDropDown = new JComboBox();
        numberDropDown.addItem("1.");
        numberDropDown.addItem("a.");
        container.add(panelTools.addThisComponent(numberDropDown));

        questionsBlock = panelTools.addNewJPanel();
        questionsBlock.setLayout(new BorderLayout());

        question = new JTextArea(4, 30);
        question.setBackground(new Color(250, 250, 250)); //light gray color for noticeability
        questionsBlock.add(panelTools.addThisComponent(question), BorderLayout.PAGE_START);

        answerDropDown = new JComboBox();
        answerDropDown.addItem("--Select--");
        answerDropDown.addItem("SubQuestion");
        questionsBlock.add(panelTools.addThisComponent(answerDropDown), BorderLayout.PAGE_END);

        container.add(questionsBlock, BorderLayout.CENTER);
        add(container);
    }
}


JPanelTools class:
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.Component;

public class JPanelTools extends JPanel
{
    private JPanel tempPanel;

    public JPanelTools()
    {
        setVisible(true);
    }

    public JPanel addNewJPanel()
    {
        tempPanel = new JPanel();
        tempPanel.setBackground(Color.WHITE);
        return tempPanel;
    }

    public JPanel addThisComponent(Component compon)
    {
        tempPanel = addNewJPanel();
        tempPanel.setLayout(new FlowLayout());
        tempPanel.add(compon);
        return tempPanel;
    }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1