Java - ScrollPaneLayout - Customising the vertical scrollbar background, slider and arrows - java

I'm attempting to build a custom JScrollPane to be more specific as indicated in the design image below.
I'm having problems with the following tasks:
Customising the vertical scrollbar background, slider and arrows:
I'm having problems in customizing the scrollbar - to be more specific:
Changing the vertical scrollbar background to yellow
Changing the scrollbar slider color to another color
Creating the JScrollPane border to contain radius edges on the border
Changing the scrollbar arrows color to another color
For the border I was successful with my attempts to change it to yellow. Here's the code:
class MyScrollPane extends JScrollPane {
public MyScrollPane(Component view) {
super(view, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);
this.setLayout(new MyLayout());
setBackground(Color.red);
}
}
.
class MyLayout extends ScrollPaneLayout {
public MyLayout() {
super();
}
public void layoutContainer(Container parent) {
super.layoutContainer(parent);
vsb.setSize(vsb.getWidth() , vsb.getHeight()); // drift
vsb.setBorder(new LineBorder(Color.YELLOW));
}
}
Regarding the background I was unsuccessful - for example the following doesn't work:
vsb.setBackground(Color.red);
Any ideas/thoughts would be very helpful as I've been stuck for several days.

Related

How to change the color of Scrollbar in JScrollPane?

I want to change the JScrollPane Scrollbar color to black. I tried to make the change using UIManger. but found nothing.
To change the background color of the scroll bar, you can do this:
scrollPane.getVerticalScrollBar().setBackground(Color.BLACK);
scrollPane.getHorizontalScrollBar().setBackground(Color.BLACK);
To change the color of the scrollbar itself, use the following code:
import javax.swing.plaf.basic.BasicScrollBarUI;
scrollPane.getVerticalScrollBar().setUI(new BasicScrollBarUI() {
#Override
protected void configureScrollBarColors() {
this.thumbColor = Color.BLACK;
}
});

Javafx text area scroll pane border color problem

I have a problem in javafx text area: When I focus the text area, border is applied... that is ok.
But when I drag with scroll-bar handle, the text area border focus is lost.
See the image below:
This is the my simple text area:
Text area changed when focused like this:
But when I scroll in text area with the scroll handle, the border is changed like before (un-focused) state:
Is there any way to control text area within scroll pane (in text area)?
One possible work around is to not allow focus on the ScrollPane within TextArea. ie., the moment ScrollPane gets focus we force to focus on TextArea. This way the focus will be always on TextArea.
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
public class CustomTextArea extends TextArea {
private ScrollPane textAreaScrollPane;
#Override
protected void layoutChildren() {
super.layoutChildren();
if (textAreaScrollPane == null) {
textAreaScrollPane = (ScrollPane) lookup(".scroll-pane");
textAreaScrollPane.focusedProperty().addListener((obs, oldVal, focused) -> {
if (focused) {
requestFocus();
}
});
}
}
}
And you will use this CustomTextArea across your application.
TextArea textArea = new CustomTextArea();

Java Swing Panel displaying image strange behaviour

I have no idea what the heck is going on while I'm trying to render an image on a JPanel in a JFrame. In fact I just want an image displayed in a fixed position on my JFrame.To achieve this I have a JPanel of a fixed size (min, max, pref size set) on my JFrame. On press of a button, I add another panel to mentioned panel, having the same size. The "child" panel has an overriden paint method to draw an image. So far so good, once I press the button and add that child panel, nothing happens at all. If I then click on the empty panel, the image is drawn as it should be, not overflowing the bounds of the child panel. However, all other components width increases drastically, stretching them out of the JFrames bounds. One would think that at least a scrollbar would appear, but no, the components are then out of reach. I'm attaching two screenshots displaying that very logic behavior.
The code is as follows:
void setPoster(BufferedImage poster) {
ImagePanel ip = new ImagePanel(poster);
ip.setSize(new Dimension(222, 327));
panelPoster.add(ip);
ip.setSize(new Dimension(222, 327));
}
Inside ImagePanel:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
System.out.println("Paiting imagepanel with size " + getSize());
}
Before screenshot:
After screenshot:
Could anyone enlighten me on what could possibly occur here?
I fixed the strange resizing by setting the layout to null on both the JFrame and the JPanel inside my tabbed pane.
setLayout(null);
jPanel4.setLayout(null);
Anyway, that's not needed when using the JLabel instead of the JPanel to hold the image.
However, my ImagePanel would not draw the image anymore, so I replaced it by a JLabel which draws the image just fine:
void setPoster(BufferedImage poster) {
JLabel imageLabel = new JLabel(new ImageIcon(poster));
panelPoster.add(imageLabel);
imageLabel.setSize(new Dimension(222, 327));
}
Note that without setting the size of the JLabel, it won't display anything.

Vaadin: button's layout is not displayed or not in the bottom

I have Vaadin Vertical Layout which contains a horizontal layout with description, a panel with a table and horizontal layout with buttons. There is a problem with different displays:
1) On small monitors, button's layout is not visible.
public class ApprovalView extends VerticalLayout implements ClickListener
{
public ApprovalView()
{
setSizeFull();
setSpacing(true);
setMargin(true);
initialize();
addComponent(topLayout);
addComponent(tablePanel);
addComponent(bottomLayout);
}
...
}
2) On large monitors, button's layout is not on the bottom. They sticks to the tablePanel that causes an ugly empty area on the bottom.
public class ApprovalView extends VerticalLayout implements ClickListener
{
public ApprovalView()
{
//setSizeFull();
setSpacing(true);
setMargin(true);
initialize();
addComponent(topLayout);
addComponent(tablePanel);
addComponent(bottomLayout);
}
...
}
I tried to create a panel with another vertical layout with same content to make it scrollable but it only messed everything up. And scroll stopped until the buttons were shown.
How to resolve this problem?

Border affects components position java

So I have a JPanel that has an inner border (it's toggled based on MouseEnter/MouseExit, as a sort of a rollover effect). I also have a JLabel. The problem is that the JLabel seems to be positioned relative to the border - not the actual edge of the JPanel. So whenever I move my mouse over the panel, the label shifts over a couple of pixels. I would prefer it to stay stationary.
So I guess my question is, what's the best way to change the border of a panel without affecting the positions of the components inside the panel?
Here's the mouselisteners for the panel:
panel.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
panel.setBorder(BorderFactory.createBevelBorder(1, Color.BLACK, Color.BLACK));
}
#Override
public void mouseExited(MouseEvent e) {
panel.setBorder(null);
}
});
The JLabel is added simply using borderlayout:
panel.setLayout(new BorderLayout());
JLabel label = new JLabel("testlabel");
panel.add(label,BorderLayout.PAGE_END);
You could try using an EmptyBorder when the bevel border is not in use. Give it the same width/height you would the bevel border.
I don't do a lot of messing around with layouts or their managers but that's what I would try.
Edit
Since it seems you may wish to have an overlay type effect instead of a border, you could create a custom JPanel class and include some code in the paintComponent(Graphics g) method to draw this overlay.
Something similar to:
public class OverlayBorderJPanel extends JPanel
{
boolean containsMouse = false; //set to true by mouseListener when contains
BufferedImage overlay = //you would need to load an image border here,
//rather than having a java created border
//You could have alpha so it is half see-through
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (containsMouse)
{
g.drawImage(//use 0,0 position with panel width/height)
}
}
}
I think it would work with something like that, but you may need to call the panel's repaint() method in the listener as well.

Categories