1

So I just found the Netbeans JFrame Form referenced in an online tutorial to help with layout (my textbook hadn't made mention of it in the GUI section, at least that I've seen!) Since I'm having a difficult time with the layout of the program I'm working on (I can't get the text area to behave itself and stay in the center of the window instead of taking up the entire window!) I thought a visual aid might be helpful, however, as you might have guessed I already have a large amount of code sunk into this program. Is it possible to link a new JFrame Form with an existing class? If so, how would I go about doing it? I can provide my code if you need it, but we're talking 500 lines of code in just one of the main three classes.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package theproblem;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.TextArea;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
*
* @author Heather
*/
public class TheProblem {

/**
 * @param args the command line arguments
 */

public static void main(String[] args) {

    JFrame window2 = new JFrame();
    TextArea battleLogging = new TextArea(3,10);
    JScrollPane logScrollPane = new JScrollPane(battleLogging);
    JLabel BattleLog = new JLabel();
    JLabel p1HPLabel= new JLabel();
    JLabel p2HPLabel= new JLabel();
    String attack1ButtonContents = "Just an attack";
    String attack2ButtonContents = "Just another attack";
    JButton attack1=new JButton(attack1ButtonContents);
    JButton attack2=new JButton(attack2ButtonContents);


    window2.setLayout(new BorderLayout());
    window2.setSize(400,400);
    JPanel attackPanel = new JPanel();
    attackPanel.add(attack1);
    attackPanel.add(attack2);
//        attack1 = new JButton(p1A1);
//        attack2 = new JButton(p1A2);
//        attack1.addActionListener(new Attack1());
//        attack2.addActionListener(new Attack2());
    //window2.add(attackPanel, BorderLayout.CENTER);
    window2.add(battleLogging, BorderLayout.CENTER);
    battleLogging.setEditable(false);
    logScrollPane.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    logScrollPane.setPreferredSize(new Dimension(50, 50));
    //battleLogging.setLineWrap(true);
    //battleLogging.setWrapStyleWord(true);
    window2.add(BattleLog, BorderLayout.NORTH);
    window2.add(p1HPLabel, BorderLayout.WEST);
    window2.add(p2HPLabel, BorderLayout.EAST);
    window2.setVisible(true);
    window2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Heather T
  • 323
  • 1
  • 10
  • 20
  • Trying to use a gui builder with existing code is going to be way more trouble than it's worth, IMHO. Why don't you just write the code yourself? – Kevin Workman Jan 09 '14 at 14:36
  • Make a [SSCCE](http://sscce.org) demonstrating your problem. – nachokk Jan 09 '14 at 14:41
  • I did, and then tried to add a JTextArea to contain a log of occurrences visible on the window, and it doesn't stay in the center of the BorderLayout I had specified. It takes up the entire window to where one cannot see any buttons nor the JLabel at the top – Heather T Jan 09 '14 at 14:41
  • I insist post a [SSCCE](http://sscce.org) demonstrating your problem, then it will be easy for answerers, more test less guessing. – nachokk Jan 09 '14 at 14:50
  • I'm working on it, but it's not the easiest thing in the world to figure out. I had to click on the link to find out what an SSCCE was. – Heather T Jan 09 '14 at 15:01
  • easy, a short compilable example demonstrating only the issue you have, where you only have to copy paste in an editor to see what it's happening – nachokk Jan 09 '14 at 15:12
  • There. Think I got it narrowed down. Editing to add it now. – Heather T Jan 09 '14 at 15:14
  • @HeatherT *"window2 is just a name."* Just a very poor one, given it is in no way a `java.awt.Window` or an extension of one. Attribute names should be meaningful, rather than misleading. :-( – Andrew Thompson Jan 09 '14 at 15:54
  • @Andrew Thompson - My apologies, I'm certainly not trying to be tricky. I pulled the majority of code for the SSCCE straight from the group project code. One partner out of my group and I are still annoyed it wasn't working right and we've been trying to get it up and running but all of our GUI information is self taught from the internet and the book. Jframes, when they pop up, look like windows, so that's what we named it after. – Heather T Jan 09 '14 at 15:58

1 Answers1

2

To answer your question directly, you can create a JFrame (actually a custom class extending JFrame) and design it in your Netbeans visual designer, then instantiate it in your existing class. You can use composition (http://en.wikipedia.org/wiki/Object_composition) and have a reference to the JFrame as a field in your existing class. You can provide additional methods in your JFrame to pass data to it.

rhobincu
  • 906
  • 1
  • 7
  • 22