Swing components are too small - java

So I have this code in my contructor, my class extends JFrame:
super("myBucketList");
i = new Item();
//controls panel
controls = new JPanel();
itemField = new JTextField();
itemField.addActionListener(new ListenAction());
delField = new JTextField();
delField.addActionListener(new ListenAction());
listArea = new JTextArea();
listArea.setEditable(false);
controls.add(delField);
controls.add(itemField);
//txtareaPanel
textareaPanel = new JPanel();
listScroll = new JScrollPane();
listScroll.setViewportView(listArea);
textareaPanel.add(listScroll);
//tab1 panel
tab1 = new JPanel();
tab1.setLayout(new BorderLayout());
tab1.add(controls, BorderLayout.NORTH);
tab1.add(textareaPanel, BorderLayout.CENTER);
add(tab1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,200);
setVisible(true);
Now if I run my application the components are really small, how to fix it?
Here is a screenshot of the situation/problem: http://snag.gy/yjbUl.jpg
Thanks

Just call the constructor with columns, see here
For example:
itemField = new JTextField(10); //Or the number of cols you want
Or call JTextField#setColumns()
itemField.setColumns(10); //Or the number of columns you want
For the JTextArea call this constructor as follows:
listArea = new JTextArea("", 10, 10); //Text, rows, cols.
Or again use JTextArea#setRows() and JTextArea#setColumns() as follows:
listArea.setColumns(10);
listArea.setRows(10);
That should fix your problem.

If you are using jdk 8 then keep it but
download the jre 9 and your widgets
etc will be of normal size. This solves the
problem!

Related

Add 2 JTextPane to one scrollPane

I am trying to add 2 JTextPane to one scrollPane. But it not scrolling. What am I doing wrong?
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(402, 211, 178, 193);
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
panel.setLayout(null);
JTextPane textPane_branding = new JTextPane();
textPane_branding.setBounds(98, 0, 78, 191);
panel.add(textPane_branding);
JTextPane textPane_trunk = new JTextPane();
textPane_trunk.setBounds(0, 0, 88, 191);
panel.add(textPane_trunk);
I'm not quite sure what you are trying to achieve here. If you want both of your JTextPane to be scrollable you need to put each one into its own JScrollPane. That would look like this:
JTextPane textPane_branding = new JTextPane();
JScrollPane scroll_branding = new JScrollPane(textPane_branding);
scroll_branding.setBounds(98, 0, 78, 191);
panel.add(scroll_branding);
JTextPane textPane_trunk = new JTextPane();
JScrollPane scroll_trunk = new JScrollPane(textPane_trunk);
scroll_trunk.setBounds(0, 0, 88, 191);
panel.add(scroll_trunk);
If you want both of your JTextPane being into one JPanel that is scrollable I'm wondering why you set fixed Bounds to your JScrollPane and to the JTextPanes. That makes Scrolling absurd here. And thats why JScrollPanes don't work with Panes that don't have Layouts and use fixed Bounds. Also it is very bad practise.
So I would suggest here to stay with a Layout Manager in your JPanel and use setPreferredSize with your JTextPanes to define your desired Dimensions. And then your JScrollPane will start working.
JScrollPane scrollPane = new JScrollPane();
//scrollPane.setBounds(402, 211, 178, 193); // Don't do this!
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
//panel.setLayout(null); // Use a Layout Manager
JTextPane textPane_branding = new JTextPane();
textPane_branding.setPreferredSize(new Dimension(78,191));
//textPane_branding.setBounds(98, 0, 78, 191);
panel.add(textPane_branding);
JTextPane textPane_trunk = new JTextPane();
textPane_trunk.setPreferredSize(new Dimension(88,191));
//textPane_trunk.setBounds(0, 0, 88, 191);
panel.add(textPane_trunk);

JPanel/JFrame Automatically resizing. JAVA

