Adding a Text Area to FlowLayout Panel Java Swing - java

I am trying to add a text area to a Panel that has flow layout, but its not showing on my GUI...any ideas pls?
private void makeTypes() {
westPanel.setVisible(false);
centerPanel.setVisible(false);
northPanel.setVisible(false);
contentPane.add(westPanel, BorderLayout.WEST);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(northPanel, BorderLayout.NORTH);
// set panel layout and add components
centerPanel.setLayout(new FlowLayout());
northPanel.setLayout(new GridLayout(4, 1));
/**
* Adding East Panel
*/
eastPanel.setLayout(new GridLayout(4,1));
/**
* Adding text area to Centre Panel
*/
a1=new JTextArea();
centerPanel.add(a1);
a1.setVisible(true);
}

centerPanel is invisible:
centerPanel.setVisible(false);
And so nothing added to it will show.
I see that you call setVisible(true) on the JTextArea, but this will have no effect if it is being added to an invisible container since it too will be invisible. Other suggestions: Give your JTextArea decent row and column property values, something that could be done via its constructor that takes two ints (for row and column). And wrap the JTextArea within a JScrollPane and add that to the GUI. If this code is called during program run and not at startup, then call revalidate() and repaint() on the container after adding and removing components.
For more and better help, consider creating and posting a valid SSCCE.

Related

How can I get the JTextArea to appear above the JTextField?

When I add both of them to SOUTH using BorderLayout, only the JTextArea appears. I'm using the text field as an input which is then displayed in the text area as an output above it with some other text.
It works if I set the text area to NORTH but it doesn't look great.
JPanel cmdPanel = new JPanel(new BorderLayout());
field = new JTextField(20);
cmdPanel.add(field, BorderLayout.SOUTH);
JTextArea output=new JTextArea();
output.setEditable(false);
output.setLineWrap(true);
cmdPanel.add(output, BorderLayout.SOUTH);
This image shows how it looks now with the TextArea set to NORTH.
I'm just trying to have it appear over the TextField and move up
the screen as outputs are added.
You have not provided a minimal, reproducible example but from the screen capture of your running app, it looks like you are explicitly setting the size of the JFrame. Rather than doing that, you should call method pack. Note that you should call that method after you have added both your JTextField and JTextArea to the cmdPanel and also after you have added cmdPanel to the JFrame. Also you should call method setVisible on the JFrame after calling method pack().
You should also make sure that the JTextArea has a preferred size before adding it to cmdPanel. One way to do this is by calling the constructor that takes the number of rows and columns parameters.
Maybe also wrap the JTextArea in a JScrollPane and make that the "center" component of cmdPanel?
JPanel cmdPanel = new JPanel(new BorderLayout());
field = new JTextField(20);
cmdPanel.add(field, BorderLayout.SOUTH);
JTextArea output=new JTextArea(10, 20);
output.setEditable(false);
output.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(output);
cmdPanel.add(scrollPane, BorderLayout.CENTER);
It is not possible to add two components in the same position (i.e BorderLayout.PAGE_END). To add more components in the same position, you should create a JPanel, add the components on it and then add the JPanel to the desirable position (i.e BorderLayout.PAGE_END).

Java Graphics2D, JPanel, JScrollPane

I'm working right now on a project, and I can't find a solution for my problem.
So here is my problem: I have a JFrame, then I add a container JPanel, and I add 2 other JPanel to this container panel , first panel (InputPanel) is for user input, second panel (Board) for displaying the specified algorithm based on the user input.
But the displayed algorithm is too large, so I thought I will add a JScrollPane to the DisplayPanel, but it didn't worked like I thought it would have. Here is a picture, the red rectangle is the area what changed after I added the JScollPane:
Notice that at my Board class I override the paintComponent(Graphics g) to draw the algorithm.
My code in the main frame:
container = new JPanel();
container.setLayout(new BorderLayout());
board = new Board();
container.add(board, BorderLayout.CENTER);
inputPanel = new InputPanel(board);
container.add(inputPanel, BorderLayout.SOUTH);
pane = new JScrollPane(board);
pane.setViewportView(board);
pane.setPreferredSize(new Dimension(700, 0));
container.add(pane, BorderLayout.WEST);
add(container);
My initial plan was to add a horizontal scrollpane to the Board panel. Can somebody post an example code, or point out for my problem pls?
You added the board twice to container. Add the JScrollPane to the center of container, and continue to pass board to the constructor of your JScrollPane. Don't add the board individually to the container if you want to add it via a JScrollPane.

