This is the code I am struggling with. It is refusing to amend the JTextArea with the new text. I create the window and set it to visible in the main function of the project.
Thanks ahead.
EDIT:
By refusing, I mean the JTextArea will simply not display the text. It just stays empty. I'm not getting and error or exception. It is all logical.
class Window extends JFrame{
protected JTextArea text;
public Window() {
setTitle("Create a list of names");
setSize(500,400);
Container containerPane = getContentPane();
JPanel jp = new JPanel();
text = new JTextArea(10,50);
text.setPreferredSize(new Dimension(256,256) );
text.setEditable(false);
JScrollPane scrollText = new JScrollPane(text);
scrollText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jp.add(scrollText);
containerPane.add(jp, BorderLayout.CENTER);
text.append("Test");
}
public static void main(String[] args) {
Window w = new Window();
w.setVisible(true);
}
}
The column width of 50 is greater than the width of the frame so the added text appears offscreen. Reduce its value to fit the parent window
textArea = new JTextArea(10, 35);
Don't use setPrerredSize. Let the layout manager do its job and call pack after all components have been added.
Related
I'm making a simple weather app using IntelliJ and built in form designer. I have made and designed a form and edited the bound class file accordingly. When I actually run the code, the form I designed does now show up at all, in fact just the last line (maxtemp) is the only value that shows up on an empty white screen.
P.S I am using Gradle to build, I have also set the GUI designer to use Java source code instead of byte code (since Gradle does not support the byte code)
import org.json.JSONException;
import org.json.JSONObject;
import javax.swing.*;
import java.awt.*;
public class Frame extends JFrame {
// Get the API
private API api = new API();
// Form elements
public JPanel mainPanel;
public JLabel text;
public JLabel category;
public JLabel mintemp;
public JLabel maxtemp;
public JLabel link;
public Frame() throws Exception, JSONException {
initFrame();
}
public void initFrame() throws Exception, JSONException {
// Get API response
JSONObject wjson = api.connection();
// Filter response and get data
String[] data = api.respFilter(wjson);
// Swing components
mainPanel = new JPanel(new BorderLayout());
text = new JLabel();
category = new JLabel();
mintemp = new JLabel();
maxtemp = new JLabel();
link = new JLabel();
// Add to the frame
add(mainPanel);
add(text);
add(category);
add(mintemp);
add(maxtemp);
text.setText(data[0]);
category.setText(data[1]);
mintemp.setText(data[3]);
maxtemp.setText(data[4]);
}
public static void main(String[] args) throws Exception, JSONException {
JFrame app = new Frame();
app.setTitle("Java-WeatherApp");;
app.setSize(900, 600);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setLocationRelativeTo(null);
app.setVisible(true);
}
}
I'm pretty new to Java and very new to the form builder on IntelliJ. Unfortunately I could not find any good tutorials on the form builder. Any help is appreciated!
A JFrame has BorderLayout as a default layout.
In your initFrame() method when you call,
add(mainPanel);
add(text);
add(category);
add(mintemp);
add(maxtemp);
You are adding all the swing components to the JFrame. Since, you are not specifying the position of a component and since the JFrame has BorderLayout, only one component is added to the JFrame that is the last component you add to it, which the maxtemp Jlabel.
You should specify the position of a component when adding to the JFrame having BorderLayout as following,
add(minTemp, BorderLayout.NORTH);
I suggest you to learn more about BorderLayout and LayoutManagers.
OR,
You can use code similar to this,
// Swing components
mainPanel = new JPanel(new GridLayout(4, 1));
text = new JLabel();
category = new JLabel();
mintemp = new JLabel();
maxtemp = new JLabel();
link = new JLabel();
// Add to the mainPanel
mainPanel.add(text);
mainPanel.add(category);
mainPanel.add(mintemp);
mainPanel.add(maxtemp);
//Add mainPanel to Frame
add(mainPanel);
The above written code does the following,
mainPanel = new JPanel(new GridLayout(4, 1)); This line of code set GridLayout as the layout of mainPanel. GridLayout is a type of layout manager which divides the container(mainPanel) in to equal number of grids by dividing it into rows and columns.
In the given code I have divided the mainPanel into 4 rows and 1 columns, so total 4 grids.
mainPanel.add(text);
mainPanel.add(category);
mainPanel.add(mintemp);
mainPanel.add(maxtemp);
This bunch of code add the swing components to the mainPanel. When the first line of code is executed, the text label is added to mainPanel in first grid. Similarly, second line add category to the second grid and similar for the rest.
add(mainPanel); This line of code adds mainPanel to the Frame.
If you executed the above code, the output will be similar to,
Note : I have changed the text of the labels as shown in above image.
It seems like the only way to display text with multiple styles in a text area is to use a JEditorPane. To explore this, I wrote a small app to list all the fonts in their font.
Everything works, and the list is displayed in a JEditorPane, which is inside a JScrollPane.
However, it takes a few seconds to launch and to repopulate, because it generates the entire new list before displaying it. The code is below, following MCV. Minimalizing it to just the message and text made less work so the lag is no longer nearly as noticeable, but my question (below) was more about rendering and keeping track of JScrollPane's position anyway than about this app.
FontFrame.java
public class FontFrame extends JFrame {
private FontFrame() {
JFrame frame = new JFrame("Fonts Displayer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FontPanel fontPanel = new FontPanel();
frame.getContentPane().add(fontPanel);
frame.getRootPane().setDefaultButton(fontPanel.getDefaultButton());
frame.setMinimumSize(new Dimension(600, 600));
frame.setPreferredSize(new Dimension(1000, 700));
frame.pack();
frame.setVisible(true);
}
public static FontFrame launchFontFrame() {
return new FontFrame();
}
public static void main(String[] args) {
FontFrame.launchFontFrame();
}
}
FontPanel.java
class FontPanel extends JPanel {
private String message = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz";
private JEditorPane fontPane;
private JTextField messageInput;
private JButton changeButton;
protected FontPanel() {
buildPanel();
}
private void buildPanel() {
setLayout(new BorderLayout());
// Build message input panel
JPanel inputPanel = new JPanel();
messageInput = new JTextField(message);
messageInput.setFont(new Font("SansSerif", Font.PLAIN, 14));
messageInput.setColumns(40);
JLabel messageLabel = new JLabel("Message:");
changeButton = new JButton("Change");
changeButton.addActionListener((ActionEvent e) -> {
String text = messageInput.getText();
if (!text.isEmpty() && !text.equals(message)) {
message = text;
refreshFontList();
}
});
inputPanel.add(messageLabel);
inputPanel.add(messageInput);
inputPanel.add(changeButton);
// Build scrolling text pane for fonts display
fontPane = new JEditorPane();
fontPane.setContentType("text/html");
fontPane.setEditable(false);
fontPane.setVisible(true);
JScrollPane scrollPane = new JScrollPane(fontPane);
refreshFontList();
// Add components to main panel
add(inputPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
}
private void refreshFontList() {
String[] list = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
StringBuilder messages = new StringBuilder();
// Set global table style settings
messages.append("<table>");
// Make a row for each font
for (String font : list) {
messages.append("<tr>");
//Append data cells
messages.append("<td>").append(font).append("</td>")
.append("<td style=\"font-family:").append(font).append("\">")
.append(message)
.append("</td>");
messages.append("</tr>");
}
messages.append("</table>");
fontPane.setText(messages.toString());
}
JButton getDefaultButton() {
return changeButton;
}
}
Is there any way of redoing refreshFontList so it can generate only what fontPane would show in its viewport first, possibly by using JScrollBar, and then generate the rest of the list right after so the computation time doesn't cause lag in display? Is there a different component that would work better?
I made a simple program in Java that contains only one text area and a button. The button is suppose to add a "text". However, it doesn't work for me.
On a side note: I'm trying to keep my functions as short as possible. (I don't want a function with too many line of codes)
First, I create the JFrame
private static void createFrame()
{
//Build JFrame
JFrame frame = new JFrame("Text Frame");
frame.setLayout(null);
frame.setSize(500,400);
Container contentPane = frame.getContentPane();
contentPane.add(textScrollPane());
contentPane.add(buttonAddText());
//Set Frame Visible
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Then the TextArea and the Scrollpane (for adding scrollbar)
private static JTextArea textArea()
{
JTextArea output = new JTextArea();
output.setLineWrap(true); // Text return to line, so no horizontal scrollbar
output.setForeground(Color.BLACK);
output.setBackground(Color.WHITE);
return output;
}
private static JScrollPane textScrollPane()
{
JScrollPane scrollPane2 = new JScrollPane(textArea());
scrollPane2.setBounds(0, 0, 490, 250);
return scrollPane2;
}
And finally, the button
private static JButton buttonAddText()
{
JButton testbutton = new JButton("TEST");
testbutton.setBounds(20, 280, 138, 36);
testbutton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e)
{
//action listener here
textArea().insert("TEXT",0);
System.out.println("Button Tested!");
}
});
return testbutton;
}
When I click on the button, it doesn't do anything.
I just want a text to be added in the JTextArea. Did I forget something?
textArea() is returning a new JTextArea everytime it is called. Therefore your buttonAddText() function is calling textArea() and adding text to a newly created text area that is not contained in the scroll pane.
You need to pass a reference of the text area to the textScrollPane() and the buttonAddText() functions.
Something like this would work:
JTextArea jta = textArea();
contentPane.add(textScrollPane(jta));
contentPane.add(buttonAddText(jta));
Change textScrollPane() and buttonAddText() so that they accept a JTextArea parameter and don't call textArea() in these functions anymore to create new text areas. Instead use the JTextArea object which is passed into the functions.
Here's the portion of my java application GUI that I have a question about.
What this GUI consists is a blue JPanel(container) with default FlowLayout as LayoutManager that contains a Box which contains two JPanels(to remove the horizontal spacing or i could have used setHgaps to zero for that matter instead of a Box) that each contains a JLabel.
Here's my code for creating that part of the GUI.
private void setupSouth() {
final JPanel southPanel = new JPanel();
southPanel.setBackground(Color.BLUE);
final JPanel innerPanel1 = new JPanel();
innerPanel1.setBackground(Color.ORANGE);
innerPanel1.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
innerPanel1.add(new JLabel("Good"));
final JPanel innerPanel2 = new JPanel();
innerPanel2.setBackground(Color.RED);
innerPanel2.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
innerPanel2.add(new JLabel("Luck!"));
final Box southBox = new Box(BoxLayout.LINE_AXIS);
southBox.add(innerPanel1);
southBox.add(innerPanel2);
myFrame.add(southPanel, BorderLayout.SOUTH);
}
My question is how would i get rid the vertical padding between the outer JPanel(the blue one) and the Box?
I know this is padding because i read on Difference between margin and padding? that "padding = space around (inside) the element from text to border."
This wouldn't work because this has to due with gaps(space) between components.- How to remove JPanel padding in MigLayout?
I tried this but it didn't work either. JPanel Padding in Java
You can just set the gaps in the FlowLayout, i.e.
FlowLayout layout = (FlowLayout)southPanel.getLayout();
layout.setVgap(0);
The default FlowLayout has a 5-unit horizontal and vertical gap. Horizontal doesn't matter in this case as the BorderLayout is stretching the panel horizontally.
Or simple initialize the panel with a new FlowLayout. It'll be the same result.
new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
Edit:
"I tried that, didn't work.."
Works for me...
Setting the gap ↑ Not setting the gap ↑
import java.awt.*;
import javax.swing.*;
public class Test {
public void init() {
final JPanel southPanel = new JPanel();
FlowLayout layout = (FlowLayout)southPanel.getLayout();
layout.setVgap(0);
southPanel.setBackground(Color.BLUE);
final JPanel innerPanel1 = new JPanel();
innerPanel1.setBackground(Color.ORANGE);
innerPanel1.add(new JLabel("Good"));
final JPanel innerPanel2 = new JPanel();
innerPanel2.setBackground(Color.RED);
innerPanel2.add(new JLabel("Luck!"));
final Box southBox = new Box(BoxLayout.LINE_AXIS);
southBox.add(innerPanel1);
southBox.add(innerPanel2);
southPanel.add(southBox); // <=== You're also missing this
JFrame myFrame = new JFrame();
JPanel center = new JPanel();
center.setBackground(Color.yellow);
myFrame.add(center);
myFrame.add(southPanel, BorderLayout.SOUTH);
myFrame.setSize(150, 100);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setLocationByPlatform(true);
myFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new Test().init();
}
});
}
}
Note: Always post a runnable example (as I have done) for better help. You say it doesn't work, but it always works for me, so how would we know what you're doing wrong without some code that will run and demonstrate the problem?
My JFrame Consists of three main parts a banner at top scrollpane containing a JTextArea center and a JTextField at the bottom. When I re-size the frame I adjust the columns and rows in my JTextArea. When making the frame larger the JTextArea expands visually but removes the scroll-bar. Then if I make the frame smaller the JTextArea stays the same size. This Is where I attempt to re-size my JTextArea.
frame.addComponentListener(new ComponentAdapter() {//Waits for window to be resized by user
public void componentResized(ComponentEvent e) {
uneditTextArea.setRows(((int)((frame.getHeight()-140)/18.8)));//sets Textarea size based on window size
uneditTextArea.setColumns(((int)((frame.getWidth()-100)/10.8)));
frame.revalidate();//refreshes screen
}
});
Why would the ScrollPane not re adjust to the change in size of the TextField.
The Rest of the code is below in case it is needed.
public class window extends JFrame
{
private static JFrame frame = new JFrame("Lillian");
private static JButton inputButton = new JButton("Send");
private static JTextField editTextArea = new JTextField(46);
private static JTextArea uneditTextArea = new JTextArea(26,50);
private static JPanel logoPanel = new JPanel();//Input text window
private static JPanel itextPanel = new JPanel();//Input text window
private static JPanel submitPanel = new JPanel();//Submit Button
private static JPanel bottom = new JPanel();//will contain scrollpane
private static JPanel middle = new JPanel();//willcontain itextpanel & submitbutton
private static JPanel otextPanel = new JPanel();//Text Output
public static void runWindow()
{
ImageIcon logo = new ImageIcon("Lillian_resize.png");//banner
ImageIcon icon = new ImageIcon("Lillian_icon.png");//application icon
frame.setIconImage(icon.getImage());
frame.setSize(660,640);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
logoPanel.setSize(10,10);
JLabel logoLabel = new JLabel(logo);
final JScrollPane scrollPane = new JScrollPane(otextPanel);//adds text to panel will scrollbar
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);//scrollbar only apears when more text than screen
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);//scrollbar never apears
scrollPane.setBorder(BorderFactory.createEmptyBorder());
logoPanel.add(logoLabel);
submitPanel.add(inputButton);
itextPanel.add(editTextArea);
otextPanel.add(uneditTextArea);
frame.getContentPane().add(logoPanel,"North");
frame.getContentPane().add(middle);
frame.getContentPane().add(bottom,"South");
middle.add(scrollPane,"North");//adds panels to outer panel
bottom.add(itextPanel, "West");
bottom.add(submitPanel, "East");
uneditTextArea.setLineWrap(true);
uneditTextArea.setWrapStyleWord(true);
uneditTextArea.setEditable(false);
uneditTextArea.setCaretPosition(uneditTextArea.getDocument().getLength());
frame.revalidate();//refreshes screen
//---------------wait for action------------
frame.addComponentListener(new ComponentAdapter() {//Waits for window to be resized by user
public void componentResized(ComponentEvent e) {
uneditTextArea.setRows(((int)((frame.getHeight()-140)/18.8)));//sets Textarea size based on window size
uneditTextArea.setColumns(((int)((frame.getWidth()-100)/10.8)));
frame.revalidate();//refreshes screen
}
});
}
}
There should be no need to use a ComponentListener to resize components. That is the job of the layout managers that you use to dynamically resize the components.
You should not be adding the text area to a JPanel first. Instead when using text areas you would generally add the text area directly to the viewport of a JScrollPane by using code like:
JScrollPane scrollPane = new JScrollPane( textArea );
Then you add the scrollpane to the frame with code like:
frame.add(scrollPane, BorderLayout.CENTER);
As you have noticed you should also NOT use hardcoded literals like "Center". Instead use the variables provided by the layout manager. Since you are using a BorderLayout, use the variables defined in the BorderLayout class.
Also, you should NOT be using static variable to create your GUI. I suggest you read the section from the Swing tutorial on Layout Manager. The tutorial will give you more information and the example code will show you how to better structure your program so that you don't need to use static variables.