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);
Related
Hi I am trying to make a GUI using Javax to look something like this.
But currently it looks like this
This is my code
class Intro extends JFrame implements ActionListener
{
JFrame frame = new JFrame();
JButton ok;
JLabel background;
JLabel demo;
public Intro()
{
frame.setTitle("Let us start");
frame.setSize(600,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new JLabel(new ImageIcon("welcome.gif")));
frame.setLayout(new FlowLayout());
background = new JLabel();
frame.add(background);
frame.setSize(500,400);
ok = new JButton("OK");
demo = new JLabel("CSIT 121 Demo System", SwingConstants.RIGHT);
frame.add(demo);
ok.setHorizontalTextPosition(JButton.CENTER);
ok.setVerticalTextPosition(JButton.BOTTOM);
frame.add(ok);
}
}
What do i need to modify?
You should use another layout. Here is the correct example:
public class Intro extends JFrame {
JFrame frame = new JFrame();
JButton ok;
JLabel background;
JLabel demo;
public Intro() {
frame.setTitle("Let us start");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel contentPane = new JLabel(new ImageIcon("welcome.gif"));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
frame.setContentPane(contentPane);
frame.setLayout(new BorderLayout());
background = new JLabel();
frame.add(background); // why you need it??? it has no visual effect here
frame.setSize(500, 400);
ok = new JButton("OK");
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(ok);
frame.add(buttonPanel, BorderLayout.PAGE_END);
demo = new JLabel("CSIT 121 Demo System"); // probably you need to change foreground of label to fit your background image
frame.add(demo, BorderLayout.LINE_END);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(Intro::new);
}
}
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);
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());
I am trying to resize the JPanels but there is a space under it . Here is a link to show :
And this is the code :
import java.awt.*;
import javax.swing.*;
public class Ex1 extends JFrame{
private JTextArea textarea = new JTextArea ();
private JTextField field = new JTextField ();``
private JButton buton = new JButton ("Trimite");
public Ex1(){
JPanel panel = new JPanel (new BorderLayout(2,2));
JPanel panel1 = new JPanel (new BorderLayout(2,2));
JPanel panel2 = new JPanel (new BorderLayout(2,2));
JLabel label1 = new JLabel ("Mesaje");
JLabel label2 = new JLabel ("Scrieti un mesaj");
panel1.setPreferredSize(new Dimension(350,100));
panel2.setPreferredSize(new Dimension(350,25));
panel1.add(label1, BorderLayout.NORTH);
panel1.add(textarea, BorderLayout.CENTER);
panel2.add(label2, BorderLayout.WEST);
panel2.add(field, BorderLayout.CENTER);
panel2.add(buton, BorderLayout.EAST);
setLayout(new GridLayout(2,1,1,1));
panel.add(panel1, BorderLayout.NORTH);
panel.add(panel2, BorderLayout.CENTER);
add(panel);
}
public static void main(String[] args) {
JFrame frame = new Ex1();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
You are setting a layout for a frame to GridLayout in which all components are given equal size. You have two rows, add(panel) adds the panel to the first row of the grid. The second row is left empty. See How to Use GridLayout.
Comment out setLayout(new GridLayout(2,1,1,1)); and the extra space should go away. When you comment this line the layout of frame's content pane will be BorderLayout. The default layout of the JFrame is BorderLayout. So add(panel); will add the panel to the center of the frame's content pane. As a result the panel should occupy all the available space.
As a side note, avoid setPreferredSize(), usually it is not necessary, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing for details.
You can specify the number of rows and columns for a text area and wrap it in the scroll pane, ie:
textArea = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane(textArea);
For more details see How to Use Text Areas
EDIT: example of getPreferredSize()
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
public class Ex1 extends JPanel{
private JTextArea textarea = new JTextArea ();
private JTextField field = new JTextField ();
private JButton buton = new JButton ("Trimite");
public Ex1() {
setLayout(new BorderLayout());
JPanel panel1 = new JPanel (new BorderLayout(2,2));
JPanel panel2 = new JPanel (new BorderLayout(2,2));
JLabel label1 = new JLabel ("Mesaje");
JLabel label2 = new JLabel ("Scrieti un mesaj");
panel1.add(label1, BorderLayout.NORTH);
panel1.add(new JScrollPane(textarea), BorderLayout.CENTER);
panel2.add(label2, BorderLayout.WEST);
panel2.add(field, BorderLayout.CENTER);
panel2.add(buton, BorderLayout.EAST);
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(350, 300);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
Ex1 panel = new Ex1();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
});
}
}
You need to resize the JFrame not the JPanel. Try:
this.setPreferredSize(new Dimension(350, 25);// in Ex1
Or in your main method:
frame.setPreferredSize(new Dimension(350, 25);
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