I am attempting to teach myself how to make a GUI using Java swing and Window Builder Pro, after watching several youtube videos, and reading some tutorials I have accomplished the following.
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class JetstreamFrame extends JFrame {
private static final long serialVersionUID = 1L;
JTabbedPane tabPane;
JPanel buttonOnePanel;
JPanel buttonTwoPanel;
JPanel textDisplayPanel;
JTextPane textPane;
SpringLayout sl_textDisplayPanel;
SpringLayout springLayout;
SpringLayout sl_buttonTwoPanel;
SpringLayout sl_buttonOnePanel;
JButton ButtonTwo;
JButton ButtonOne;
public JetstreamFrame() {
springLayout = new SpringLayout();
getContentPane().setLayout(springLayout);
tabPane = new JTabbedPane(JTabbedPane.TOP);
setupTabPane();
buttonOnePanel = new JPanel();
sl_buttonOnePanel = new SpringLayout();
setupButtonOnePanel();
ButtonOne = new JButton("Click Here!");
setupButtonOne();
buttonTwoPanel = new JPanel();
sl_buttonTwoPanel = new SpringLayout();
setupButtonTwoPanel();
ButtonTwo = new JButton("Click Here!");
setupButtonTwo();
textDisplayPanel = new JPanel();
sl_textDisplayPanel = new SpringLayout();
setupTextDisplayPanel();
textPane = new JTextPane();
setupTextPane();
}
private void setupTabPane()
{
springLayout.putConstraint(SpringLayout.NORTH, tabPane, 0, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.WEST, tabPane, 0, SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, tabPane, -198, SpringLayout.SOUTH, getContentPane());
springLayout.putConstraint(SpringLayout.EAST, tabPane, 484, SpringLayout.WEST, getContentPane());
getContentPane().add(tabPane);
}
private void setupButtonOnePanel()
{
tabPane.addTab("Tab One", null, buttonOnePanel, null);
buttonOnePanel.setLayout(sl_buttonOnePanel);
}
private void setupButtonOne()
{
ButtonOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
}
});
sl_buttonOnePanel.putConstraint(SpringLayout.NORTH, ButtonOne, 99, SpringLayout.NORTH, buttonOnePanel);
sl_buttonOnePanel.putConstraint(SpringLayout.WEST, ButtonOne, 187, SpringLayout.WEST, buttonOnePanel);
buttonOnePanel.add(ButtonOne);
}
private void setupButtonTwoPanel()
{
tabPane.addTab("Tab Two", null, buttonTwoPanel, null);
buttonTwoPanel.setLayout(sl_buttonTwoPanel);
}
private void setupButtonTwo()
{
ButtonTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
}
});
sl_buttonTwoPanel.putConstraint(SpringLayout.NORTH, ButtonTwo, 99, SpringLayout.NORTH, buttonTwoPanel);
sl_buttonTwoPanel.putConstraint(SpringLayout.WEST, ButtonTwo, 187, SpringLayout.WEST, buttonTwoPanel);
buttonTwoPanel.add(ButtonTwo);
}
private void setupTextDisplayPanel()
{
springLayout.putConstraint(SpringLayout.NORTH, textDisplayPanel, 6, SpringLayout.SOUTH, tabPane);
springLayout.putConstraint(SpringLayout.WEST, textDisplayPanel, 0, SpringLayout.WEST, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, textDisplayPanel, -10, SpringLayout.SOUTH, getContentPane());
springLayout.putConstraint(SpringLayout.EAST, textDisplayPanel, 0, SpringLayout.EAST, getContentPane());
getContentPane().add(textDisplayPanel);
textDisplayPanel.setLayout(sl_textDisplayPanel);
}
private void setupTextPane()
{
sl_textDisplayPanel.putConstraint(SpringLayout.NORTH, textPane, 0, SpringLayout.NORTH, textDisplayPanel);
sl_textDisplayPanel.putConstraint(SpringLayout.WEST, textPane, 0, SpringLayout.WEST, textDisplayPanel);
sl_textDisplayPanel.putConstraint(SpringLayout.SOUTH, textPane, 182, SpringLayout.NORTH, textDisplayPanel);
sl_textDisplayPanel.putConstraint(SpringLayout.EAST, textPane, 0, SpringLayout.EAST, textDisplayPanel);
textDisplayPanel.add(textPane);
}
public void start()
{
this.setSize(500, 500);
this.setVisible(true);
}
}
The premise of this code is to create a window with multiple tabs, and a text area at the bottom that is visible no matter which tab the user is in. I have created the text area, the tabs, the buttons, as well as the button event listeners for this GUI.
Unfortunately, none of the tutorials I have read have showed me how to print anything into the text area. I would like to have something along the lines of
System.out.println();
attached to each of the buttons, and have that printing show up in the text area, but I do not know how to accomplish this.
I would also like to know if the text area I have created with result in a scroll bar along the side should the printed text extend beyond the visible frame.
If I understand you correctly, you could simply use the
append()
method.Here is more about JTextArea
To test it out, add a call to it in one of your button events (they don't seem to be doing anything at the moment). So you could do,
jTextArea.append("Did it print?");
and click that button to see it work.Edit It looks like you are using JTextPane, in that case, maybe use
setText()
. More info hereWill add any needed text to your text area based on an
event
. When weappend()
text we can also add new text onto an existing passage. If you want to clear theJTextArea
then I recommend you call thesetText()
method.A
JScrollPane
is needed to add a scroll bar to aJTextArea
which can be done by the following:Personally I like to set my scroll pane's properties to this, to ensure that the JScrollBar only is able to scroll Up and Down and remove the Left and Right scrolling:
scrollPane.setHorizonalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);