basic questions in swing components in Java?

my question is could be very basic in terms of understanding this simple code. I wrote this code myself grabbing bits of code from here and there to understand. I would like to actually follow this code line by line as to what each line means?
I have added my understanding as comments above the line of code, it could be wrong or some of them marked as **** means I just dont know what it means. If you could help me out here, it will be great.
Thanks
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.text.ParseException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestingSwingComponents {
public TestingSwingComponents() {
//Create a frame which is the window that pops up
JFrame myframe = new JFrame();
//*****
myframe.getContentPane().setLayout(new BorderLayout());
//set the frame size to be 600 X 600 size
myframe.setSize(600, 600);
// create Pane1
JPanel myPanel = new JPanel();
//set the Layout component of Panel, as how you would like it to be
//here it is 2 rows and 15 columns
myPanel.setLayout(new GridLayout(2, 15));
//create a button with text in it
JButton letterButton = new JButton("click Me");
//add the created button component to the panel
myPanel.add(letterButton);
//******
myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
// create another panel
JPanel panelFormat = new JPanel();
//create a textfield
JTextField txtfield = new JTextField();
//create a label for the textfield
JLabel label = new JLabel("Guesss");
//set the layout type for this panel
panelFormat.setLayout(new BorderLayout());
//add label to panel
panelFormat.add(label);
//add textfield to panel
panelFormat.add(txtfield);
//I dont know the difference between the below two
//BorderLayout.CENTER still does not center the panel in the frame, I dont know why
myframe.getContentPane().add(panelFormat, BorderLayout.CENTER);
myframe.add(panelFormat);
// default settings
myframe.setTitle("Get buttons");
myframe.setVisible(true);
myframe.setLocationRelativeTo(null);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) throws ParseException {
new TestingSwingComponents();
}
}
myframe.getContentPane().setLayout(new BorderLayout());
To answer this, you need to understand the structure of a Swing window. A JFrame (in fact any Swing window) is made up a series of components which generate the view of the window.
(Picture from How to use Root Panes)
A JRootPane makes up the base of view, on-top of which is a JLayeredPane and what is know as the "glass pane". The JLayeredPane is responsible for managing the JMenuBar and the "content pane".
The content pane is where you components reside on the window.
So, what this line is saying is, "get the frame's content pane and set it's layout to use a BorderLayout"
The layouts API is an entire question on it's own and it would be use to you to have a read through Laying out components within a container for a more indeepth description, but basically, layout managers remove the need for you to care (a greate deal) about differences in rendering techniques employeed by different systems...
//******
myframe.getContentPane().add(myPanel, BorderLayout.SOUTH);
This comes back to the layout manager. Because you can have any number of layout managers, Swing allows you to pass a "constraint" to the layout manager when you add the component, giving the layout manager some idea of how you might like this component to be added.
If you take a closer look at BorderLayout you will see that it has five positions in which components can be added.
The line is basically saying, "please add myPanel to the SOUTH position within the frame/content pane"
Update from comments
If you have a look at this snippet...
panelFormat.setLayout(new BorderLayout());
//add label to panel
panelFormat.add(label);
//add textfield to panel
panelFormat.add(txtfield);
It sets the layout manager for panelFormat to BorderLayout. BorderLayout can only have a single component in any of it's five available positions. When you use add(Component) without passing it a layout constraint, BorderLayout use CENTER as the default position, this means you are trying to add two components to the CENTER position, this is not possible, so BorderLayout simply uses the last component that was added.
why not borderlayout fix the size of textfield instead of stretching
it all window
Because this is how BorderLayout works and no, GridLayout would probably do something simular.
You could try FlowLayout or GridBagLayout
Updated from comments
You seriously need to take the time to read through the linked (and other suggested) tutorials...but basically, you can use a GridBagLayout just like any other layout, you create an instance of it and apply it to the container...
// create another panel
JPanel panelFormat = new JPanel();
//create a textfield
JTextField txtfield = new JTextField(10);
//create a label for the textfield
JLabel label = new JLabel("Guesss");
//set the layout type for this panel
panelFormat.setLayout(new GridBagLayout());
//add label to panel
panelFormat.add(label);
//add textfield to panel
panelFormat.add(txtfield);
//I dont know the difference between the below two
//BorderLayout.CENTER still does not center the panel in the frame, I dont know why
myframe.getContentPane().add(panelFormat, BorderLayout.CENTER);
myframe.add(panelFormat);
A Swing top-level container, including a JFrame, JDialog is composed of several components all held together including a JRootPane which holds all together, a JLayeredPane, and a contentPane the latter of which holds most of the GUI excepting the top window bar. You can read more about the details in this tutorial here: Top Level Containers:
So when you add a component to a JFrame in a default way, you're actually adding it to its contentPane. In other words, this:
myJFrame.add(myComponent);
is functionally the same as this:
myJFrame.getContentPane().add(myComponent);
For learning Swing, I used this great tutorial which goes over everything you have, and explains it pretty clearly in depth.
The tutorial also goes over the elements that you are having trouble understanding.
Here is said tutorial.

