GUI Newb... Im not sure how to make this work. (Auto-scrollbars using a JTextArea)

  • Thread starter Inertia_sublimation
  • Start date
I

Inertia_sublimation

Hello all!
Im trying to create a very basic text editing window, using a JTextArea. Due
to the nature of why the GUI was created, the JTextArea needs to be non-line
wrapping, so it needs scrollbars on both sides. I tryed wrapping the
JTextArea in a JScrollPane, but its not working... the text just keeps on
going outside of the visible JTextArea, and the scrollbars dont react. I
created the GUI in Net Beans (using the drag 'n drop interface), so bear
with me if it looks a bit sloppy.

Heres the code Im using:
import java.awt.*;

public class CodeColorerGUI extends javax.swing.JFrame {
/* User defined instance variables */
//ForumCodeColorer parser;

/** Creates new form CodeColorerGUI */
public CodeColorerGUI() {
// Start up the parser:
//parser = new ForumCodeColorer();
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
javax.swing.JLabel jLabel1;
javax.swing.JLabel jLabel2;
javax.swing.JProgressBar jProgressBar1;
javax.swing.JScrollPane jScrollPane1;
javax.swing.JSeparator jSeparator1;
javax.swing.JMenuBar mainMenuBar;
javax.swing.JMenu menuFile;
javax.swing.JPanel textPanel;
javax.swing.JScrollPane textScrollPane;

resultsWindow = new javax.swing.JFrame();
jScrollPane1 = new javax.swing.JScrollPane();
resultTextArea = new javax.swing.JTextArea();
waitDialog = new javax.swing.JDialog();
jProgressBar1 = new javax.swing.JProgressBar();
jLabel2 = new javax.swing.JLabel();
textPanel = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
textScrollPane = new javax.swing.JScrollPane();
codeTextArea = new javax.swing.JTextArea();
mainMenuBar = new javax.swing.JMenuBar();
menuFile = new javax.swing.JMenu();
itemCompile = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JSeparator();
itemExit = new javax.swing.JMenuItem();

resultsWindow.getContentPane().setLayout(new
java.awt.GridBagLayout());

resultsWindow.setTitle("Results:");
resultsWindow.setForeground(java.awt.Color.lightGray);
resultsWindow.setName("resultsWindow");
resultTextArea.setEditable(false);
resultTextArea.setText("This shouldnt appear.");
resultTextArea.setMinimumSize(getTextAreaSize());
resultTextArea.setPreferredSize(getTextAreaSize());
jScrollPane1.setViewportView(resultTextArea);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
resultsWindow.getContentPane().add(jScrollPane1,
gridBagConstraints);

waitDialog.getContentPane().setLayout(new java.awt.GridBagLayout());

waitDialog.setTitle("Processing...");
waitDialog.setBackground(java.awt.Color.lightGray);
waitDialog.setCursor(new
java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
waitDialog.setForeground(java.awt.Color.lightGray);
waitDialog.setName("waitDialog");
waitDialog.setResizable(false);
jProgressBar1.setIndeterminate(true);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
waitDialog.getContentPane().add(jProgressBar1, gridBagConstraints);

jLabel2.setText("Please Wait...");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
waitDialog.getContentPane().add(jLabel2, gridBagConstraints);

getContentPane().setLayout(new
javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.X_AXIS));

setTitle("Second Life Forums Automatic LSL Code Colorer");
setForeground(java.awt.Color.lightGray);
setLocationRelativeTo(null);
setName("mainWindow");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

textPanel.setLayout(new java.awt.GridBagLayout());

textPanel.setBorder(new javax.swing.border.EtchedBorder());
jLabel1.setText("Code:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
textPanel.add(jLabel1, gridBagConstraints);


textScrollPane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONT
AL_SCROLLBAR_ALWAYS);

textScrollPane.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_S
CROLLBAR_ALWAYS);
textScrollPane.setAutoscrolls(true);
codeTextArea.setTabSize(4);
codeTextArea.setText("//Enter code here...");
codeTextArea.setBorder(null);
codeTextArea.setPreferredSize(getTextAreaSize());
textScrollPane.setViewportView(codeTextArea);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
textPanel.add(textScrollPane, gridBagConstraints);

getContentPane().add(textPanel);

menuFile.setText("File");
itemCompile.setText("Color Script...");
itemCompile.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
itemCompileMouseClicked(evt);
}
public void mousePressed(java.awt.event.MouseEvent evt) {
itemCompileMouseClicked(evt);
}
});

