How can I move a table downward in Java? - java

Is it possible to move a table downward in Java. I tried setBorder, setLocation, ... but it didn't work. What should I do?
JLabel label = new JLabel("Enter proper data: ");
label.setBounds(0, 0, 120, 50);
frame.add(label);
JButton btn = new JButton("Click");
btn.setBounds(100, 300, 80, 50);
frame.add(btn);
String data [][] = new String [6][4];
String column[]={"No.","Player 1","Player 2","Strategy"};
JTable table = new JTable(data, column);
JScrollPane sp=new JScrollPane(table);
frame.add(sp);
frame.setSize(300,400);
frame.setVisible(true);
Please look at this image:

Looks good for a BorderLayout for the outer panel (outlined in blue). The big red label goes in the PAGE_START and the (scroll pane of the) table goes in the CENTER.
Put the button in a panel with a FlowLayout (centered), the panel with the red outline, in the PAGE_END of the border layout. Done.
All extra height (if the user makes the GUI bigger) will be given to the table.

I changed the code based on #AndrewThompson suggestion and result was good.
JFrame frame = new JFrame("Play");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel outerPanel = new JPanel(new BorderLayout());
JPanel topPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Enter proper data: ");
JButton btn = new JButton("Click");
String data [][] = new String [100][4];
String column[]={"No.","Player 1","Player 2","Strategy"};
JTable table = new JTable(data, column);
JScrollPane sp=new JScrollPane(table);
topPanel.add(label, BorderLayout.PAGE_START);
topPanel.add(sp, BorderLayout.CENTER);
topPanel.add(btn, BorderLayout.PAGE_END);
outerPanel.add(topPanel);
frame.add(outerPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

Related

Java swing scrollpane doesn't show the scroll bar

I was working on a java swing gui project for my course. When I was doing that, I found that I had too much information in a panel and it was not able to show everything. As a result, I wanted to add a Jscrollpane and I did some research about how to use this function but it didn't seem to work for my project, and I had no reason why even after I tried almost everything I could find on google.
Here is my code:
JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(10, 1100));
musiclist.setBounds(0, 0, 190, 1000);
musiclist.setLayout(null);
listpanel.setLayout(null);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
listpanel.add(selectButton);
c.add(musiclist, BorderLayout.WEST);
frame.setVisible(true);
musiclist.setVisible(true);
The problem now is that the scroll bar never shows up even I set the vertical to always show. I am just new to java, so any help will be helpful and thank you all for your time!
JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(100, 1100));
musiclist.setBounds(0, 0, 190, 1000);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
JList mlist=new JList();
DefaultListModel lmodel=new DefaultListModel();
lmodel.addElement("Song 1");
lmodel.addElement("Song 2");
lmodel.addElement("Song 3");
mlist.setModel(lmodel);
listpanel.setLayout(new BorderLayout());
listpanel.add(mlist, BorderLayout.CENTER);
listpanel.add(selectButton, BorderLayout.SOUTH);
c.add(musiclist, BorderLayout.WEST);
musiclist.setVisible(true);
frame.setVisible(true);

JFrame/BoxLayout weird size behavior

