I want to set every element location with .setLocation(x, y)
I need a JPanel in a JScrollPane. But when I add components to JPanel, only buttons are showing. But not JLabel.
Method below is calling in JFrame constructor:
private void initGUI_test() {
this.setSize(950, 700);
this.setResizable(false);
this.getContentPane().setLayout(null);
JScrollPane mainScroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel container = new JPanel();
mainScroll.setSize(900,500);
mainScroll.setLocation(0,100);
mainScroll.setBorder(BorderFactory.createLineBorder(Color.blue));
mainScroll.add(container);
container.setLayout(null);
container.setLocation(0, 0);
container.setSize(900, 500);
JLabel rowA = new JLabel();
rowA.setSize(180, 26);
rowA.setLocation(10, 100);
rowA.setText("Row A");
JButton loadButton = new JButton();
loadButton.setSize(180, 34);
loadButton.setLocation(290, 110);
loadButton.setText("Load file");
container.add(rowA);
container.add(loadButton);
this.getContentPane().add(mainScroll);
}
Although I agree completely with #Frakcool about null layout, the problem you are facing has a different source. You should not add components directly into JScrollPane, but into JScrollPane's ViewPort.
The line mainScroll.add(container); should be mainScroll.getViewport().add(container);
Related
I have been working on this for hours. I honestly cannot figure it out. I have JTextArea's inside a JSplitPane which is inside a JPanel with a JButton and all that is put in my JFrame. I am using Layout managers. I have tried using pack(). I have tried using preferred sizes. Without the JPanel my button does not display in the proper location or switch buttons in other Tabs. With the JPanel it cuts off all my text, stops the scroll function(yes I have tried setting the TextAreas to always have horizontal and vertical scroll bars...does not solve the problem where text just stops wrapping for no apparent reason).
public static void main(String[] args) throws IOException
{
JFrame frame = new JFrame();
Deck blackjack = new Deck(Deck.TYPE[0]);
JTextArea textBlackjackUnshuffled = new JTextArea();
JTextArea textBlackjackShuffle = new JTextArea();
JButton shuffleButtonBlackjack = new JButton(new ImageIcon(ImageIO.read(new File("res/shuffle.png"))));
JToolBar toolBarBlackjack = new JToolBar("Blackjack");
JSplitPane splitPaneBlackjack = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JPanel panel = new JPanel();
JTabbedPane tabbedPaneBlackJack = new JTabbedPane();
JTabbedPane tabbedPaneCanasta = new JTabbedPane();
JTabbedPane tabbedPanePinochle = new JTabbedPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
textBlackjackUnshuffled.setColumns(10);
textBlackjackUnshuffled.setLineWrap(true);
textBlackjackUnshuffled.setWrapStyleWord(true);
textBlackjackUnshuffled.setEditable(false);
textBlackjackUnshuffled.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackUnshuffled.append(blackjack.toString());
textBlackjackShuffle.setColumns(10);
textBlackjackShuffle.setLineWrap(true);
textBlackjackShuffle.setWrapStyleWord(true);
textBlackjackShuffle.setEditable(false);
textBlackjackShuffle.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackShuffle.append(blackjack.toString());
shuffleButtonBlackjack.setBorderPainted(false);
shuffleButtonBlackjack.setFocusPainted(false);
shuffleButtonBlackjack.setContentAreaFilled(false);
splitPaneBlackjack.add(new JScrollPane(textBlackjackUnshuffled));
splitPaneBlackjack.add(new JScrollPane(textBlackjackShuffle));
panel.add(splitPaneBlackjack, BorderLayout.CENTER);
panel.add(shuffleButtonBlackjack, BorderLayout.PAGE_END);
tabbedPaneBlackJack.addTab("Blackjack", panel);
frame.add(tabbedPaneBlackJack);
frame.setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));
frame.setVisible(true);
}
You're adding the JScrollPanes to the panel in BorderLayout positions, but have not set the layout manager of panel to BorderLayout. In this situation, panel will be using JPanel's default layout manager, FlowLayout, a manager which is not smart enough to respect the scroll pane's preferred sizes.
Your code needs:
panel.setLayout(new BorderLayout());
I have the next code:
public class MyScroll extends JFrame {
public MyScroll() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(null);
for (int i = 0; i < 10; i++) {
JButton b = new JButton("Hello-" + i);
b.setBounds(0, i * 50, 100, 45);
panel.add(b);
b.setLayout(null);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(50, 30, 100, 325);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(500, 400));
contentPane.setBackground(Color.BLUE);
contentPane.add(scrollPane);
contentPane.setLayout(null);
setContentPane(contentPane);
pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
And, it is rendering this:
As you can see, the vertical and horizontal scroll aren't working, but both are defined and are displaying inside the JPanel.
Can someone explain me what am I doing wrong?
This code is based on this one:
Scrolling a JPanel
But, the is not working at the moment to use the verticall scrolling
Can someone explain me what am I doing wrong?
panel.setLayout(null);
Don't use a null layout.
The scrollbars will only appear automatically when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane.
It is the job of the layout manager to determine the preferred size of the panel. Since you don't use a layout manager the preferred size isn't calculated.
So the solution is to use a Layout Manager. Maybe vertical BoxLayout.
I have a JFrame with three areas:
A scrollpane with a list of objects
A panel with labels and textfields
A scrollpane with a panel potentially having multiple labels
When you click the item on the list, the textfields on the panel are filled and the labels on the second scroll are created. I have two problems with my code:
For some reason the scrollpane at the botom of the screen does not fill the whole borderlayout's south area, only half of it.
the scrollpane does not show anything when the item on the list is selected.
Here I tried to make an example:
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
JPanel geral = new JPanel();
JPanel lista = new JPanel();
JPanel dados = new JPanel();
JPanel paneHist = new JPanel();
JPanel historico = new JPanel();
GridLayout gridLay = new GridLayout(0, 2, 5, 10);
geral.setLayout(gridLay);
dados.setLayout(gridLay);
historico.setLayout(new BorderLayout());
lista.setLayout(new BorderLayout());
paneHist.setLayout(gridLay);
this.setLayout(new BorderLayout());
this.add(geral);
geral.add(lista, BorderLayout.WEST);
geral.add(dados, BorderLayout.EAST);
geral.add(historico, BorderLayout.SOUTH);
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("just testing");
final JList list = new JList(listModel);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisible(true);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scroll = new JScrollPane(list);
scroll.setPreferredSize(new Dimension(100, 500));
lista.add(scroll, BorderLayout.CENTER);
JTextField jtf = new JTextField();
dados.add(new JLabel("test:"));
dados.add(jtf);
list.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent lse) {
jtf.setText("clicked");
paneHist.add(new JLabel("texttexttext"));
paneHist.add(new JLabel("texttexttext"));
}}
);
JScrollPane scrollHist = new JScrollPane(paneHist);
scrollHist.setPreferredSize(new Dimension(500, 100));
historico.add(new JLabel("Historico:"), BorderLayout.NORTH);
historico.add(scrollHist, BorderLayout.EAST);
//list.setCellRenderer(new CellRenderer());
this.validate();
this.repaint();
}
Can't really tell what you are doing from the posted code.
Some general comments:
Don't use setPreferredSize(). Let each component determined its preferred size. In the case of a JList you can use the setVisibleRowCount(...) method so the JList can calculate a reasonable size.
In your ListSelectionListener, when you add/remove components from a visible GUI you need to revalidate() and repaint() the panel.
Here is code:
ScreenHeight = Toolkit.getDefaultToolkit().getScreenSize().height,
ScreenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
JFrame MainFrame = new JFrame();
MainFrame.setSize(ScreenWidth, ScreenHeight);
MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
MainFrame.setVisible(true);
/* When set to false, all buttons and boxes are displayed,
otherwise only the main window appears */
MainFrame.setUndecorated(true);
Container Pane = Frame.getContentPane();
Pane.setLayout(new MigLayout());
initLoginPanel(Pane);
The function that lays out the controls:
private void initLoginPanel(Container Obj)
{
JPanel LoginContainer = new JPanel();
LoginContainer.setLayout(new MigLayout());
Obj.add(LoginContainer, "pos 0.5al 0.5al");
JLabel uNameLabel = new JLabel("Username");
JTextField uNameBox = new JTextField();
JLabel uPassLabel = new JLabel("Password");
JTextField uPassBox = new JTextField();
JButton LoginButton = new JButton("Login", 90, 26);
LoginContainer.add(uNameLabel, "wrap");
LoginContainer.add(uNameBox, "span");
LoginContainer.add(uPassLabel, "wrap");
LoginContainer.add(uPassBox, "span");
LoginContainer.add(LoginButton, "");
}
If, in the above code, MainFrame.setUndecorated(false) is used, it works fine but no full screen. That is the title bar, close, minimize and maximize buttons are displayed.
Question:
1. How can I get the components working in fullscreen mode.
There are two problems:
The position of setVisible
Showing the frame must be the last step;
first you must setup your frame and add his content.
The call to initLoginPanel
Your code is doing incorrect things. Why don't you add the components directly to the frame? i.e.
initLoginPanel( YourJFrame );
Fixed, simplified code:
JFrame frame = new JFrame("Main window");
frame.setSize( Toolkit.getDefaultToolkit().getScreenSize() );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(false);
initLoginPanel(frame);
frame.setVisible(true); //FINALLY show the JFrame!
I'd like to use a JScrollPane for a panel which has an arbitrary list of labels in it using box layout. I'm trying to get it so that the scrollbar would appear if there were too many items (labels) to display.
I tried adding a JScrollPane to the panel and then add the labels but then I don't see any scroll bar.
Any ideas?
TIA
For this kind of thing, you'd normally use a JList or JTable (if you need custom rendering).
Make sure that you call validate() or revalidate() on the JScrollPane after adding an item, to force the preferred size of the panel to be recalculated.
Here's how I did it.
JPanel midPanel = new JPanel();
midPanel.setLayout(new BoxLayout(midPanel, BoxLayout.Y_AXIS));
midPanel.add(new JLabel("<html><u>Label</u>"));
Box box = Box.createVerticalBox();
for (Item item : data.getInventory()) {
inventory.add(box.add(new JLabel(item.getName())));
}
JScrollPane jscrlpBox = new JScrollPane(box);
midPanel.add(jscrlpBox);
add(midPanel, BorderLayout.CENTER);
From:
http://www.java2s.com/Code/Java/Swing-JFC/JScrollPanetoholdscrollablecomponent.htm
Did you remember to set the preferred size of the content panel?
final JFrame frame = new JFrame("Scroll Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
final Box textArea = Box.createVerticalBox();
final JScrollPane textAreaScroll = new JScrollPane(textArea);
textAreaScroll.setPreferredSize(new Dimension(80,150)); /* essential! */
JButton addButton = new JButton("ADD");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.add(new JLabel("abc"));
textArea.revalidate();
}
});
frame.getContentPane().add(textAreaScroll, BorderLayout.SOUTH);
frame.getContentPane().add(Box.createRigidArea(new Dimension(10,10)), BorderLayout.CENTER);
frame.getContentPane().add(addButton, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
In this example, the scroll bar works correctly, but if you remove the line marked as "essential", it will not work anymore.