menuFile.add(itemCompile);

menuFile.add(jSeparator1);

itemExit.setText("Exit");
itemExit.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
itemExitMouseClicked(evt);
}
public void mousePressed(java.awt.event.MouseEvent evt) {
itemExitMouseClicked(evt);
}
});

menuFile.add(itemExit);

mainMenuBar.add(menuFile);

setJMenuBar(mainMenuBar);

pack();
java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension dialogSize = getSize();

setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogS
ize.height)/2);
}

private void itemCompileMouseClicked(java.awt.event.MouseEvent evt) {
// Add your handling code here:
java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();

waitDialog.setSize((int)screenSize.getWidth()/6,(int)screenSize.getHeight()/
8);
java.awt.Dimension dialogSize = waitDialog.getSize();

waitDialog.setLocation((screenSize.width-dialogSize.width)/2,(screenSize.hei
ght-dialogSize.height)/2);
waitDialog.show();

//resultTextArea.setText(parser.parseCode(codeTextArea.getText()));
resultsWindow.pack();

dialogSize = resultsWindow.getSize();

resultsWindow.setLocation((screenSize.width-dialogSize.width)/2,(screenSize.
height-dialogSize.height)/2);

waitDialog.setVisible(false);
waitDialog.dispose();

resultsWindow.show();
}

private void itemExitMouseClicked(java.awt.event.MouseEvent evt) {
// Add your handling code here:
System.exit(0);
}

private Dimension getTextAreaSize()
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
return new Dimension(screenSize.width / 2, screenSize.height / 2);
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

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

public static void main(String args[]) {
new CodeColorerGUI().show();
}


// Variables declaration - do not modify
private javax.swing.JTextArea codeTextArea;
private javax.swing.JMenuItem itemCompile;
private javax.swing.JMenuItem itemExit;
private javax.swing.JTextArea resultTextArea;
private javax.swing.JFrame resultsWindow;
private javax.swing.JDialog waitDialog;
// End of variables declaration

}

Ignore the ForumCodeColorer object, since its the thing thats doing the
back-end parsing work, I'm just trying to get the GUI working at the moment.

Another problem I'm having is getting the waitDialog's JProgressBar to draw
correctly. I activate it in itemCompileMouseClicked(), but even if I put a
Thread.sleep() in before I dispose of and close waitDialog, all that shows
up is a grey box, with no JProgressBar inside.

Thanks a bunch in advance!
 
H

hiwa

IDE generated code is a torture for human eye.
And, noncommented GridBagLayout is another pain.
There are unnecessarily too many of them in netbeans generated code, as usual.
(I suspect that is the cause of your bug.)

Do simple by your HAND.

JTextArea jta = new JTextArea();
JScrollBar jsb = new JScrollBar(jta); //you can add 'policy' for the constructor
 
H

hiwa

OK. I have endured the torture and pain.

(1)Don't hardcode the preferred size of a component. It hinders
scrolling. Because scrolling means 'free size.'

// comment-out
// codeTextArea.setPreferredSize(getTextAreaSize());

(2)Give only temporary size from parent container.

setBounds(0, 0, 700, 700);
setVisible(true);
/**** comment-out
pack();
java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension dialogSize = getSize();

setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
****/
 
A

Andrew Thompson

Inertia_sublimation said:
Hello all!
Im trying to create a very basic text editing window, using a JTextArea.

