Why JTextArea doesn't work with JPanel? - java

Why when I add JTextArea to JPanel it doesn't work? When I use JButton instead of JTextArea everything works corectly. Why doesn't JTextArea work with JPanel but with JFrame does?
public class Searching extends JPanel {
private JPanel searchPanel;
private JTextArea addMedicament;
public Searching(){
searchPanel = new JPanel();
searchPanel.setLayout(new GridLayout(1,1));
setBackground(Color.BLUE);
addMedicament = new JTextArea();
searchPanel.add(addMedicament);
this.add(searchPanel);
}
}

A text area will work fine with a panel.
Try creating the text area as follows:
JTextArea textArea = new JTextArea(5, 20);
JScrollPane = new JScrollPane( textArea );
panel.add( scrollPane );
Now the text area will be created with a preferred size. As the data is changed scrollbars will appear/disappear as required because the problem is with your code and the context of how you use your code, not the panel or text area.
If this doesn't help then post a proper SSCCE that demonstrates the problem.

Related

Why is my scrolling textarea in java not scrolling?

I have a frame f, panel Fpanel. and textarea j.
This is a part of my code.
The scroll does not seem to be working on my text area.
JTextArea j=new JTextArea();
j.setBounds(60,150, 400,400);
j.setMargin(new Insets(3,3,3,3));
j.setEditable ( false ); // set textArea non-editable
JScrollPane scroll = new JScrollPane(j);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
I have added
Fpanel.add(scroll);
and
f.add(Fpanel);
to my code as well but it does not seem to be scrolling.
Am I missing a piece of the code or have I written something incorrectly?
if you want to have a vertical scroll bar:
JPanel panel = new JPanel();
JScrollPane jsp = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
if you want to have a horizontal scroll bar:
JPanel panel = new JPanel();
JScrollPane jsp = new JScrollPane(panel, ScrollPaneConstants.HORIZONTOL_SCROLLBAR_ALWAYS, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

Showing TextArea in one Line

JPanel leftPanel = new JPanel();
JPanel leftTopPanel = new JPanel();
JPanel leftDownPanel = new JPanel();
JTextArea textArea = new JTextArea();
textArea.setBackground(Color.red);
textArea.setText("DESCRIPTION");
textArea.setEnabled(false);
leftPanel.setLayout( new GridLayout(2,1));
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setPreferredSize( new Dimension(10,10));
leftPanel.add(leftTopPanel);
leftPanel.add(scrollPane);
return leftPanel;
Hi everyone. I am trying to make a textArea which basially will get description from database.
Here is the problem. When I write the real description in to TextArea, It always shows me the Description in one line. How can I fix it ?
THANKS :)

Adding buttons and scrollable panel to a tab - Java

