Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using GroupLayout

 
Brett Wiggins
Greenhorn
Posts: 20
IntelliJ IDE Mac OS X MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

This is my first post to these forums. I am having some trouble understanding the GroupLayout class (javax.swing.GroupLayout).
I have defined a Class InputOutputPanel that extends JPanel which contains code for the object and a main method that creates
an instance of InputOutputPanel and then adds the panel to a JFrame. My problem is I want to use GroupLayout but am not sure
where to put the code.

Should I use GroupLayout instead of using JFrame in the main method?


thanks,

synack.

Below is the code I have written.


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


public class InputOutputPanel extends JPanel{

private JLabel inputLabel;
private JTextField input;
private String line;
private JTextArea output;
private JScrollPane jScrollPane1;

public static void main(String[] args){
JFrame frame = new JFrame("input/output");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
InputOutputPanel panel = new InputOutputPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}

public InputOutputPanel(){

inputLabel = new JLabel("Enter Commands:");
input = new JTextField(20);
input.addActionListener(new CommandListener());
output = new JTextArea();
//new stuff
output.setColumns(20);
output.setLineWrap(true);
output.setRows(5);
//output.setWrapStyleWord(true);
output.setEditable(false);

jScrollPane1 = new JScrollPane(output);

add(inputLabel);
add(input);


add(jScrollPane1);

setPreferredSize(new Dimension(500,500));

}

public String getLine(){
return line;
}

private class CommandListener implements ActionListener{

public void actionPerformed(ActionEvent event){

line = input.getText();
//System.out.println(line);
output.append(line+"\r\n");
input.setText("");
}
}
}
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags.
 
Brett Wiggins
Greenhorn
Posts: 20
IntelliJ IDE Mac OS X MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about not using code tags, hope this is better;

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I am having some trouble understanding the GroupLayout class

is there a particular reason you want to use GroupLayout?

my understanding is it is just used by the netbeans IDE, generating about 500 lines of code,
where 5 lines would normally do the job.
 
Brett Wiggins
Greenhorn
Posts: 20
IntelliJ IDE Mac OS X MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

I saw GroupLayout being used in the sun java tutorials online. What is the alternative that uses five lines of code?

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> What is the alternative that uses five lines of code?

just about all of them, when hand-coded.
it's the netbeans-generated code (groupLayout) that produces (seemingly) 500 lines.

here's a link to sun's layout tutorial main page

http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

have a look at all of them (leave GridBagLayout till last - very complex).
each has a particular strength, and a layout effect is often achieved by nesting panels.

newcomers to layoutManagers often choose 'absolute positioning' (or null layoutManager),
as it seems easier to put components into specific coordinates, but this leads to major
problems in other areas, so using a null layoutManager is best avoided
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!