I so have made a GUI for a Chat that im currently working on. Its aaaaalmost done but the only thing is the automatically resizing when dragging the window. I can't find out why its happend and this was my last chance. So I really need help from you guys! Im kinda out of idea
The code is here:
ClientGUI(String host, int port) {
super("Chat Client");
defaultPort = port;
defaultHost = host;
// The CenterPanel which is the chat room
MessageText = new JTextArea("Welcome to the Chat room\n");
JPanel centerPanel = new JPanel();
MessageText.setWrapStyleWord(true);
MessageText.setLineWrap(true);
centerPanel.setLayout(null);
JScrollPane scrollPane = new JScrollPane(MessageText);
scrollPane.setBounds(0, 0, 584, 486);
getContentPane().add(scrollPane);
MessageText.setEditable(false);
getContentPane().add(centerPanel, BorderLayout.CENTER);
WriteMessage = new JTextField("Write your username here!");
WriteMessage.setBounds(0, 492, 584, 35);
centerPanel.add(WriteMessage);
WriteMessage.setColumns(234);
WriteMessage.setBackground(Color.WHITE);
// the 3 buttons
Login = new JButton("Login");
Login.setBounds(594, 338, 125, 35);
centerPanel.add(Login);
Logout = new JButton("Logout");
Logout.setBounds(594, 469, 125, 35);
centerPanel.add(Logout);
Logout.addActionListener(this);
Logout.setEnabled(false); // you have to login before being able to logout
Online = new JButton("Online");
Online.setBounds(594, 403, 125, 35);
centerPanel.add(Online);
Online.addActionListener(this);
Online.setEnabled(false); // you have to login before being able to Who is in
JLabel PortNumberText = new JLabel("Port Number: ");
PortNumberText.setBounds(594, 83, 144, 20);
centerPanel.add(PortNumberText);
PortNumber = new JTextField("" + port);
PortNumber.setBounds(594, 114, 129, 20);
centerPanel.add(PortNumber);
PortNumber.setHorizontalAlignment(SwingConstants.RIGHT);
JLabel ServerText = new JLabel("Server Address: ");
ServerText.setBounds(594, 21, 240, 20);
centerPanel.add(ServerText);
// the two JTextField with default value for server address and port number
ServerAddress = new JTextField(host);
ServerAddress.setBounds(594, 52, 129, 20);
centerPanel.add(ServerAddress);
Login.addActionListener(this);
WriteMessage.requestFocus();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(760, 570);
setVisible(true);
}
A picture what I mean
Don't use a null layout. Don't use setBounds(). Swing was designed to be used with layout managers.
Resizing of components can only be done when you use a layout manager. Read the section from the Swing tutorial on Layout Manager for more information and examples. You can always use multiple panels each with a different layout manager to get your desired results.
Also follow Java naming conventions. Variable names should NOT start with an upper case character. Half the time your names are correct and half the time they are not. Be consistent!
You are using a Null-Layout (absolute positioning), and this means you have to take care of everything related to layout yourself. Better use a LayoutManager.
Alright so I just made a border layout all from start so I guess its all good, Thanks everyone for helping! Doesnt look that professional. So yeah, Might be some more updates later on :)

refreshing data in jscroll pane swings

I have a main panel in which there is another panel to which i am applying scrolling.This panel in turn contains 2 panels.
Now when i apply scrolling the scroll bar appears.
The problem is when I dynamically add data from the user screen partially.....the data appears onto the screen but I am not able to scroll it panel down and the data below the scroll bar is not seen properly.
generalInformationPanel = new JPanel(new GridLayout(1, 1));
generalInformationPanel.setBounds(0, 0, 1050, 500);
generalInformationPanel.setBackground(Color.WHITE);
gd_GeneralInformationUpperPanel = new JPanel();
gd_GeneralInformationUpperPanel.setBounds(0, 0, 1050, 700);
gd_GeneralInformationUpperPanel.setLayout(null);
gd_GeneralInformationUpperPanel.setBackground(Color.WHITE);
js1 = new JScrollPane(gd_GeneralInformationUpperPanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
js1.setBounds(0, 0, 950, 450);
js1.setVisible(true);
generalInformationPanel.add(js1);
add(generalInformationPanel, BorderLayout.CENTER);

JTextfield in JTabbedPane oversized

textArea = new JTextArea(textString);
JScrollPane text = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
text.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
And I add the JScrollPane text to a JTabbedPane, nothing special.
However the texfield expands and resizes the entire window when I switch tabs.
JFrame frame = new JFrame();
frame.setSize(600, 500);
JPanel main = new JPanel();
main.setLayout(new BorderLayout(0,0));
main.setBorder(new EmptyBorder(10, 10, 10, 10) );
textArea = new JTextArea(textString);
JScrollPane text = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
text.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
JTabbedPane tabs = new JTabbedPane();
tabs.add("Text lines", text);
tabs.add("Another", new JPanel());
main.add(new JLabel("test"), BorderLayout.NORTH);
main.add(tabs, BorderLayout.SOUTH);
frame.setContentPane(main);
frame.pack();
The problem that I found with your example is that when you enter many lines in the text area and switch to another tab, then the tabbed pane grows in size. In order to fix that, just enter how many text area rows you want to be display in the JTextArea constructor. For example, to display 5 rows:
textArea = new JTextArea(textString, 5, 0);
Now the tabbed pane will not get resized.

a retractable JTextArea with scrollpane

Goodevening
how can have a JTextArea like in netbeans (see the pic)
(source: hostingpics.net)
my code of the JTextArea:
JTextArea infoArea = new JTextArea(10,10);
infoArea.setLineWrap(true);
infoArea.setFont(police);
infoArea.setForeground(Color.YELLOW);
infoArea.setBackground(Color.BLACK);
infoArea.setEditable(false);
JScrollPane scroll = new JScrollPane(infoArea);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setBorder(BorderFactory.createEmptyBorder(5, 0, 10, 0));
scroll.setPreferredSize(new Dimension(sousFrame.getWidth(),90));
scroll.setFont( new Font("Arial", Font.BOLD, 13));
scroll.setBorder(BorderFactory.createTitledBorder("Output"));
thank you
I'm not sure what NetBeans uses, but we used flexdock at my last company to create dockable windows in a Java Swing application (assuming this is what you meant by "retractable").

Categories

Resources