I can't add scroll bar on EditorPane.
private JEditorPane editorPane;
private JScrollPane scrollpane;
Container :
Container c = getContentPane();
c.setLayout(null);
setBounds(100, 100, 450, 300);
editorPane = new JEditorPane();
editorPane.setBounds(0, 54, 434, 208);
scrollpane = new JScrollPane(editorPane);
scrollpane.setPreferredSize(new Dimension(350, 110));
c.add(scrollpane);
..
..
Nothing added
You're shooting yourself in the foot here:
editorPane.setBounds(0, 54, 434, 208);
By setting the editorPane's absolute size, you prevent it from expanding when it needs to do so, preventing the JScrollBars from having to show.
Solution: don't do this. And yeah, avoid using null layouts. They'll bite you, as you're finding out. Set the width using CSS
getContentPane().setLayout(null);
This means "I give a damn on the help of others because I know better that anyone else how to layout a GUI!"
So this is where you are.
I'd recomment to go through the tutorials and learn how to build GUIs using LayoutManagers.
Related
I have such piece of code which shoud add button over JTextArea placed in JScrollPane. Button isn't inside scroll pane!
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(10, 340, 375, 242);
contentPane.add(scrollPane);
JButton btnClean = new JButton();
btnClean.setBounds(340, 341, 26, 23);
contentPane.add(btnClean);
taLog = new JTextArea();
taLog.setEditable(false);
taLog.setLineWrap(true);
taLog.setFont(new Font("Arial", Font.PLAIN, 12));
scrollPane.setViewportView(taLog);
epInfo = new JEditorPane();
epInfo.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
epInfo.setFont(new Font("Tahoma", Font.PLAIN, 12));
epInfo.setContentType("text/html");
epInfo.setEditable(false);
epInfo.setBackground(null);
epInfo.setBorder(null);
epInfo.setBounds(10, 241, 375, 37);
setInfoText();
contentPane.add(epInfo);
Problem is whenever JTextArea changes its value button is not refreshed - it just dissapears - until I will drag mouse cursor over it. I suppose then some repaint() is launched.
I figured out I can add DocumentListener to JTextArea and there manually refresh button but it works until scroll bar appears in JScrollPane.
I can also use
scrollPane.getViewport().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e)
{}});
but it is invoked many times and all those refreshes are not neccesary.
Is there any reasonable (and efficient) way to check if JScrollPane changed?
Null layout is useful in this case because main window is fixed
No it is not useful.
Any time you code uses random numbers (ie to set the size/location) of components) the code is not easily maintainable.
Also I use WindowBuilder
Code generated by an IDE is not maintainable in another IDE.
Learn to use layout managers. The point of using layout managers is to avoid hard coding of numbers and to make the code maintainable in any environment.
Problem is whenever JTextArea changes its value button is not refreshed
contentPane.add(scrollPane);
...
contentPane.add(btnClean);
Swing is designed to display components in two dimension when added to the same container.
If you want components displayed in two dimension then you need to have a parent / child relationship:
- content pane
- text area
button
So the code should be something like:
textArea.setLayout( new FlowLayout(FlowLayout.RIGHT) );
//contentPane.add(btnClean);
textArea.add(btnClean);
Note: you can configure the FlowLayout to remove the spacing on the top and right.
The problem with this is that the button will cover any text that displayed in the text area.
So the better solution is to make the button external to the text area.
I would like to achieve the below layout.
There are 6 panels. The 4 buttons at the top are one panel, and the 3 buttons at the right side of the image are also in one panel. Apart from those two there are 4 other panels as indicated by the borders. I tried the below code but displays everything in a scattered way.
mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(lefsideToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(descriptionPanel,BorderLayout.LEFT);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(propertiesPanel,BorderLayout.EAST);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);
How can I achieve the design as shown in the image? I need all the panels to be arranged inside that mainPanel. I cannot use null layout though. Please advice.
After trashgod's answer :
JPanel gridPanel = new JPanel(new GridLayout(1, 0));
gridPanel.add(jInternalFrame1);
gridPanel.add(descriptionPanel);
mainPanel.add(gridPanel, BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(PropertiesPanel,BorderLayout.LINE_END);
What I get :
Add lefsideToolBarPanel and descriptionPanel to a panel having GridLayout; add the new panel to the BorderLayout.
Panel p new Panel(new GridLayout(1, 0));
p.add(lefsideToolBarPanel);
p.add(descriptionPanel);
//mainPanel.add(lefsideToolBarPanel, BorderLayout.LINE_START);
//mainPanel.add(descriptionPanel, BorderLayout.LEFT);
mainPanel.add(p, BorderLayout.LINE_START);
There is no BorderLayout.LEFT. See also A Visual Guide to Layout Managers.
Addendum: Your updated question shows elements of topToolBarPanel, which should be added to PAGE_START, rather than LINE_START.
//mainPanel.add(topToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout. PAGE_START);
The width of the propertiesPanel and height of the tablePanel need to be increased. I used setSize()…
For the propertiesPanel, you can override getPreferredSize(), as discussed here. For the tablePanel, override getPreferredScrollableViewportSize() to customize the size of the table's enclosing JScrollPane, for example.
I suggest using a JLabel as your "layout" to use exact positioning of yout objects with setBounds(x, y, width, height). It would look similar to this :
JButton button = new JButton("Text or Image");
JLabel backgr = new JLabel();
JFrame frame = new JFrame("JLabel as Layout");
button.setBounds(100, 200, 340, 40);
backgr.add(button);
frame.add(backgr);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocation(40, 40);
frame.validate();
frame.setVisible(true);
I know that this is just a quick example for you, but I think it should do for explanation... so just add everything on the backgr JLabeland your good to go. Quick and dirty example but the a way to go.
I'm currently using Luna Eclipse and Windows Builder to create a GUI.
I am using a static layout and have added a JPanel and my question is... How do i set the width of my JPanel to 100% of the window.
My code so far:
JPanel panel = new JPanel();
panel.setBackground(new Color(30, 144, 255));
panel.setBounds(0, 0, 643, 54);
panel.setSize(643,50);
frame.getContentPane().add(panel);
Using the panel.setSize() i can edit the width, as you can see i have 643. But I want to change this too 100% of the window.
Could someone please explain to me how to do this.
Thanks in advance,
Sam
Use BorderLayout:
frame.getContentPane().add(panel, BorderLayout.PAGE_START);
I know this question was asked before, but there was no answer to it.
In Java, when I am adding components to the frame, all the elements after adding a JTextField are not rendered when the application is initialized. They are rendered after you refresh the screen e.g. minimize and maximize the screen. In the following only the textfield is rendered. It looks like some Java rendering issue.
My code is as follows:
private void initialize() {
frame = new JFrame();
frame.setVisible(true);
frame.setBounds(100, 100, 569, 321);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
txtGenerationRate = new JTextField();
txtGenerationRate.setBounds(322, 29, 86, 20);
frame.getContentPane().add(txtGenerationRate);
txtGenerationRate.setColumns(10);
lblAmountOfSolarPanelsText = new JLabel("Amount of solar panels:");
lblAmountOfSolarPanelsText.setBounds(10, 57, 159, 14);
frame.getContentPane().add(lblAmountOfSolarPanelsText);
frame.setVisible(true); // added it for the second time, just to make sure
}
Can anyone help, please?
Piotr
At first, you should definitely read about how to use layouts: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
The second thing, you should set your frame visible after all components are added, if you do not, you have to call repaint().
Try putting this at the bottom of the initialize method
frame.setVisible(true);
frame.setBounds(100, 100, 569, 321);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
you could also try frame.revalidate(); and frame.repaint();
This seems working for me, I see both label and text field when the window first appears. The label text does not fit into label so is truncated, but apart that everything looks as expected. Maybe the wrong fragment has been removed from the code while trying to simplify it?
I have a JTextArea inside of a JPanel that is then placed into the JScrollPane. When the JPanel that contains the JScrollPane is first show the JScrollPane shows up but not the contents. As soon as the JFrame is resized the contents show up.
JTextArea area = new JTextArea(6, 20);
area.setText("Some test text");
JPanel panel = new JPanel(new BorderLayout());
panel.add(area, BorderLayout.CENTER);
JScrollPane pane = new JScrollPane();
pane.setBounds(20, 20, WIDTH - 40, 300 - 40);
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40));
add(pane);
pane.setViewportView(panel);
pane.setBounds(20, 20, WIDTH - 40, 300 - 40);
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40));
Those two lines of code doen't make sense (although they are not the cause of your problem)
The first line is used when you are using a "null layout".
The second is used when you are using layout managers.
They should not be used together.
The second is preferred since you should be using layout managers.
In the application different JPanels are swapped out in a manner similar to a slide-show. So something like this would be found in the code:
panel.remove(slide1);
panel.add(slide2);
panel.repaint();
The problem being that all of the contents of the second slide, slide2, would not show up. The solution is to add
frame.validate();
Where frame is the parent window of panel.
new JScrollPane(panel);
I believe that you need to add the panel to the scroll pane constructor.