It should not take 11Kb for a simple UI w/text area.
..Due
to the nature of why the GUI was created, the JTextArea needs to be non-line
wrapping, so it needs scrollbars on both sides. I tryed wrapping the
JTextArea in a JScrollPane, but its not working... the text just keeps on
going outside of the visible JTextArea, and the scrollbars dont react. I
created the GUI in Net Beans (using the drag 'n drop interface)

Well that explains both the bloated code
_and_ the fact it does not work.

Try using a simpler IDE like TextPad,
no drap and drop GUI designer
(but it does have context hilighting
w/ appropriate .syn file) and consult
the API docs and Sun tutorials on
making UI's.
.., so bear
with me if it looks a bit sloppy.

Heres the code Im using:
<trimmed>

The primary problem was lack of 'validate'
but I'd changed a couple of things before
that, so here's my version..

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

public class CodeColorerGUI extends JFrame {
// Variables declaration - do not modify
/* stuff you Net Beans -
I'll put where I damn well like! */
private JTextArea codeTextArea;
private JMenuItem itemCompile;
private JMenuItem itemExit;
private JTextArea resultTextArea;
private JFrame resultsWindow;
private JDialog waitDialog;
// End of variables declaration

/* User defined instance variables */
JScrollPane textScrollPane;
//ForumCodeColorer parser;

/** Creates new form CodeColorerGUI */
public CodeColorerGUI() {
// Start up the parser:
//parser = new ForumCodeColorer();
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
GridBagConstraints gridBagConstraints;
JLabel jLabel1;
JLabel jLabel2;
JProgressBar jProgressBar1;
JScrollPane jScrollPane1;
JSeparator jSeparator1;
JMenuBar mainMenuBar;
JMenu menuFile;
JPanel textPanel;

resultsWindow = new JFrame();
jScrollPane1 = new JScrollPane();
resultTextArea = new JTextArea();
waitDialog = new JDialog();
jProgressBar1 = new JProgressBar();
jLabel2 = new JLabel();
textPanel = new JPanel();
jLabel1 = new JLabel();
codeTextArea = new JTextArea();
textScrollPane = new JScrollPane( codeTextArea );
mainMenuBar = new JMenuBar();
menuFile = new JMenu();
itemCompile = new JMenuItem();
jSeparator1 = new JSeparator();
itemExit = new JMenuItem();

resultsWindow.getContentPane().setLayout(new
java.awt.GridBagLayout());

resultsWindow.setTitle("Results:");
resultsWindow.setForeground(java.awt.Color.lightGray);
resultsWindow.setName("resultsWindow");
resultTextArea.setEditable(false);
resultTextArea.setText("This shouldnt appear.");
// resultTextArea.setMinimumSize(getTextAreaSize());
// resultTextArea.setPreferredSize(getTextAreaSize());
jScrollPane1.setViewportView(resultTextArea);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
resultsWindow.getContentPane().add(jScrollPane1,
gridBagConstraints);

waitDialog.getContentPane().setLayout(new java.awt.GridBagLayout());

waitDialog.setTitle("Processing...");
waitDialog.setBackground(java.awt.Color.lightGray);
waitDialog.setCursor(new
java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
waitDialog.setForeground(java.awt.Color.lightGray);
waitDialog.setName("waitDialog");
waitDialog.setResizable(false);
jProgressBar1.setIndeterminate(true);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
waitDialog.getContentPane().add(jProgressBar1, gridBagConstraints);

jLabel2.setText("Please Wait...");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
waitDialog.getContentPane().add(jLabel2, gridBagConstraints);

getContentPane().setLayout(new
BoxLayout(getContentPane(), BoxLayout.X_AXIS));

setTitle("Second Life Forums Automatic LSL Code Colorer");
setForeground(java.awt.Color.lightGray);
setLocationRelativeTo(null);
setName("mainWindow");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

textPanel.setLayout(new java.awt.GridBagLayout());

textPanel.setBorder(new EtchedBorder());
jLabel1.setText("Code:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
textPanel.add(jLabel1, gridBagConstraints);


//
textScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR
_ALWAYS);

textScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR
_AS_NEEDED);

//
textScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALW
AYS);
textScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_
NEEDED);
textScrollPane.setAutoscrolls(true);
codeTextArea.setTabSize(4);
codeTextArea.setText("//Enter code here...\n");
for(int ii=0; ii<40; ii++)
{
codeTextArea.append("The quick brown fox jumped over the lazy dog.\n");
}
codeTextArea.setBorder(null);
//codeTextArea.setPreferredSize(getTextAreaSize());
//textScrollPane.setViewportView(codeTextArea);

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
textPanel.add(textScrollPane, gridBagConstraints);

getContentPane().add(textPanel);

menuFile.setText("File");
itemCompile.setText("Color Script...");
itemCompile.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
itemCompileMouseClicked(evt);
}
public void mousePressed(java.awt.event.MouseEvent evt) {
itemCompileMouseClicked(evt);
}
});

