Add 2 JTextPane to one scrollPane - java

I am trying to add 2 JTextPane to one scrollPane. But it not scrolling. What am I doing wrong?
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(402, 211, 178, 193);
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
panel.setLayout(null);
JTextPane textPane_branding = new JTextPane();
textPane_branding.setBounds(98, 0, 78, 191);
panel.add(textPane_branding);
JTextPane textPane_trunk = new JTextPane();
textPane_trunk.setBounds(0, 0, 88, 191);
panel.add(textPane_trunk);

I'm not quite sure what you are trying to achieve here. If you want both of your JTextPane to be scrollable you need to put each one into its own JScrollPane. That would look like this:
JTextPane textPane_branding = new JTextPane();
JScrollPane scroll_branding = new JScrollPane(textPane_branding);
scroll_branding.setBounds(98, 0, 78, 191);
panel.add(scroll_branding);
JTextPane textPane_trunk = new JTextPane();
JScrollPane scroll_trunk = new JScrollPane(textPane_trunk);
scroll_trunk.setBounds(0, 0, 88, 191);
panel.add(scroll_trunk);
If you want both of your JTextPane being into one JPanel that is scrollable I'm wondering why you set fixed Bounds to your JScrollPane and to the JTextPanes. That makes Scrolling absurd here. And thats why JScrollPanes don't work with Panes that don't have Layouts and use fixed Bounds. Also it is very bad practise.
So I would suggest here to stay with a Layout Manager in your JPanel and use setPreferredSize with your JTextPanes to define your desired Dimensions. And then your JScrollPane will start working.
JScrollPane scrollPane = new JScrollPane();
//scrollPane.setBounds(402, 211, 178, 193); // Don't do this!
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
//panel.setLayout(null); // Use a Layout Manager
JTextPane textPane_branding = new JTextPane();
textPane_branding.setPreferredSize(new Dimension(78,191));
//textPane_branding.setBounds(98, 0, 78, 191);
panel.add(textPane_branding);
JTextPane textPane_trunk = new JTextPane();
textPane_trunk.setPreferredSize(new Dimension(88,191));
//textPane_trunk.setBounds(0, 0, 88, 191);
panel.add(textPane_trunk);

Related

JTextPane/JScrollPane display issue

I 'm trying to wrap a JTextPane with a JScrollPane, and I need the background to be transparent.
Here's my code:
public CharPanel(){
super.setLayout(null);
JButton LButton = new JButton("<");
LButton.setBounds(0, 300, 50, 50);
super.add(LButton);
JButton RButton = new JButton(">");
RButton.setBounds(950, 300, 50, 50);
super.add(RButton);
JTextPane Bio = new JTextPane();
Bio.setBackground(new Color(0, 0, 0, 0));
JScrollPane BioScroll = new JScrollPane(Bio);
BioScroll.setBackground(new Color(0, 0, 0, 0));
BioScroll.setBounds(0, 500, 1000, 150);
super.add(BioScroll);
}
This is what it looks like normally: https://gyazo.com/db7e4d2a5668b36ffc617cefb1423fc4
This is what it looks like after I enter a character: https://gyazo.com/1509d952005195d6e14365f0ae2da69a
See the weird visual bug?
Bio.setBackground(new Color(0, 0, 0, 0));
Don't use Colors with an alpha value. This breaks Swing painting rules and Swing don't know how to paint the component properly and you get painting artifacts.
For full transparency just use:
component.setOpaque( false );
Check out Backgrounds With Transparency for more information on this and a solution if you need to use partial transparency.

How do i create two or more colums inside a scrollpanel in GWT?

