Reputation: 1421
My JList in my Java program is taking up the whole JPanel! What do I do?
package infoKupProcess;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
class TCPServer extends JFrame {
public static String processes = "none";
static String processesList[] = processes.split(":");
private static int screenWidth = 800, screenHeight = 600;
static JButton quitButton;
static JScrollPane procScroll;
static ServerSocket inSocket;
private static boolean running = false;
private static JList procList;
public TCPServer() {
initUI();
}
public void initUI() {
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(new BorderLayout());
setSize(800, 600);
// {
// String listData[] = { "Item 1", "Item 2", "Item 3", "Item 4" };
//
// // Create a new listbox control
// procList = new JList(listData);
// panel.add(procList, BorderLayout.CENTER);
// }
{
quitButton = new JButton("Ugasi");
quitButton.setBounds(705, 533, 80, 30);
quitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
running = false;
try {
inSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
}
});
panel.add(quitButton);
}
// setTitle("TCPServer");
// setSize(screenWidth, screenHeight);
// setLocationRelativeTo(null);
// setDefaultCloseOperation(EXIT_ON_CLOSE);
// Set the frame characteristics
procList = new JList();
procList.setListData(processesList);
procScroll = new JScrollPane(procList);
panel.add(procScroll);
}
public static void main(String argv[]) throws Exception {
running = true;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TCPServer ex = new TCPServer();
ex.setVisible(true);
}
});
processProcesses();
}
public static void processProcesses() throws Exception {
String clientSentence, capitalizedSentence;
inSocket = new ServerSocket(25565);
while (running) {
Socket connectionSocket = inSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
clientSentence = inFromClient.readLine();
processes = clientSentence;
processesList=processes.split(":");
procList.setListData(processesList);
DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
Upvotes: 1
Views: 405
Reputation: 20755
This may solve your problem, but I recommend you to use a proper Layout Manager
Replace
panel.add(procScroll);
By
procScroll.setPreferredSize(new Dimension(150,80));
JPanel panel1=new JPanel();
panel1.add(procScroll);
panel.add(panel1);
Upvotes: 0
Reputation: 285415
You're adding your list's JScrollPane to a BorderLayout using JPanel's default BorderLayout.CENTER position. So it will fill the JPanel. Solution: use another layout, or better yet, place the BorderLayout using JPanel in another JPanel that uses a decent layout.
As an aside, other problems:
pack()
on the JFrame prior to setting it visible.Upvotes: 3