Preventing JList from auto resizing

I have a JList inside a JScrollPane that's placed in a JPanel (BorderLayout.CENTER) and putting that inside another JPanel's BorderLayout.EAST (this JPanel's CENTER contains another JPanel) and this whole JPanel is placed inside a JTabbedPane. Initially, it would look like this:
Now I add some books to the list:
If I go to another tab and come back, this happens:
What I don't understand is that, the JPanel containing the JList has both its minimum and maximum size set:
JPanel listPanel = new JPanel();
listPanel.setLayout(new BorderLayout());
listPanel.add(new JScrollPane(bookList), BorderLayout.CENTER);
listPanel.setMinimumSize(listPanel.getPreferredSize());
listPanel.setMaximumSize(listPanel.getPreferredSize());
checkOutPanel.add(listPanel, BorderLayout.EAST);
How can I prevent the JList from auto resizing?
Depending on what it is you trying archive, you can either use JList#setPrototypeCellValue or JList#setFixedCellWidth. These will feed back into the PeferredScrollableViewportSize method which will effect the scroll pane

How can I make JFrame resize automatically to display all buttons

I have a simple swing application which consists of a JLabel and three buttons. The three buttons are in their own JPanel which is in a JFrame along with the JLabel. The JPanel uses flowlayout manager to arrange the buttons horizontally and the JFrame uses the BorderLayout manager to arrange the JLabel and JPanel vertically.
My problem is when I launch the application, during the course of use the text on one of the buttons changes which increases its width. However, the window doesn't resize to accomdate this and one of the buttons disappears. I thought about calling pack() again, but the JFrame is a local variable in my constructor, also, I shouldn't have to tell my program to resize, right? I haven't been able to find anything on google or here to help me but there must be a simple solution, what am I missing? Code is below.
playButton = new JButton("Play");
pauseButton = new JButton("Pause");
stopButton = new JButton("Stop");
curTrackLabel = new JLabel("No Track Selected");
JFrame myFrame = new JFrame("MediaPlayer");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setTitle("MediaPlayer");
myFrame.setLocation(400,300);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
myFrame.add(topPanel);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(playButton);
buttonPanel.add(pauseButton);
buttonPanel.add(stopButton);
topPanel.add(buttonPanel, BorderLayout.CENTER);
topPanel.add(curTrackLabel, BorderLayout.NORTH);
playButton.addActionListener(new playButtonHandler());
pauseButton.addActionListener(new pauseButtonHandler());
stopButton.addActionListener(new stopButtonHandler());
myFrame.pack();
myFrame.setVisible(true);
Maybe try
((JFrame)myButton.getTopLevelAncestor()).pack();
Where myButton is the button whose text is modified during execution.
As with learning any GUI software, experimentation is best. Try messing with BorderLayouts with nested JPanels.
Ultimately, you use JPanel with a BorderLayout (Flow Layout is OK but really when resizing the window, it epically fails). See http://download.oracle.com/javase/tutorial/uiswing/layout/border.html to learn more about BorderLayouts.
Now for your layout scheme it should be something along the lines of:
Top Level Container: JFrame
JFrame contains a JPanel (Call this
JPanel 1) with a BorderLayout.
The three buttons should be in a
SEPARATE jPanel (JPanel 2). JPanel
1 should add the three buttons as
BorderLayout.CENTER. In this way,
the window will resize if the button
changes its width and/or hright.
The JLabel should be added as
BorderLayout.LINE_START.
The tutorial at: http://download.oracle.com/javase/tutorial/uiswing/layout/border.html should help you with this. But in general, use the following:
Use JPanel and nest JPanels as necessary
BorderLayout.CENTER will accomodate size changes---this is the key! (Experiment with this)
JFrame should only be used as a top level container (for more complex GUIs, this is true).
If you require more flexibility, check out JGoodies: http://www.jgoodies.com/ . This is more along the lines of creating forms.

Categories

Resources