I am new to Java and learning GUI now a days. I want to add space on top of may FirstPlayer name I am using JPanel with GridLayout but when I add invisible box as the first element to set my elements in the center but I got nothing as my desire. Please help me.
Here is my code:
JPanel main = new JPanel();
GridLayout layout = new GridLayout(6,1);
layout.setVgap(10);
JPanel parentPanel = new JPanel(layout);
parentPanel.setOpaque(false);
parentPanel.add(Box.createRigidArea(new Dimension(80,0)));
parentPanel.add(main.getFirstName());
parentPanel.add(main.getFirstField());
parentPanel.add(Box.createRigidArea(new Dimension(20,0 )));
parentPanel.add(main.getSecondName());
parentPanel.add(main.getSecondField());
main.add(parentPanel,BorderLayout.CENTER);
JFrame frame = new JFrame("Player Menu");
frame.add(main,BorderLayout.CENTER);
frame.add(Box.createRigidArea(new Dimension(100,0 )),BorderLayout.NORTH);
frame.setVisible(true);
frame.pack();
frame.setSize(900,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Related
I am working with JDBC.
My Class has a JFrame with a JTabbedPane to display my JPanels with the UI's for my different methods.
On this panel I want to display a result set in a JTable with to additional columns of buttons.
This is all currently working when I display it on a new JFrame but not when I try to display it on the existing one. Could somebody please explain why.
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
contentPane.add(tabbedPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
tabbedPane.addTab("Main", null, panel, null);
panel.setLayout(null);
JTable table = new JTable(model);
DisplayButtonColumn testWithButtons1 = new DisplayButtonColumn(table,
displayHandler.getColCount());
DisplayButtonColumn testWithButtons2 = new DisplayButtonColumn(table,
displayHandler.getColCount() + 1);
panel.add(new JScrollPane(table));
// JFrame f = new JFrame();
// f.setSize(1000, 500);
// f.getContentPane().add(new JScrollPane(table));
// f.setVisible(true);
panel.setLayout(null);
The problem is the null layout. Don't do that. Swing is designed to be used with layout managers and not doing that will lead to trouble almost without exception. Using absolute layout would require you to manually manage the bounds to the components, and you're not doing that. If you need something else than the default FlowLayout, just create one that matches your needs:
panel.setLayout(new BorderLayout());
(It would be also possible to add to JTabbedPane without the helper panel, but I suppose you want more components in the tab).
i am doing a small Gui in java. i am using setBounds methods to set the position of buttons etc on my JFrame , but problem is that when i use it with JPanel button is not visible on JFrame , and without JPanel its quite ok ,, see both the codes and please help me as i am beginner and facing these foolish problems .
This one is working fine
JFrame jframe = new JFrame("Working Fine");
jframe.setLayout(null);
JButton jbutton = new JButton("Position Test");
jbutton.setBounds(0, 0, 100, 100);
jframe.add(jbutton);
jframe.setSize(300,300);
jframe.setVisible(true);
Same code when i add Button to Jpanel then it does not work so whats wrong , please guide me
JFrame jframe = new JFrame("causing problem ");
jframe.setSize(300,300);
JPanel p = new JPanel();
jframe.setLayout(null);
JButton jbutton = new JButton("Position Test");
jbutton.setBounds(0, 0, 100, 100);
jframe.add(p);
p.add(jbutton);
p.setVisible(true);
//jframe.add(jbutton);
jframe.setVisible(true);
please help me in this small problem
You must get rid of the JPanel's layout, in order to set absolute positions:
p.setLayout(null);
The problem is that when you use absolute positioning, the JPanel component has no default size so does not appear. To get it to appear you could do
JFrame frame = new JFrame("No Problem");
JPanel panel = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
};
};
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton button = new JButton("Position Test");
panel.add(button);
frame.add(panel);
frame.pack();
frame.setVisible(true);
From Doing Without a Layout Manager
Although it is possible to do without a layout manager, you should use a layout manager if at all possible. A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container's changing size, and to different locales.
The choice of layout manager will depend on how you wish to lay out the components.
See A Visual Guide to Layout Managers.
Is there any way that I could add panels in bottom-to-top order?..
I've tried some layout managers, but still i couldn't get it..
need help.
JPanel mainpanel = new JPanel();
JPanel panel_1 = new JPanel();
JPanel panel_2 = new JPanel();
JPanel panel_3 = new JPanel();
mainpanel.add(panel_1);
mainpanel.add(panel_2);
mainpanel.add(panel_3);
mainpanel.add(panel_4);
mainpanel.add(panel_5);
mainpanel.add(panel_n);
You should be able to specify an index for adding the panel. Ie:
mainpanel.add(panel_1, 0);
mainpanel.add(panel_2, 0);
mainpanel.add(panel_3, 0);
This will always add each panel in the first position.
MigLayout works here as well - and increases maintainabilty:
JPanel mainpanel = new JPanel(new MigLayout();
private void addPanel(JPanel newPanel) {
mainpanel.add(newPanel, "dock north");
}
Add the panel in order, In which order you want to display in the frame.
Let p1,p2,p3 are the subv-panels and p is main panel.
You want order of panel as folowing : p2,p3,p1
p.add(p2);
p.add(p3);
p.add(p1);
then finally add it to your frame.
I think this will help you.
I have a JTextField, and right below it I want to show a JLabel placed in a JLayeredPane (I will use it for autosuggestions later on).
How can I place my JLabel in JLayeredPane right below the JTextField?
Here is some code I have, and the current result shown in the screenshot below:
public static void main(String[] args) {
JTextField field = new JTextField();
JLabel lbl = new JLabel("Hello");
lbl.setBackground(Color.YELLOW);
lbl.setOpaque(true);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setLayout(new GridLayout(0,1));
layeredPane.add(lbl, (Integer) (JLayeredPane.POPUP_LAYER - 10));
layeredPane.setPreferredSize(field.getPreferredSize());
JPanel panel = new JPanel(new BorderLayout());
panel.add(field, BorderLayout.NORTH);
panel.add(layeredPane, BorderLayout.SOUTH);
JFrame frame = new JFrame();
frame.add(panel);
frame.setSize(200, 360);
frame.setVisible(true);
}
Second try:
public static void main(String[] args) {
JTextField field = new JTextField();
JLabel lbl = new JLabel("Hello");
lbl.setBackground(Color.YELLOW);
lbl.setOpaque(true);
lbl.setBounds(field.getBounds().x, field.getBounds().y,
field.getBounds().width, field.getBounds().height);
JPanel popPanel = new JPanel(new BorderLayout());
popPanel.add(lbl, BorderLayout.NORTH);
popPanel.setLocation(field.getLocation().x+10, field.getLocation().y+20);
popPanel.setPreferredSize(field.getPreferredSize());
JFrame frame = new JFrame();
JLayeredPane layeredPane = frame.getRootPane().getLayeredPane();
layeredPane.setLayout(new GridLayout(0,1));
layeredPane.add(popPanel, (Integer) (JLayeredPane.POPUP_LAYER - 10));
JPanel panel = new JPanel(new BorderLayout());
panel.add(field, BorderLayout.NORTH);
frame.add(panel);
frame.setSize(200, 360);
frame.setVisible(true);
}
Add the layeredPane to the "CENTER", not the SOUTH.
However, your understanding a layed pane seems to be a little confused. You use a layered pane when you want multiple components to be displayed on top (stacked?) of one another. You are still using the layered pane in 2 dimensions which is unnecessary. YOu can just use a panel for this.
If you want to popup a list of suggestions then you should just use a JPopupMenu and position it below the text field. Read the section from the Swing tutorial on Bringing up Popup Menus.
First of all, I don't think you should use a JLayeredPane for that, but just a permanent label.
If you do use a layered pane, you'll have to compute where the text field ends (y = field.getY() + field.getHeight()) and set your JPanel at 'panel.setLocation(0, y)' inside the JLayeredPane (provided the JLayeredPane has the same starting position as the underlying JFrame). You could equivalently position the JLayeredPane at (0, y) and put the label at (0, 0) within that layered pane.
You have to make sure this is done every time the components are resized.
why not using AutoComplete ComboBox / JTextField and if you don't want to display JComboBox, then there is AutoCompleted JTextField, and for somehow reduced autosuggestions, would be better look for undecorated JDialog/Window with JTable with one TableColum and without TableHeaded in the JScrollPane, just with plain RowSorter, very simle job
I want to show a textArea showing some text (will show log lines) , and have an animated gif hoovering above it. I tried the solution described here , but all I get is a grey screen. Hints?
public class TestLayeredPanes {
private JFrame frame = new JFrame();
private JLayeredPane lpane = new JLayeredPane();
public TestLayeredPanes() {
frame.setPreferredSize(new Dimension(600, 400));
frame.setLayout(new BorderLayout());
frame.add(lpane, BorderLayout.CENTER);
//Build the animated icon
JLabel buildingIcon = new JLabel();
buildingIcon.setIcon(new ImageIcon(this.getClass().getResource(
"/com/ct/tasks/cmviewer/gui/progress_bar.gif")));
JPanel iconPanel = new JPanel();
iconPanel.add(buildingIcon);
//Build the textArea
JTextArea textLog = new JTextArea("Say something");
JPanel textPanel = new JPanel();
textPanel.add(new JScrollPane(textLog));
//Add the panels to the layered pane
lpane.add(textPanel, 0);
lpane.add(iconPanel, 1);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new TestLayeredPanes();
}
}
Try putting your animated GIF on the glass pane of your root pane:
http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html
JXLayer make easier to do that. Look at JXLayer samples.
You also can take a look at code of XSwingX
Since you started with a working example, why did you remove lines of code from the example you copied?
Layered panes don't use a layout manager therefore the size of your components are (0, 0), so there is nothing to display. The setBounds(...) method in the example are there for a reason.