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!
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!