I am trying to set the sizes of my JButtons in a JPanel with BoxLayout correctly, but the behavior is beyond weird.
It will take the height from JButton.setPreferredSize, but completely ignore the width. This also only works when all buttons are set to the same height. As soon as one is smaller, it will revert all of them to some random minimum size (which isn't even the same for all buttons)
My code is this:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 500);
JPanel rightPanel = new JPanel();
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
JButton bBookmarks = new JButton("Bookmarks");
bBookmarks.setPreferredSize(new Dimension(200, 100));
//more buttons with same size
leftPanel.add(bBookmarks);
//more buttons
JSplitPane mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
mainPanel.setDividerLocation(200);
frame.add(mainPanel);
frame.setResizable(false);
frame.setVisible(true);
This creates this image.
The middle button is always wider than the rest as well. Using frame.pack() doesn't do anything except resizing the frame because the right panel is empty.
What am I doing wrong?
Edit: Should look like this:
Divide and conquer: break the design into small, easy to layout containers. In this case do not place the buttons directly in the left (BoxLayout) container but in a nested JPanel using GridLayout manager.
This ensures that all buttons have the same size:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
//add all buttons to a panel using a GridLayout which shows all components having the same size
JPanel buttons = new JPanel(new GridLayout(0,1));
JButton bBookmarks = new JButton("Bookmarks"); buttons.add(bBookmarks);
JButton bPlotter = new JButton("Plotter"); buttons.add(bPlotter);
JButton bShips = new JButton("Ships"); buttons.add(bShips);
//add buttons and text area to a panel using BoxLayout
JPanel leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(100,400));
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
leftPanel.add(buttons);
leftPanel.add(new TextArea(10,30));
JPanel rightPanel = new JPanel();
rightPanel.setPreferredSize(new Dimension(600,400));
rightPanel.add(new JLabel("right pane"));
JSplitPane mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true, leftPanel, rightPanel);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);

JPanels: One with a JTextArea and another with a JLabel

I've been at this for a good while now, but I cannot seem to get a hand of it. I'm trying to produce a JPanel that has a JTextArea above and two JLabels below, but my JLabel ends up on the left side of my JTextArea and I cannot make the other appear.
Here's my code (sorry for the display stuff- just filler really):
public JPanel contentPane() {
JPanel something = new JPanel();
String information = "Please";
info = new JTextArea(information, 4, 30);
info.setEditable(false);
info.setLineWrap(true);
info.setWrapStyleWord(true);
JPanel one = new JPanel(new BorderLayout());
one.setBackground(Color.WHITE);
one.setLocation(10, 10);
one.setSize(50, 50);
one.add(info, BorderLayout.CENTER);
something.add(one, BorderLayout.NORTH);
JPanel two = new JPanel(new BorderLayout());
two.setBackground(null);
two.setLocation(220, 10);
two.setSize(50, 50);
two.add(new JLabel("Please work"), BorderLayout.EAST);
two.add(new JLabel("Oh gosh, please"), BorderLayout.WEST);
something.add(two, BorderLayout.SOUTH);
something.setOpaque(true);
return something;
}
public static void GUI() {
JFrame frame = new JFrame("You Guessed It!");
DisplayStudent panel = new DisplayStudent();
frame.setContentPane(panel.contentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 150);
frame.setVisible(true);
}
Please and thank you to anyone who takes the time to help.
When you create you something, you don't specify any layout manager, but later on you attempt to add one to something using BorderLayout constants -- which will not work, since the default layout manager for a JPanel is FlowLayout.
Try this instead;
JPanel something = new JPanel(new BorderLayout());

Adding components into JPanel inside a JFrame

