Java Swing resize panel inside contentPanel - java

I have the following structure in a frame:
Frame->contentPane panel->topPanel panel->table
This is the code:
private JPanel contentPane;
private JTable table;
private JPanel topPanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ShowList frame = new ShowList();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ShowList() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 675, 433);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
topPanel = new JPanel();
topPanel.setBounds(76, 76, 491, 245);
contentPane.add(topPanel);
topPanel.setLayout(null);
table = new JTable();
table.setBounds(0, 0, 491, 245);
topPanel.add(table);
}
What I want to achieve is that when I resize the frame making the window bigger, the topPanel and the table that it is contained by this one resize also and do not stay the same size as they were before resizing the frame. I have read about Layouts but I can not make it work. Any suggestion?

Use a LayoutManager to control the size and position of the components inside of your frame. Avoid setting the layout manager of your panels as null.
I recommend you to take a look at this link: "A Visual Guide to Layout Managers" to learn more about the different kinds of layouts you can use.
In your code, for example, I would use a BorderLayout:
contentPane.setLayout(new BorderLayout());
...
contentPane.add(topPanel, BorderLayout.CENTER);
topPanel.setLayout(new BorderLayout());
...
topPanel.add(table, BorderLayout.Center);
By the way, I suppose your class ShowList extends JFrame because methods like setDefaultCloseOperation gives you an error if not, right?
I hope it helps.

Related

Add a scroll bar to text area

I use Eclipse Window Builder. When I click the button, something will be written on the screen. But since my prints are long, I want to use a scroll pane.
public class uyg2 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
uyg2 frame = new uyg2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public uyg2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(32, 29, 89, 23);
contentPane.add(btnNewButton);
JTextArea textArea = new JTextArea();
textArea.setBounds(10, 63, 233, 173);
contentPane.add(textArea);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setBounds(249, 10, 173, 118);
contentPane.add(scrollPane);
}
So, based on...
public class uyg1 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
uyg1 frame = new uyg1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public uyg1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JTextArea textArea = new JTextArea("Test");
textArea.setSize(400, 400);
JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scroll);
frame.setVisible(true);
}
}
textArea.setSize(400, 400); is irrelevant as the layout manager will deal with it. You can provide sizing hints via the JTextArea(String, int, int) constructer, but remember, this is the number of characters in width/height, not pixels.
The following are giving you issues...
frame.getContentPane().add(scroll);
frame.setVisible(true);
because frame is undefined. Since the class extends from JFrame, they are pointless, it should just be
getContentPane().add(scroll);
setVisible(true);
but, I'd add...
pack();
setLocationRelativeTo(null);
before it, as it will give you a generally better experience
You need to add the TextArea to the ScrollPane. Don't add the textarea tho the contentpane.

how to change the content of a panel using another panel from another classe in java

On Eclipse, using JFrames, I'm using a mainFrame as the main user interface and in that frame i got a contentPanel (JPanel) containing a small panel (JPanel as well), under that panel there's a buttong, and i have another class which i named 'clients' (another frame) containting a contentPanel too (JPanel) with other compenents, i want to get the the second class contentPanel to show in the first's panel (the small panel i mentionned).
here's what i did, but it's not working!! any help?
MainF.java
public class mainF extends JFrame {
private JPanel contentPane;
public JPanel panel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainF frame = new mainF();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public mainF() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 404);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
panel = new JPanel();
panel.setBounds(10, 11, 414, 256);
contentPane.add(panel);
panel.setLayout(null);
JLabel Panel = new JLabel("Main Panel");
Panel.setHorizontalAlignment(SwingConstants.CENTER);
Panel.setBounds(10, 11, 69, 33);
panel.add(Panel);
JButton btnClients = new JButton("Clients");
btnClients.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel = new Clients().getContentPane();
panel.setVisible(true);
}
});
btnClients.setBounds(162, 296, 89, 23);
contentPane.add(btnClients);
}
}
Clients
public class Clients extends JFrame {
private JPanel contentPane;
public JPanel getContentPane() {
return contentPane;
}
public Clients() {
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblClients = new JLabel("Clients");
lblClients.setHorizontalAlignment(SwingConstants.CENTER);
lblClients.setFont(new Font("Times New Roman", Font.BOLD, 18));
lblClients.setBounds(10, 11, 85, 37);
contentPane.add(lblClients);
}
}
Thanks Everyone!!
here's an image to explain the situation:
See the image here
You are using one JPanel reference panel, and using it to create 2 different panel objects. So first remove the panel from contentPane then add it back with new initialization.
public void actionPerformed(ActionEvent e) {
contentPane.remove(panel);
panel = new Clients().getContentPane();
panel.setVisible(true);
contentPane.add(panel);
contentPane.repaint();
contentPane.revalidate();
}
});
Also you require contentpane to revalidate() and repaint()

How to avoid Hgap for first component added to a JPanel using FlowLayout

