scrollbars adjust when zooming using Jxlayer and PBar extensions - java

I tried to use Jxlayer and PBar extensions (using the response of MadProgrammer from here) to add the zoom capability to my JPanel which is contained in a JScrollPanel.
The zoom itself work fine but when the zoomed panel becomes larger than the containing JFrame the scrollbars doesn’t adjust and stay inactive.
Here is my code:
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
Integer[] zoomList = {50, 75, 100, 125, 150, 200};
JComboBox<Integer> zoomBox = new JComboBox<>(zoomList);
zoomBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
int value = (int) zoomBox.getSelectedItem();
double scale = value / 100d;
transformModel.setScale(scale);
}
});
topPanel.add(zoomBox);
Panel centerPanel = new TestPane();
JScrollPane scrollPane = new JScrollPane(synopticPanel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.getViewport().setOpaque(false);
scrollPane.setOpaque(false);
frame.add(topPanel, BorderLayout.NORTH);
frame.add(synopticPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
and in the Testpane class
public class TestPane extends JPanel {
private JLayer<JComponent> layer;
private JPanel content;
public TestPane() {
content = new JPanel();
content.setLayout(null);
//Adding some components
transformModel = new DefaultTransformModel();
transformModel.setScaleToPreferredSize(true);
layer = TransformUtils.createTransformJLayer(content, transformModel, null);
setLayout(new BorderLayout());
add(layer);
}
Thank you for your help

Related

How to have a JButton that moves as I scroll on the frame?

I have a Panel which I have made scrollable in my frame.
What I need is to add a button that stays fixed in the lower right corner even when I scroll.
I'm new to Java Swing so would appreciate all and any help that I can get.
mainPanel = new SimulationPanel(); //class SimulationPanel extends JPanel
//making mainPanel scrollable
mainPanel.setPreferredSize(new Dimension(((int)(WIDTH*1.2)), HEIGHT));
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);
// Settings for JFrame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(scrollPane);
frame.setSize(screenSize.width, screenSize.height);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
I would use nested panels with the outer one be with BorderLayout. Then one with FlowLayout and align FlowLayout.RIGHT and the button inside it.
public class Example extends JFrame {
public Example() {
super("");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea(10000, 0);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("button");
JPanel panelWithButton = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panelWithButton.add(button);
add(panelWithButton, BorderLayout.PAGE_END);
setLocationByPlatform(true);
pack();
setSize(600, 600);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Example().setVisible(true);
});
}
}
Result:
I would go for a BoxLayout. Add another panel (metaPanel) in which your first put your scrollingPanel, and then you add a button. Instead of usgin scrollingPanel as contentPane, you use metaPanel. Example (the example works, but you need to modify it to make the interface look nice):
JPanel mainPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);
JPanel metaPanel = new JPanel();
BoxLayout boxlayout = new BoxLayout(metaPanel, BoxLayout.Y_AXIS);
metaPanel.setLayout(boxlayout);
metaPanel.add(scrollPane);
metaPanel.add(new JButton("button"));
// Settings for JFrame
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(metaPanel); // Put metaPanel here
frame.setSize(500, 300);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);

Java JXBrowser: Is it possible to draw over the JXBrowser Component?

I just wanted to know if it's possible to paint over the JXBrowser component? I have searched on the internet and found that the used BrowserView inherits paintcomponents etc. But I can't seem to get it to work.
Here's the code:
public test() {
browser = new Browser();
view = new BrowserView(browser);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(view, BorderLayout.CENTER);
test = new JButton("Open FOE");
test.addActionListener(this);
test1 = new JButton("Helpen");
test1.addActionListener(this);
test1.setMaximumSize(new Dimension(90, 20));
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.add(test);
panel1.add(Box.createRigidArea(new Dimension(0, 15)));
panel1.add(test1);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(panel, BorderLayout.CENTER);
panel2.add(panel1, BorderLayout.EAST);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel2);
frame.setSize(1500, 1000);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
How it looks now:
What I want to achieve:
Use glass pane,
sample code fragment:
JPanel glassPane = new JPanel()
{
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("Test",100,100);
g.setColor(Color.BLUE);
g.drawRect(300,300,300,300);
}
};
glassPane.setOpaque(false);
frame.getGlassPane().setVisible(true);
frame.setGlassPane(glassPane);
frame.setVisible(true);