In general a scroll panel has only one column. If I want two columns, how do I have to modify my code?
ScrollPanel scrollPanel =new ScrollPanel(panel1);
scrollPanel.setSize("200px","100px");
DecoratorPanel decoratorPanel =new DecoratorPanel();
decoratorPanel.add(scrollPanel);
RootPanel.get("gwtContainer").add(panel);
RootPanel.get("gwtContainer").add(decoratorPanel);
You can scroll a Grid, FlexTable or LayoutPanel so you will have a common scroll for a flexible layout.
LayoutPanel lp = new LayoutPanel();
lp.add(whatever1);
lp.add(whatever2);
lp.setWidgetLeftWidth(whatever1, 0, Unit.PCT, 50, Unit.PCT);
lp.setWidgetRightWidth(whatever2, 0, Unit.PCT, 50, Unit.PCT);
ScrollPanel scrollPanel = new ScrollPanel(lp);
scrollPanel.setSize("200px", "100px");
DecoratorPanel decoratorPanel = new DecoratorPanel();
decoratorPanel.add(scrollPanel);
RootPanel.get("gwtContainer").add(decoratorPanel);
Hope it helps

refreshing data in jscroll pane swings

I have a main panel in which there is another panel to which i am applying scrolling.This panel in turn contains 2 panels.
Now when i apply scrolling the scroll bar appears.
The problem is when I dynamically add data from the user screen partially.....the data appears onto the screen but I am not able to scroll it panel down and the data below the scroll bar is not seen properly.
generalInformationPanel = new JPanel(new GridLayout(1, 1));
generalInformationPanel.setBounds(0, 0, 1050, 500);
generalInformationPanel.setBackground(Color.WHITE);
gd_GeneralInformationUpperPanel = new JPanel();
gd_GeneralInformationUpperPanel.setBounds(0, 0, 1050, 700);
gd_GeneralInformationUpperPanel.setLayout(null);
gd_GeneralInformationUpperPanel.setBackground(Color.WHITE);
js1 = new JScrollPane(gd_GeneralInformationUpperPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
js1.setBounds(0, 0, 950, 450);
js1.setVisible(true);
generalInformationPanel.add(js1);
add(generalInformationPanel, BorderLayout.CENTER);

JTextfield in JTabbedPane oversized

textArea = new JTextArea(textString);
JScrollPane text = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
text.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
And I add the JScrollPane text to a JTabbedPane, nothing special.
However the texfield expands and resizes the entire window when I switch tabs.
JFrame frame = new JFrame();
frame.setSize(600, 500);
JPanel main = new JPanel();
main.setLayout(new BorderLayout(0,0));
main.setBorder(new EmptyBorder(10, 10, 10, 10) );
textArea = new JTextArea(textString);
JScrollPane text = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
text.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
JTabbedPane tabs = new JTabbedPane();
tabs.add("Text lines", text);
tabs.add("Another", new JPanel());
main.add(new JLabel("test"), BorderLayout.NORTH);
main.add(tabs, BorderLayout.SOUTH);
frame.setContentPane(main);
frame.pack();
The problem that I found with your example is that when you enter many lines in the text area and switch to another tab, then the tabbed pane grows in size. In order to fix that, just enter how many text area rows you want to be display in the JTextArea constructor. For example, to display 5 rows:
textArea = new JTextArea(textString, 5, 0);
Now the tabbed pane will not get resized.

a retractable JTextArea with scrollpane

Goodevening
how can have a JTextArea like in netbeans (see the pic)
(source: hostingpics.net)
my code of the JTextArea:
JTextArea infoArea = new JTextArea(10,10);
infoArea.setLineWrap(true);
infoArea.setFont(police);
infoArea.setForeground(Color.YELLOW);
infoArea.setBackground(Color.BLACK);
infoArea.setEditable(false);
JScrollPane scroll = new JScrollPane(infoArea);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setBorder(BorderFactory.createEmptyBorder(5, 0, 10, 0));
scroll.setPreferredSize(new Dimension(sousFrame.getWidth(),90));
scroll.setFont( new Font("Arial", Font.BOLD, 13));
scroll.setBorder(BorderFactory.createTitledBorder("Output"));
thank you
I'm not sure what NetBeans uses, but we used flexdock at my last company to create dockable windows in a Java Swing application (assuming this is what you meant by "retractable").

Categories

Resources