I'm building an application which will consist of few tabbed panels. On each of them, I'd like to put sets of components separated from each other by borders. It would look like:
|- Titled Border 1 ---
[JTextField] [JComboBox] [JTextField] [JComboBox]
|--------
|- Titled Border 2 ---
[JTextField] [JComboBox] [JTextField] [JComboBox]
|--------
... and so forth.
When I tried to simply add new border, Titled Border 2, to the panel, it was added and covered the first one leaving components on top though. In some examples I saw many JPanels defined within one frame and each panel had its own border. It might work in my case but how to add these panels to be displayed within one tab?
One of Oracle's tutorials shows exactly a tabbed pane with demo of many kinds of borders. When I tried to edit it and put a component there, it appeared between two borders instead of being surrounded. And it was another option that was unsuccessful for me.
Second thing is, I don't use any layout manager, components positions are fixed and honestly I would keep this setting. Or maybe you recommend using any layout manager in this specific case?
Would you have any hints on how to solve this problem?
Edit: it seems I'm not allowed yet to attach a screenshot, but here's the part of code taking care of displaying the borders:
lenMicro = new JPanel();
lenMicro.setLayout(null);
bGreyLine = BorderFactory.createLineBorder(Color.GRAY, 1, true);
bTitled1 = BorderFactory.createTitledBorder(bGreyLine, "Length (1/2)", TitledBorder.LEFT, TitledBorder.TOP);
lenMicro.setBorder(bTitled1);
bTitled2 = BorderFactory.createTitledBorder(bGreyLine, "Length (2/2)", TitledBorder.LEFT, TitledBorder.TOP);
lenMicro.setBorder(bTitled2);
The border titled "Length (2/2)" is displayed when the last two lines are uncommented.
To put a border around a JComponent , you use its setBorder method. You can use the BorderFactory class to create most of the borders that Swing provides. If you need a reference to a border — say, because you want to use it in multiple components — you can save it in a variable of type Border .
createLineBorder() − To create a line border. JPanel. setBorder(border) − To set the desired border to the JPanel.
You can't have a border directly on a JWindow or JFrame. You need to put a JPanel with the desired border using the default JWindow/JFrame LayouManager (a BorderLayout). Then the JPanel is extended giving the impression that its borders are the JWindow one.
Frame: The lines and borders that contain the panels.
When using Absolute Positioning, you have to provide Location for each and every component, that will become a part of the view. Hence without looking at exactly what you doing it's hard to predict where exactly the things are going wrong. Though using Layout Managers, will remove this big burden off your shoulders regarding placement of different components on the view.
Moreover, you have to set the border for the respective component. So in no way I can assume that you added one component and it appeared between two borders (though considering the fact that you using Absolute Positioning, you might have given the wrong Co-ordinates for the said component on the view). Please have a look at this example code, that might can help you a bit in this direction :
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class BorderExample
{
private JPanel topPanel;
private JPanel centerPanel;
private JPanel bottomPanel;
private int hgap;
private int vgap;
private JTextField tfield1, tfield2;
private JComboBox cbox1, cbox2;
private String[] data = {"One", "Two"};
public BorderExample()
{
hgap = 5;
vgap = 5;
}
private void displayGUI()
{
JFrame frame = new JFrame("Border Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(
BorderFactory.createEmptyBorder(hgap, hgap, hgap, hgap));
contentPane.setLayout(new BorderLayout(hgap, vgap));
topPanel = new JPanel();
topPanel.setOpaque(true);
topPanel.setBackground(Color.WHITE);
topPanel.setBorder(
BorderFactory.createTitledBorder("Top Panel"));
tfield1 = new JTextField(10);
tfield1.setBorder(
BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(
EtchedBorder.RAISED, Color.GRAY
, Color.DARK_GRAY), "JTextField"));
JPanel comboPanel = new JPanel();
cbox1 = new JComboBox(data);
cbox1.setBorder(
BorderFactory.createTitledBorder("JComboBox"));
topPanel.add(tfield1);
topPanel.add(cbox1);
centerPanel = new JPanel();
centerPanel.setOpaque(true);
centerPanel.setBackground(Color.WHITE);
centerPanel.setBorder(
BorderFactory.createTitledBorder("Center Panel"));
tfield2 = new JTextField(10);
tfield2.setBorder(
BorderFactory.createLoweredBevelBorder());
cbox2 = new JComboBox(data);
cbox2.setBorder(
BorderFactory.createRaisedBevelBorder());
centerPanel.add(tfield2);
centerPanel.add(cbox2);
bottomPanel = new JPanel();
bottomPanel.setOpaque(true);
bottomPanel.setBackground(Color.WHITE);
bottomPanel.setBorder(
BorderFactory.createTitledBorder("Center Panel"));
contentPane.add(topPanel, BorderLayout.PAGE_START);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(bottomPanel, BorderLayout.PAGE_END);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new BorderExample().displayGUI();
}
});
}
}
Here is the output of the same :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With