I'm trying to build a Java GUI where among other things there will be several tabs.
In one of the tabs I want to have both a scrollable JTextArea, and also some buttons at the top/bottom that interacts with the JTextArea. I can't figure out how to get both into the same tab, I'm either getting the buttons and a non-scrollable jtextarea, or just the scrollable jtextarea. I also don't want to display the button in the other tabs. Here is my code:
private final JTextArea music = new JTextArea();
private final JTextArea button = new JTextArea();
private final JTextArea test = new JTextArea();
private final JTabbedPane tab = new JTabbedPane();
private JTable table;
music.append(newPlaylist.toString());
JFrame frame = new JFrame("GUI");
frame.setTitle("Music File Organiser");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton button1 = new JButton("Hello");
JButton button2 = new JButton("Sort by Album Title");
JButton button3 = new JButton("Sort by Track Title");
button1.addActionListener(new ActionListener() {code};
button2.addActionListener(new ActionListener() {code};
button3.addActionListener(new ActionListener() {code};
panel.add(music);
panel.add(button1);
panel.add(button2);
panel.add(button3);
JScrollPane scroll = new JScrollPane(panel);
JScrollPane scroll2 = new JScrollPane(table);
tab.add("Music Files", scroll);
tab.add("Table", scroll2);
this.add(tab);
frame.setLocationRelativeTo(null);
this.setSize(1200, 1000);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The tab in mind is the first one. How do I add buttons to "scroll"? The way I'm trying it here is to add the JTextArea "music" into panel, then adding the buttons to the same panel, then adding the panel to a JScrollPane, and then adding the JScrollPane to tab. Any help would really be appreciated.
Encapsulate your visual elements into child Panels.
JPanel buttonPanel = new JPanel(); //panel for buttons.
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
JScrollPane scroll = new JScrollPane(music); //scrollable pane for JTextArea
panel.add(buttonPanel);
panel.add(scroll); //add sub-components to panel for tab
/*here you would add some layout code to fit the panel and scroll into the associated spaces */
tab.add("Music Files", panel); //add panel to tab
You can do something like this:
JPanel tab = new JPanel()
JPanel tabWithButtons = new JPanel()
JPanel tabWithScrollPanel = new JPanel()
tab.add(tabWithScrollPanel)
tab.add(tabWithButtons)
IMO this is better way, because I assume that buttons shouldn't be included in JScrollPane and they should be visible all the time
Of course, you have to add all elements to proper JPanels.
Also, you can try with width/height settings of panel which contains all your elements, maybe ScrollArea panel is to big?

Add multiple jlabel on jpanel and on same line using box layout

Want to add two jlabel with some space on same line to jpanel, japnel layout is set to box layout,due to some constraints i can't change layout to another and property of box layout from Y_AXIS to LINE_AXIS, so please provide me with some solution, so i can put jlabel on same line..
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
So please let me know the solution for the same mentioned above.
Wrap your labels in a JPanel with a Border layout. Add one to the West panel and another to the East panel. Set the alignment of the JLabels as needed. Then add the JPanel to your box layout.
It looks like you believe you can't change the layout because you're dealing with the content pane of a JFrame and you don't want to change the rest of the window.
If that's the case, you can use nested layouts by adding the two JLabels to a separate JPanel (let's call it labelPanel) and adding that to the content pane. It would look something like this:
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.X_AXIS));
labelPanel.add(leftLabel);
labelPanel.add(Box.createGlue()); //creates space between the JLabels
labelPanel.add(rightLabel);
contentPane.add(labelPanel);
Try this out: JPanel with GridLayout, and JLabels left and right aligned. The frame is a Box still uses a box. What should interest you is the JPanel panel code. That's where I'm adding the labels. All you have to do is nest components and layouts
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class TwoLabels extends JFrame{
public TwoLabels(){
Box box = Box.createVerticalBox();
JPanel panel = new JPanel(new GridLayout(1, 2));
panel.setBorder(new LineBorder(Color.black));
JLabel label1 = new JLabel("Hello");
JLabel label2 = new JLabel("World");
label1.setHorizontalAlignment(JLabel.LEADING);
label2.setHorizontalAlignment(JLabel.TRAILING);
panel.add(label1);
panel.add(label2);
box.add(new JPanel(){
public Dimension getPreferredSize(){
return new Dimension(300, 300);
}
});
box.add(panel);
add(box);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
new TwoLabels();
}
}

Adding a Scrollable JTextArea (Java)

I am trying to add a scroll bar to a JTextArea. Would someone please tell me what I did wrong with the code below?
JFrame frame = new JFrame ("Test");
JTextArea textArea = new JTextArea ("Test");
JScrollPane scrollV = new JScrollPane (textArea);
JScrollPane scrollH = new JScrollPane (textArea);
scrollV.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollH.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.setVisible (true);
Thank you in advance.
EDIT: I fixed the code with the Adel Boutros' advice below.
//FRAME
JFrame frame = new JFrame ("Test");
frame.setSize(500,500);
frame.setResizable(false);
//
//TEXT AREA
JTextArea textArea = new JTextArea("TEST");
textArea.setSize(400,400);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroll);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
It doesn't work because you didn't attach the ScrollPane to the JFrame.
Also, you don't need 2 JScrollPanes:
JFrame frame = new JFrame ("Test");
JTextArea textArea = new JTextArea ("Test");
JScrollPane scroll = new JScrollPane (textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroll);
frame.setVisible (true);
Open design view
Right click to textArea
open surround with option
select "...JScrollPane".
A scroll pane is a container which contains another component. You can't add your text area to two different scroll panes. The scroll pane takes care of the horizontal and vertical scroll bars.
And if you never add the scroll pane to the frame, it will never be visible.
Read the swing tutorial about scroll panes.
You don't need two JScrollPanes.
Example:
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
// Add the scroll pane into the content pane
JFrame f = new JFrame();
f.getContentPane().add(sp);

Categories

Resources