I am developing a small desktop application in Java using Netbeans. On my jframe i have various pannels and one scroll panes. The purpose of this JScrollPane is to show some visual elements to its users. I achieve this by following the below steps in sequence:
Drag and drop JScrollPane at desired location of my JFrame
Adjust the size of JScrollPane according to my needs.
Write a new java class and extend that class with JPanel
Override the public void paintComponent(Graphics g) method
Then i add that panel to above JScrollPane,
using following code:
JPanel jpnl = new myClass();
jScrollPane2.setViewportView(jpnl);
jScrollPane2.repaint();
Now every thing is working fine as per my requirements, the only thing which is lacking is that when my drwaing is big then no sroll bars are shown at JScrollPane. This is my first application and i don't know much about Java, so any guidence regarding what is missing would be highly appreciated
Remember to add the required component to the JScrollPane object, and the scroll pane object to the panel. Also, it could be that you need to change the scroll bar policy: use scroll pane's setHorizontalScrollBarPolicy() and setVerticalScrollBarPolicy().
Consult the JScrollPane documentation for these methods.
Related
I´m creating a TileMap out of JLabels and my Problem is that the last Tile is not placed right.
For some reason the last JLablel is on a wrong position. I think this Image:
and the Code explains what my problem is.
Btw. I am using BlueJ (maybe there is a bug in it, that I dont know)
Thanks for help!!!
//create Frame
this.setSize((size+2)*imageSize+16,(size+2)*imageSize+39);
this.addKeyListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setBackground(Color.black);
this.setVisible(true);
//create MapJLabels
for(int x = 0;x<size;x++){
for(int y = 0;y<size;y++){
mapJLabels[x][y] = new JLabel();
this.add(mapJLabels[x][y]);
mapJLabels[x][y].setSize(imageSize,imageSize);
mapJLabels[x][y].setLocation((x+1)*imageSize,(y+1)*imageSize);
mapJLabels[x][y].setVisible(true);
}
}
The code is attempting to set the size/location of the component. This implies you are using a null layout. Don't. Swing was designed to be used with layout managers. You should use the GridLayout. Read the section from the Swing tutorial on Layout Managers for more information.
You create an empty JLabel. By default a JLabel is transparent. I'm not sure now the picture you posted is created from the code you posted. I don't see how the grid is populated with images etc.
Swing components are visible by default so there is no need for the setVisible(true)
Your code attempts to set the size of the frame. Again, don't. When using layout managers you just pack() the frame and the size will be calculated automatically.
Your code attempts to leave space between the frame and labels. Don't. If you want extra space you can add an EmptyBorder to the panel. Read the section from the Swing tutorial on How to Use Borders
You code is using a KeyListener. Don't. Swing was designed to be used with Key Bindings
Components should be added to the frame BEFORE the frame is made visible.
The posted code looks reasonable, but since we can't see where you change the background of the labels or where you add icons to the labels, we don't know what the rest of your logic is doing. The problem could be there.
If you need more help after fixing the above problems then post a proper minimal, reproducible example that demonstrates your new problem.
I currently have a row of JPanels that I add to a JFrame. I need these panels to be able to expand and collapse as to minimize the clutter on the JFrame.
Right now, I just create the JPanels and add them to the JFrame one at the time.
public void addFolderSearch(FolderSearchComp fsc) {
folderCompPanel.add(fsc, folderCompPanel.getComponentCount());
remake();
}
I've tried adding these FolderSearchComps (the JPanels) to a TitledPane and then adding the TitledPane to the JFrame, but the titledpane.setContent() wont take the JPanel as a parameter.
Is there any way to add a JPanel to a TitledPane and then adding that TitledPane to the JFrame?
is there any swing component similar to the titledpane? or any other
workaround to expand and collapse the jpanel
There is a complete set of Swing components extension in SwingX library which includes titled pane, task pane and collapsible pane, just to mention the ones that you are looking for.
Take a look to this related Q&A: How to create expandable panels using swing?. You might also want to have a look to the demos: SwingLabs demos
I´ve created through NEtBeans design of jframe, when its components is created automatically.
Now I put on this jFrame component jLayeredPane called agendaLayer, cause I need more panes here and switch.
I´ve set horizontal and vertical resize to layout that component belong so it is automatically resize to some value when windows (jFrame) is resized..
Then I´ve created also through designer new class stock which extends jPanel,
now I put this jPanel to JLayredPane and need to get its properties about resizable..
stock st = new stock();
st.setBounds(0,0,agendaLayer.getWidth(),agendaLayer.getHeight());
agendaLayer.add(st);
But It did not work, jLayredPane is automatically resized when window is changed, but jPanel not it remains the same..
, jLayredPane is automaticaly resized when window is changed, but jPanel not it remains the same..
A JLayeredPane uses a null layout by default so components are never resized.
cause I need more panes here and switch.
If you need to switch panels then use a CardLayout. See the Swing tutorial on Using a Card Layout for more information and examples.
You should look into layout managers:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
So I am working on a GUI application using the swing framework. In short, I have 3 JPanels that act as different views of my application. Now the problem is that no matter the order I add the JPanels to my JFrame, only the final JPanel I add resizes when I switch to that view.
Some relevant bits of code:
When creating the window, I first create each individual JPanel, and add it to the JFrame:
JPanel newPanel = new SomeClassExtendingJPanel();
this.jframe.add(newPanel);
Next, whenever I switch between views of the application, I hide the panel that is currently active:
jframe.validate();
jframe.repaint();
oldPanel.setVisible(false);
And then activate the to be shown panel:
jframe.validate();
jframe.repaint();
newPanel.setVisible(true);
Does anyone know what could be wrong?
To resize the JFrame with each swap, you could call pack() on it, but this is kludgy having a GUI resize all the time. A better solution is to use the mechanism that Swing has for swapping views -- a CardLayout. This will size the container to the largest dimension necessary to adequately display all of the "card" components.
Check out the CardLayout tutorial and the CardLayout API for more on this.
JFrame's ContentPane has implemented BorderLayout by Default, and there is possible to put only one JComponents to the one Area,
you have to change used LayoutManager or put another JPanels to the other Areas
i have an imageViewer and i want to change the imagePanel into a TabbedPane so i can have more than one image open at the same time but have each of them in a different tab and be able to add and remove the tabs as well.
what i have:
private ImagePanel imagePanel; //<< i tried to change the ImagePanel to JTabbedPane but it doesn't work and it gives error in other parts of the codes that i have.
// Create the image pane in the center
imagePanel = new ImagePanel();
imagePanel.setBorder(new EtchedBorder());
contentPane.add(imagePanel, BorderLayout.CENTER);
the code above is for the image pane where the image is displayed.
is it possible to change the image pane to a tabbedPane without changeing other bits in the code?
i have looked at alot of tabbedpane examples but i don't know how to combine the code with my code to get it to work but i did put a few code examples into my program but the frame changes to tabbedpane and not the image pane where the image is displayed. i want to change the inner frame to tabbed not the actual frame of the program.
any suggestions on how should i go about it or if you can show me a simple example that could work.
thank you
I imagine that your ImagePanel extends JPanel. You don't "change" this into a JTabbedPane, and in fact you can't, but rather you put it into a JTabbedPane. Please check out the tutorial on tabbed panes that will show you how to do this: How to use Tabbed Panes
One caveat: if you are using NetBeans drag-and-drop GUI creation tools to generate your Swing code, I advise you not to do this but rather to go through the Swing tutorials (one is linked to above) and learn to code Swing by hand. You won't regret this, and the extra knowledge gained can be used if later you need to create gui's with the NetBeans tool.