Since im a beginner and i don't want to get involved with the layout managers, i was simply adding a JPanel into my main JFrame and giving spesific location to each component in the panel. But somehow the output appears way too wrong..
frame = new JFrame(email + " (Offline)");
frame.setSize(400, 400);
frame.setLocation(0, 0);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
// out.println("BYE");
// out.flush();
frame.dispose();
thread.stop();
}
});
panel = new JPanel();
frame.add(panel);
chat = new JTextArea();
chat.setSize(400, 200);
chat.setLocation(0, 0);
chat.setEditable(false);
panel.add(chat);
panel.validate();
JLabel you = new JLabel("You:");
you.setSize(you.getPreferredSize());
you.setLocation(0, 210);
panel.add(you);
panel.validate();
input = new JTextArea();
input.setSize(200, 200);
input.setLocation(0, 220 + chat.getSize().height);
panel.add(input);
panel.validate();
send = new JButton("Send");
send.setSize(send.getPreferredSize());
send.setLocation(210, 220 + chat.getSize().height);
panel.add(send);
panel.validate();
frame.setVisible(true);
The outcome of this frame is that text areas are invisible, a You: label in the middle and next to the right of it the button.. What am i missing here?
Again, don't use null layout since it makes updating and maintaining your GUI much more difficult than it should be, and can lead to ugly GUI's if you plan on having them run on multiple platforms. Instead
Use several JPanels, each one holding a core group of components and each using its best layout manager
Nest these JPanels in other JPanels that use the best layout manager to display them
and that will allow your GUI to be resizeable without need of extra code.
Put your JTextAreas in JScrollPanes so that you can see all text even if it goes beyond the text area.
Never set the size of the JTextArea as that will not allow it to scroll. Instead set its columns and rows.
As a very simple example, run this to see what I mean:
import java.awt.*;
import javax.swing.*;
public class FooSwing2 {
public static void main(String[] args) {
JTextArea chatArea = new JTextArea(8, 40);
chatArea.setEditable(false);
chatArea.setFocusable(false);
JScrollPane chatScroll = new JScrollPane(chatArea);
JPanel chatPanel = new JPanel(new BorderLayout());
chatPanel.add(new JLabel("Chat:", SwingConstants.LEFT), BorderLayout.PAGE_START);
chatPanel.add(chatScroll);
JTextField inputField = new JTextField(40);
JButton sendBtn = new JButton("Send");
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.LINE_AXIS));
inputPanel.add(inputField);
inputPanel.add(sendBtn);
JPanel youLabelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
youLabelPanel.add(new JLabel("You:"));
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
mainPanel.add(chatPanel);
mainPanel.add(Box.createVerticalStrut(10));
mainPanel.add(youLabelPanel);
mainPanel.add(inputPanel);
JFrame frame = new JFrame("Foo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
This would result in a simple (non-functioning) GUI that looked like this:
Now say you want to change this and add another button, an "exit" JButton to the right of the send JButton. If you used null layout, you'd have to resize your GUI, you'd have to move the send button over to the left and make sure that your math was without error, etc. If you used layout managers, you'd need just two new lines of code (to change the display, not the functionality of course):
JTextField inputField = new JTextField(40);
JButton sendBtn = new JButton("Send");
JButton exitBtn = new JButton("Exit"); // ***** added
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.LINE_AXIS));
inputPanel.add(inputField);
inputPanel.add(sendBtn);
inputPanel.add(exitBtn); // ***** added
That's it, and this would display:

JTable will not auto resize last column

I have a JTable that is placed inside a JPanel which is then placed in a JFrame that has another JPanel that has a ScrollPane. The general idea can be seen below if the explanation is confusing. I have set my JTable to auto resize the last column, but it never auto sizes. What is the problem?
JFrame -> JPanel -> JTable
-> JPanel -> Scroll Pane
My code:
this refers to my class which extends JFrame
this.setLayout(new BorderLayout());
JTextArea log = new JTextArea(20, 50);
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
String[] configRow = {"Config File", "Not Loaded"};
String[] logRow = {"Log File" , "Not Loaded"};
DefaultTableModel dtm = new DefaultTableModel();
dtm.addColumn("");
dtm.addColumn("");
dtm.addRow(configRow);
dtm.addRow(logRow);
JTable status = new JTable(dtm);
status.setTableHeader(null);
status.setShowGrid(false);
status.setEnabled(false);
status.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
JPanel tablePanel = new JPanel();
tablePanel.setLayout(new BorderLayout());
tablePanel.add(status, BorderLayout.LINE_START);
JPanel logPanel = new JPanel();
logPanel.add(logScrollPane);
this.add(tablePanel, BorderLayout.NORTH);
this.add(logPanel, BorderLayout.SOUTH);
Your JTable must be placed inside a JScrollPane in order for the resizing to work properly. You are placing the JTable directly on a JPanel.
tablePanel.add(new JScrollPane(status), BorderLayout.LINE_START):

Categories

Resources