menuFile.add(itemCompile);

menuFile.add(jSeparator1);

itemExit.setText("Exit");
itemExit.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
itemExitMouseClicked(evt);
}
public void mousePressed(java.awt.event.MouseEvent evt) {
itemExitMouseClicked(evt);
}
});

menuFile.add(itemExit);

mainMenuBar.add(menuFile);

setJMenuBar(mainMenuBar);

pack();
validate();
java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
java.awt.Dimension dialogSize = getSize();

setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogS
ize.height)/2);
}

private void itemCompileMouseClicked(java.awt.event.MouseEvent evt) {
// Add your handling code here:
java.awt.Dimension screenSize =
java.awt.Toolkit.getDefaultToolkit().getScreenSize();

waitDialog.setSize((int)screenSize.getWidth()/6,(int)screenSize.getHeight()/
8);
java.awt.Dimension dialogSize = waitDialog.getSize();

waitDialog.setLocation((screenSize.width-dialogSize.width)/2,(screenSize.hei
ght-dialogSize.height)/2);
waitDialog.show();

//resultTextArea.setText(parser.parseCode(codeTextArea.getText()));
resultsWindow.pack();

dialogSize = resultsWindow.getSize();

resultsWindow.setLocation((screenSize.width-dialogSize.width)/2,(screenSize.
height-dialogSize.height)/2);

waitDialog.setVisible(false);
waitDialog.dispose();

resultsWindow.show();
}

private void itemExitMouseClicked(java.awt.event.MouseEvent evt) {
// Add your handling code here:
System.exit(0);
}

private Dimension getTextAreaSize()
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
return new Dimension(screenSize.width / 2, screenSize.height / 2);
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new CodeColorerGUI().show();
}
}
 
A

Andrew Thompson

[ And.. could I ask you to ensure that your
code does not go beyond 80 chars wide
in future as well? It wraps in newsreaders
and needs to be 'reconstructed' before it
will compile.

Though of course I sent >80 char code
straight back at you - I could claim, zen-like,
that the purpose was to encourage you not
to do that, but it was really just lazyness
on my part ..as it happens. ;-) ]

A.
 
A

Andrew Thompson

hiwa said:
"Inertia_sublimation" <[email protected]> wrote in message
IDE generated code is a torture for human eye.
And, noncommented GridBagLayout is another pain.
There are unnecessarily too many of them in netbeans generated code, as usual.
(I suspect that is the cause of your bug.)

Do simple by your HAND.

That's better advice to what I gave,
or perhaps, those were the words I
was looking for.
 
I

Inertia_sublimation

Thanks for the replys everyone! I rewrote the class from almost-scratch and
it turned out alot nicer, with *working* scrollbars! :-D
 
A

Andrew Thompson

Inertia_sublimation said:
Thanks for the replys everyone! I rewrote the class from almost-scratch and
it turned out alot nicer, with *working* scrollbars! :-D

:) I hesitate to ask,
...did you stick with the GBL?
 
I

Inertia_sublimation

'Fraid so... I really like the customization it lets you do. I havent found
any other layouts that will allow me to have something like that without
ugly workarounds. Mind I ask... whats wrong with GBL? ^_^
 
S

Sudsy

Inertia_sublimation said:
'Fraid so... I really like the customization it lets you do. I havent found
any other layouts that will allow me to have something like that without
ugly workarounds. Mind I ask... whats wrong with GBL? ^_^

Nothing, as far as I'm concerned. GridBagLayout has been able to
handle everything I've thrown at it. You might have to do some
tweaking of constraints to have everything come out right but I
like to use it for just about everything.
 
A

Andrew Thompson

Inertia_sublimation said:
'Fraid so... I really like the customization it lets you do. I havent found
any other layouts that will allow me to have something like that without
ugly workarounds. Mind I ask... whats wrong with GBL? ^_^

....hmm. Short question with long answer.

I'll put together a page on the GBL
...one day. Read the groups long
enough and I think you'll get the picture.

Glad to hear you got it working for
your project. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top