How do I add a component in the upper-left of a JScrollPane?

I have created a JScrollPane with a RowHeaderView, a ColumnHeaderView and a ViewPortView. I added JPanels in diffrent colors and noticed, that there is one cornor left, on the upper-left where you cant just add a Component. I wanted to ask, how it is possible to add a Component there.
Here a image. The area I mean is green:
And here my Code:
public class Example {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel panel0 = new JPanel();
panel0.setBackground(Color.yellow);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.red);
panel1.setPreferredSize(new Dimension(30, 200));
JPanel panel2 = new JPanel();
panel2.setBackground(Color.blue);
panel2.setPreferredSize(new Dimension(200, 30));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(panel0);
scrollPane.setRowHeaderView(panel1);
scrollPane.setColumnHeaderView(panel2);
scrollPane.setBackground(Color.green);
frame.add(scrollPane);
frame.setVisible(true);
}
}
It's easy. Use the method setCorner
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, new JButton());

Content is being add horizontally to JScrollPane

im starting to work with java Swing and i was trying to make a system to show something like a Map :
But when i try to add another entry to the JSCrollPane it's being added horizontally intead of vertically, i have tried everything i don't what i mithgt be doing wrong but i can't manage do fix it.
Here i create the Frame :
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel();
final JPanel content = new JPanel();
new DataEntry("", 0).create(content);
final JScrollPane scrollPane = new JScrollPane(content, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setWheelScrollingEnabled(true);
panel.add(scrollPane);
scrollPane.setMinimumSize(new Dimension(400, 60));
final JButton add = new JButton("Add");
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new DataEntry("", 0).create(content);
}
});
panel.add(add);
frame.setContentPane(panel);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);
And this is how i create the Entry :
public JPanel create(final JPanel content) {
final JPanel panel = new JPanel();
final JPanel fields = new JPanel();
fields.add(new JLabel("Variable"));
fields.add(variable);
fields.add(new JLabel("Row"));
fields.add(row);
panel.add(fields);
JButton remove = new JButton("Remove");
remove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
content.remove(panel);
content.revalidate();
}
});
panel.add(remove);
content.add(panel, BorderLayout.CENTER);
content.revalidate();
return panel;
}
At the start i was wondering why it wasn't displaying any new Entry, then i tried changing HORIZONTAL_SCROLLBAR_NEVER > HORIZONTAL_SCROLLBAR_AS_NEEDED and then i realized that the variables were being add horizontally.
Here is a gif to see what's going on (Couldn't manage to take a proper photo) : GIF

Java layout with frames and panels

I'm building my first Java swing GUI, but I cannot manage to have the layout as I wanted.
Can you help me to understand how the layout should be set to have the desired result (see image below)?
This is what I get (wrong):
and if I resize it manually I get the desired result:
Here is my code:
public class MainClass implements Runnable {
private JButton load = new JButton("Load..");
private JButton save = new JButton("Save..");
private JButton clear = new JButton("Clear");
private JLabel displayFile = new JLabel();
List<String> lines;
JFrame frame = new JFrame("SimpleDrawing");
public static void main(String[] args) {
MainClass maincl = new MainClass();
SwingUtilities.invokeLater(maincl);
}
#Override
public void run() {
DrawingArea area = new DrawingArea();
frame.setLayout(new FlowLayout());
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
//buttonPane.add(Box.createHorizontalGlue());
buttonPane.add(load);
//buttonPane.add(Box.createRigidArea(new Dimension(5,0)));
buttonPane.add(save);
//buttonPane.add(Box.createRigidArea(new Dimension(5,0)));
buttonPane.add(clear);
buttonPane.add(displayFile);
frame.add(buttonPane, BorderLayout.PAGE_START);
frame.add(area, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Your Frame has the FlowLayout.
frame.setLayout(new FlowLayout());
Give it a BorderLayout.
frame.setLayout(new BorderLayout());
Layout- Informations:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Categories

Resources