In my software i have a JPanel containing some JComponent. The JPanel use a FlowLayout with a certain Hgap to separate these components.
I'm trying to have this kind of design, but the first component should be layed out on left, without any Hgap. Like This:
here is the code you can use to generate the example:
public class FlowLayoutExample {
public static void main(String [] a) {
final JFrame frame = new JFrame();
frame.setSize(new Dimension(500, 80));
frame.setMinimumSize(new Dimension(350, 80));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(initJPanel());
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
frame.setVisible(true);
}
});
}
private static JPanel initJPanel() {
JPanel panel = new JPanel();
FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
flowLayout.setHgap(25);
panel.setLayout(flowLayout);
panel.setBackground(Color.LIGHT_GRAY);
panel.add(initLabel());
panel.add(initLabel());
panel.add(initLabel());
panel.add(initLabel());
return panel;
}
private static Component initLabel() {
return new JLabel("MyLabel");
}
}
Thanks for any suggestion you'll leave !
You can use an EmptyBorder to fake it out:
panel.setBorder( new EmptyBorder(0, -25, 0, 0) );
Basically the border inset and layout gap are added together.

Adding vertical spacing to NORTH component in BorderLayout

I have a JFrame with a BorderLayout. I added a JPanel to the NORTH side of the JFrame. In this panel I want to add components to it in an absolute positioning. In the Center side of the JFrame I added another JPanel which should take a huge space. However when I run the application I see nothing from the North JPanel as the Center JPanel occupied all the space of the JFrame! How can I give vertical space to the North JPanel?
I really need to used absolute positioning for the north JPanel.
Here's my code:
public class AAAA extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AAAA frame = new AAAA();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AAAA() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1136, 520);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
panel.setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(0, 0, 117, 29);
panel.add(btnNewButton);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);
}
}
Update 1
I see you have already selected an answer (prematurely, I think). Here is the first iteration of what I believe you are trying to achieve. Without need for setting bounds or preferred sizes..
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class AAAA extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AAAA frame = new AAAA();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AAAA() {
super("Laid Out");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// OMG! If you can make a GUI break at 1336 px wide, it should be
// possible to make it break at ..much smaller!
//setBounds(100, 100, 1136, 520);
setBackground(Color.YELLOW);
contentPane = new JPanel();
contentPane.setBackground(Color.BLUE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
// make it a FlowLayout as FlowLayout.LEADING with no spacing to
// make the button snug up against the top left
JPanel panel = new JPanel(
new FlowLayout(FlowLayout.LEADING, 0, 0));
panel.setBackground(Color.GREEN);
contentPane.add(panel, BorderLayout.NORTH);
//panel.setPreferredSize(new Dimension(1024,400));
JButton btnNewButton = new JButton("New button");
// we change the margin to make the button bigger than natural size.
btnNewButton.setMargin(new Insets(6, 22, 6, 22));
panel.add(btnNewButton);
JPanel panel_1 = new JPanel();
// create a solic color image to both pad the GUI and
// provide visual indication of where it is.
BufferedImage bi = new BufferedImage(
400,200,BufferedImage.TYPE_INT_RGB);
JLabel padder = new JLabel(new ImageIcon(bi));
panel_1.add(padder);
panel_1.setBackground(Color.RED);
contentPane.add(panel_1, BorderLayout.CENTER);
pack();
setMinimumSize(getSize());
}
}
I really need to used absolute positioning for the north JPanel.
Why? If we know why you think you need to do this we can probably offer a better approach.
Don't use a null layout. Swing was designed to be used with layout managers.
The BorderLayout will respect the preferred height of the component added to the NORTH. The preferred height is zero so nothing is displayed.
Note: I am not suggesting that you set the preferred height of the panel, that is the job of the layout manager and that is why you should always use a layout manager. Layout managers do more than just set the size/location of a component.

location and size of jtextarea in jscrollpane is not set

I am working on the editor. I am using Java swing for it. I have embedded a JTextArea with JScrollPane. i want to position the jtextarea of particular size at the middle of JScrollPane. To do this I used setLocation function. But this is not working?
public class ScrollPaneTest extends JFrame {
private Container myCP;
private JTextArea resultsTA;
private JScrollPane scrollPane;
private JPanel jpanel;
public ScrollPaneTest() {
resultsTA = new JTextArea(50,50);
resultsTA.setLocation(100,100);
jpanel=new JPanel(new BorderLayout());
jpanel.add(resultsTA,BorderLayout.CENTER);
scrollPane = new JScrollPane(jpanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(800, 800));
scrollPane.setBounds(0, 0, 800, 800);
setSize(800, 800);
setLocation(0, 0);
myCP = this.getContentPane();
myCP.setLayout(new BorderLayout());
myCP.add(scrollPane);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new ScrollPaneTest();
}
}
You simply have to add the JTextArea to the JScrollPane, and add it to the CENTER of the JPanel having BorderLayout.
Don't use AbsolutePositioning. Add a proper LayoutManager, and let LayoutManager do the rest for positioning and sizing your components on the screen.
In order to use the setBounds(...) method you have to use a null Layout for your component, which is not worth using, provided the perspective, as mentioned in the first paragraph of the AbsolutePositioning. Though in the code example provided by you, you are doing both the thingies together i.e. using Layout and using AbsolutePositioning, which is wrong in every way. My advice STOP DOING IT :-)
In the example provided the ROWS and COLUMNS provided by you are sufficient to size the JTextArea by the Layout concern.
Code Example :
import java.awt.*;
import javax.swing.*;
public class Example
{
private JTextArea tarea;
private void displayGUI()
{
JFrame frame = new JFrame("JScrollPane Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
JScrollPane textScroller = new JScrollPane();
tarea = new JTextArea(30, 30);
textScroller.setViewportView(tarea);
contentPane.add(textScroller);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new Example().displayGUI();
}
});
}
}

Categories

Resources