I want to create a blank text area in which the user can enter a few sentences, and then, when the user closes the window (or before), I want to save this text in a string (and print it just to test that it works). So far, the code I have written does not work:
JTextArea area = new JTextArea(5,20);
JScrollPane scrollPane = new JScrollPane(q);
JFrame frame = new JFrame("TextDemo");
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
String paragraph_text = area.getText();
System.out.println(paragraph_text);
You need to add your JTextArea to the scrollpane
JScrollPane scrollPane = new JScrollPane(area);
Related
There's something that I don't understand. My code does not like JScrollBar apparently. I add it and I cannot scroll horizontally nor vertically.
Here's what it looks like:
Keep in mind that I'm new and I'm still working on it, so I'm sorry if it was something really obvious and easily avoidable.
public ChangeLog() {
//Init.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextArea textarea = new JTextArea();
JScrollPane scrollpane = new JScrollPane(textarea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//Text Stuff
textarea.setFont(textarea.getFont().deriveFont(16f));
textarea.setText("Change Log: \n V1.0(A): Original encoder \n V1.0(B): Original decoder \n V1.1: Combination of both encoder and decoder \n V1.2: Added a heavier encoding & decoding system \n V1.3: Added an icon \n V1.4: Created an 'Info' page \n V1.5: Added a 'Change Log' page to the 'Info' page \n "
+ "V1.6: Removed the 'Change Log' \n V1.7: Added a 'Change Log' but was not implemented \n V1.8: Added a the 'Change Log' button \n V1.9: Added horizontal and vertical scroll bars to the 'Change Log'");
textarea.setForeground(Color.BLACK);
Dimension d = new Dimension(250, 275);
textarea.setPreferredSize(d);
//Other Stuff
scrollpane.setViewportView(textarea);
scrollpane.getPreferredSize();
//Layout
panel.setLayout(null);
scrollpane.setBounds(new Rectangle(new Point(20, 20), scrollpane.getPreferredSize()));
textarea.setBounds(new Rectangle(new Point(20, 23), textarea.getPreferredSize()));
//Frame Stuff
frame.setAlwaysOnTop(true);
frame.setSize(300, 350);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
//Panel Stuff
frame.add(panel);
panel.setSize(frame.getSize());
panel.setBackground(Color.BLUE);
panel.add(textarea);
panel.add(scrollpane);
} }
I have created a working solution. Made some changes also.
public TestClass() {
//Init.
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
JTextArea textarea = new JTextArea();
JScrollPane scrollpane = new JScrollPane(textarea);
panel.add(scrollpane, BorderLayout.CENTER);
//Text Stuff
textarea.setFont(textarea.getFont().deriveFont(16f));
textarea.setText("Change Log: \n V1.0(A): Original encoder \n V1.0(B): Original decoder \n V1.1: Combination of both encoder and decoder \n V1.2: Added a heavier encoding & decoding system \n V1.3: Added an icon \n V1.4: Created an 'Info' page \n V1.5: Added a 'Change Log' page to the 'Info' page \n "
+ "V1.6: Removed the 'Change Log' \n V1.7: Added a 'Change Log' but was not implemented \n V1.8: Added a the 'Change Log' button \n V1.9: Added horizontal and vertical scroll bars to the 'Change Log'");
textarea.setForeground(Color.BLACK);
//Dimension d = new Dimension(250, 275);
//textarea.setPreferredSize(d);
//Other Stuff
scrollpane.setViewportView(textarea);
scrollpane.getPreferredSize();
//Layout
//scrollpane.setBounds(new Rectangle(new Point(20, 20), scrollpane.getPreferredSize()));
//textarea.setBounds(new Rectangle(new Point(20, 23), textarea.getPreferredSize()));
//Listeners
//Frame Stuff
frame.setAlwaysOnTop(true);
frame.setSize(300, 350);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
//Panel Stuff
frame.add(panel);
panel.setSize(frame.getSize());
panel.setBackground(Color.BLUE);
panel.add(scrollpane);
}
Also when swing better works with the layout managers and null layout will leads to inconsistent look on different screen types.
Let me know if anything more required. And yes everybody starts from scratch. I am still learning. You will too get many things. Just keep the hunger of learning. :-)
Dimension d = new Dimension(250, 275);
textarea.setPreferredSize(d);
Don't hardcode a size for the text area. The size of the text area will change dynamically as text is added/removed and scrollbars will appear/disappear as required.
JTextArea textarea = new JTextArea();
Don't create the text area with no parameters. Instead, when you create the text area use something like:
JTextArea textarea = new JTextArea(5, 20);
to suggest a default size of the text area. Then when you have more than 5 lines of text the scrollbar will appear.
So I'm a relatively new Java developer
Start by reading the Swing Tutorial for Swing basics. There is a section on How to Use Text Areas to get you started.
panel.setLayout(null);
scrollpane.setBounds(...)
Don't a null layout. Don't use setBounds(). Swing was designed to be used with layout managers. See the above tutorial for working examples.
I have a scrollpane in a panel and a jtextarea under the scrollpane. jtextarea append is not working
I am using this for logging purpose.
JPanel panel_1 = new JPanel();
tabbedPane.addTab("Logs", null, panel_1, null);
panel_1.setLayout(null);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(0, 0, 672, 303);
panel_1.add(scrollPane_1);
JTextArea jTextArea = new JTextArea(100,200);
jTextArea.setEditable(false);
jTextArea.setVisible(true);
scrollPane_1.add(jTextArea);
jTextArea.append("Hello");
scrollPane_1.add(jTextArea);
Don't add components to a scrollpane. The component needs to be added to the viewport of the scrollpane.
The easiest way to do this is to use:
JTextArea jTextArea = new JTextArea(100,200);
jTextArea.setEditable(false);
jTextArea.setVisible(true);
//scrollPane_1.add(jTextArea);
jTextArea.append("Hello");
JScrollPane scrollPane_1 = new JScrollPane(jTextArea);
scrollPane_1.setBounds(0, 0, 672, 303);
panel_1.add(scrollPane_1);
The other way to do this is to use:
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setViewportView( jTextArea );
Also you should note when you create a text area the parameters are row/columns, not pixels so your values are too large. I would suggest something like:
//JTextArea jTextArea = new JTextArea(100,200);
JTextArea jTextArea = new JTextArea(30,10);
Finally you should not be setting the bounds of the scrollpane. Swing was designed to be used with layout managers. The layout manager will determine the size of the scrollpane based on the size of the text area:
//scrollPane_1.setBounds(0, 0, 672, 303);
I am trying to do a fairly basic swing GUI where a user can enter a url, choose a local file location etc. I am using multiple layout managers including boxlayout, borderlayout and flowlayout. The code is below.
My problem is that some of the components are moving around when the user puts text into the optionsTxt jtextarea. Anyone know where I should start to stop this happening?
Setup menu bar
JButton menu_File = new JButton("File");
JButton menu_Edit = new JButton("Edit");
JToolBar toolBar = new JToolBar();
toolBar.add(menu_File);
toolBar.add(menu_Edit);
//Setup options area
JPanel options = new JPanel();
options.setBorder(BorderFactory.createTitledBorder("Options"));
BoxLayout layout_Options = new BoxLayout(options, BoxLayout.Y_AXIS);
options.setLayout(layout_Options);
JLabel optionsLblURL = new JLabel("Enter URL:");
optionsTxtUrl = new JTextArea(1,15);
JLabel chooseDestLbl = new JLabel("Choose save location:");
chooseDest = new JButton("Browse");
chooseDest.addActionListener(this);
options.add(optionsLblURL);
options.add(optionsTxtUrl);
options.add(chooseDestLbl);
options.add(chooseDest);
//Setup launch area
JPanel launch = new JPanel();
launch.setBorder(BorderFactory.createTitledBorder("Launch"));
launchBtnStart = new JButton("Start Download");
launchBtnStart.setVerticalAlignment(SwingConstants.CENTER);
launchBtnStart.setHorizontalAlignment(SwingConstants.CENTER);
launchBtnStart.addActionListener(this);
launch.add(launchBtnStart);
//Setup reporting area
JPanel logging = new JPanel();
logging.setBorder(BorderFactory.createTitledBorder("Log"));
BoxLayout layout_Logging = new BoxLayout(logging, BoxLayout.Y_AXIS);
logging.setLayout(layout_Logging);
JTextArea loggingTxt = new JTextArea(3,10);
loggingTxt.setEditable(false);
logging.add(pb);
logging.add(loggingTxt);
//Add components to window
BorderLayout borderLayout = new BorderLayout();
setLayout(borderLayout);
add("North", toolBar);
add("West", options);
add("East", launch);
add("South", logging);
setVisible(true);
"My problem is that some of the components are moving around when the user puts text into the optionsTxt jtextarea. Anyone know where I should start to stop this happening?"
Start by putting your JTextArea in a ScrollPane and setLineWrap(true) and setWrapStyleWord(true). You may want to consider doing this with both JTextAreas you have
JTextArea optionsTxtUrl = new JTextArea(1,15);
optionsTxtUrl.setLineWrap(true);
optionsTxtUrl.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(optionsTxtUrl);
options.add(scroll); // take out options.add(optionsTxtUrl);
This will make your lines wrap when they reach the right edge of the text area
public void setWrapStyleWord(boolean word) - Sets the style of wrapping used if the text area is wrapping lines. If set to true the lines will be wrapped at word boundaries (whitespace) if they are too long to fit within the allocated width. If set to false, the lines will be wrapped at character boundaries. By default this property is false.
public void setLineWrap(boolean wrap) - Sets the line-wrapping policy of the text area. If set to true the lines will be wrapped if they are too long to fit within the allocated width. If set to false, the lines will always be unwrapped. A PropertyChange event ("lineWrap") is fired when the policy is changed. By default this property is false.
If this doesn't solve your problem, you should edit your post with a Minimal, Complete, Tested and Readable example so we can test out your problem.
I would like to add HTML in my JEditorPane but the text is not displayed correctly.
Also, when the text's height is greater that the editor's height,
the the cursor goes to the last line of the scroll Pane.
My code is as follows:
JPanel JPInfo = new JPanel(new BorderLayout());
JPInfo.setBorder(BorderFactory.createTitledBorder("Information"));
editorPaneInfo = new JEditorPane();
editorPaneInfo.setEditable(false);
editorPaneInfo.setText("<html><p style=\"color:green\"> Test Test </p></html>");
JScrollPane editorScrollPaneInfo = new JScrollPane(editorPaneInfo);
editorScrollPaneInfo.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPInfo.add(editorScrollPaneInfo,BorderLayout.CENTER);
SOLVED:
I added the following line before setText
editorPaneInfo.setContentType("text/html");
My problem was solved:
I added the following line before .setText()
editorPaneInfo.setContentType("text/html");
I am facing a problem, I want to add more than a button to a JFrame, but it only takes the last one and puts it into the frame, a sample of my code is below:
String isName = "";
JFrame frame = new JFrame(isName);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
String childAmb = "PDA276";
for (int j=0; j<3; j++){
if (childAmb.matches("Phone\\w\\w\\w"))
fancyButtonCreator(childAmb, new ImageIcon ("src/phone.gif"), frame);
else if (childAmb.matches("PDA\\w\\w\\w"))
fancyButtonCreator(childAmb, new ImageIcon ("src/pda.gif"), frame);
else if (childAmb.matches("PC\\w\\w\\w"))
fancyButtonCreator(childAmb, new ImageIcon ("src/pc.gif"), frame);
}
frame.setVisible(true);
frame.setBounds(100, 200, 200, 200);
Thank you.
If you don't have a layout-manager, only one, the last component added will show up.
frame.setLayout (new FlowLayout ());
frame.add (new JButton ("foo"));
frame.add (new JButton ("bar"));
Read the section from the Swing tutorial on Using Layout Managers for examples.
You can start with the FlowLayout.