Create tab header of JTabbedPane manually with JPanel and JLabel - java

I want to create a JTabbedPane, but I want to create the headers manually, I created a JPanel and JLabel where I put my Icon and background color, but I don't know how to add it to the tabbed pane.
The tabbed pane has 4 panels:
JPanel header = new JPanel();
header.setSize(100, 50);
header.setBackground(Color.red);
JLabel icon_Label = new JLabel(Icon);
icon_Label.setText("header 1");
jTabbedPane1.getComponentAt(1).*************?!

i find it
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
JLabel title = new JLabel("OK");
title.setForeground(Color.RED);
title.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/icons8-boƮte-pleine-64.png")));
title.setText("GESTION A");
panel.add(title);
jTabbedPane1.setTabComponentAt(2, panel);
but there is space in the left and right of the header !

Related

My JTextArea's are not respecting the size of the component and are being cut off

I have been working on this for hours. I honestly cannot figure it out. I have JTextArea's inside a JSplitPane which is inside a JPanel with a JButton and all that is put in my JFrame. I am using Layout managers. I have tried using pack(). I have tried using preferred sizes. Without the JPanel my button does not display in the proper location or switch buttons in other Tabs. With the JPanel it cuts off all my text, stops the scroll function(yes I have tried setting the TextAreas to always have horizontal and vertical scroll bars...does not solve the problem where text just stops wrapping for no apparent reason).
public static void main(String[] args) throws IOException
{
JFrame frame = new JFrame();
Deck blackjack = new Deck(Deck.TYPE[0]);
JTextArea textBlackjackUnshuffled = new JTextArea();
JTextArea textBlackjackShuffle = new JTextArea();
JButton shuffleButtonBlackjack = new JButton(new ImageIcon(ImageIO.read(new File("res/shuffle.png"))));
JToolBar toolBarBlackjack = new JToolBar("Blackjack");
JSplitPane splitPaneBlackjack = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JPanel panel = new JPanel();
JTabbedPane tabbedPaneBlackJack = new JTabbedPane();
JTabbedPane tabbedPaneCanasta = new JTabbedPane();
JTabbedPane tabbedPanePinochle = new JTabbedPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
textBlackjackUnshuffled.setColumns(10);
textBlackjackUnshuffled.setLineWrap(true);
textBlackjackUnshuffled.setWrapStyleWord(true);
textBlackjackUnshuffled.setEditable(false);
textBlackjackUnshuffled.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackUnshuffled.append(blackjack.toString());
textBlackjackShuffle.setColumns(10);
textBlackjackShuffle.setLineWrap(true);
textBlackjackShuffle.setWrapStyleWord(true);
textBlackjackShuffle.setEditable(false);
textBlackjackShuffle.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackShuffle.append(blackjack.toString());
shuffleButtonBlackjack.setBorderPainted(false);
shuffleButtonBlackjack.setFocusPainted(false);
shuffleButtonBlackjack.setContentAreaFilled(false);
splitPaneBlackjack.add(new JScrollPane(textBlackjackUnshuffled));
splitPaneBlackjack.add(new JScrollPane(textBlackjackShuffle));
panel.add(splitPaneBlackjack, BorderLayout.CENTER);
panel.add(shuffleButtonBlackjack, BorderLayout.PAGE_END);
tabbedPaneBlackJack.addTab("Blackjack", panel);
frame.add(tabbedPaneBlackJack);
frame.setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));
frame.setVisible(true);
}
You're adding the JScrollPanes to the panel in BorderLayout positions, but have not set the layout manager of panel to BorderLayout. In this situation, panel will be using JPanel's default layout manager, FlowLayout, a manager which is not smart enough to respect the scroll pane's preferred sizes.
Your code needs:
panel.setLayout(new BorderLayout());

Adding a label to the center south of a panel?

The code below adds the label to the left south of the panel, and when I use set location with the label, the position does not change. Is there a way to make the label be in the center south of the panel without the need for an extra panel?
EDIT: the JFrame has a BorderLayout and adds the panel to CENTER
JPanel pnl = new JPanel();
pnl.setPreferredSize(new Dimension(500,500));
JLabel lbl = new JLabel("label");
pnl.setLayout(new BorderLayout());
pnl.add(lbl, BorderLayout.SOUTH);
It seem you need to set text align of label to center panel?
If so, try this:
JPanel pnl = new JPanel();
pnl.setPreferredSize(new Dimension(500, 500));
JLabel lbl = new JLabel("label", SwingConstants.CENTER); //Set text align
pnl.setLayout(new BorderLayout());
pnl.add(lbl, BorderLayout.SOUTH);
lbl.setBackground(Color.red);
lbl.setOpaque(true); //Test background
getContentPane().add(pnl);
Result:

Adding buttons and scrollable panel to a tab - Java

I'm trying to build a Java GUI where among other things there will be several tabs.
In one of the tabs I want to have both a scrollable JTextArea, and also some buttons at the top/bottom that interacts with the JTextArea. I can't figure out how to get both into the same tab, I'm either getting the buttons and a non-scrollable jtextarea, or just the scrollable jtextarea. I also don't want to display the button in the other tabs. Here is my code:
private final JTextArea music = new JTextArea();
private final JTextArea button = new JTextArea();
private final JTextArea test = new JTextArea();
private final JTabbedPane tab = new JTabbedPane();
private JTable table;
music.append(newPlaylist.toString());
JFrame frame = new JFrame("GUI");
frame.setTitle("Music File Organiser");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton button1 = new JButton("Hello");
JButton button2 = new JButton("Sort by Album Title");
JButton button3 = new JButton("Sort by Track Title");
button1.addActionListener(new ActionListener() {code};
button2.addActionListener(new ActionListener() {code};
button3.addActionListener(new ActionListener() {code};
panel.add(music);
panel.add(button1);
panel.add(button2);
panel.add(button3);
JScrollPane scroll = new JScrollPane(panel);
JScrollPane scroll2 = new JScrollPane(table);
tab.add("Music Files", scroll);
tab.add("Table", scroll2);
this.add(tab);
frame.setLocationRelativeTo(null);
this.setSize(1200, 1000);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The tab in mind is the first one. How do I add buttons to "scroll"? The way I'm trying it here is to add the JTextArea "music" into panel, then adding the buttons to the same panel, then adding the panel to a JScrollPane, and then adding the JScrollPane to tab. Any help would really be appreciated.
Encapsulate your visual elements into child Panels.
JPanel buttonPanel = new JPanel(); //panel for buttons.
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
JScrollPane scroll = new JScrollPane(music); //scrollable pane for JTextArea
panel.add(buttonPanel);
panel.add(scroll); //add sub-components to panel for tab
/*here you would add some layout code to fit the panel and scroll into the associated spaces */
tab.add("Music Files", panel); //add panel to tab
You can do something like this:
JPanel tab = new JPanel()
JPanel tabWithButtons = new JPanel()
JPanel tabWithScrollPanel = new JPanel()
tab.add(tabWithScrollPanel)
tab.add(tabWithButtons)
IMO this is better way, because I assume that buttons shouldn't be included in JScrollPane and they should be visible all the time
Of course, you have to add all elements to proper JPanels.
Also, you can try with width/height settings of panel which contains all your elements, maybe ScrollArea panel is to big?

Needs ideas on how to insert a small text phrase above a panel

I am trying to insert the "Puzzle Game" text circled in red in the attached image.
I have already placed the 4 blue buttons and the textfields all in 1 GridLayout.
I tried inserting the text together with the GridLayout as a non-clickable button, but it didn't work as the sizes per cell in GridLayout is the same throughout.
Trying to use setBounds as well, but it doesn't even show up in the JFrame =/ .
// create a new panel - panel1
JPanel panel1 = new JPanel();
// set layout of panel1
panel1.setLayout(new GridLayout(3,2));
panel1.setPreferredSize(new Dimension(500,200));
// create new buttons - button1 to button4
JButton button1 = new JButton("Start Game");
JButton button2 = new JButton("Get History");
JButton button3 = new JButton("Reset Game");
JButton button4 = new JButton("Exit Game");
// create label and text field for entering of player's names
JLabel label1 = new JLabel("Enter Player's name:",JLabel.CENTER);
JTextField field1 = new JTextField();
// add the labels and text field to panel1
panel1.add(label1);
panel1.add(field1);
// adds button1 to button4 to panel1
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
// create a new general panel to contain all panels containing components placed at the bottom
JPanel btmGenP = new JPanel();
this.add(btmGenP,BorderLayout.SOUTH);
btmGenP.add(panel1,FlowLayout.LEFT);
// create a new panel - panel2
JPanel panel2 = new JPanel();
panel2.setLayout(null);
panel2.setBounds(50,50,50,50);
this.add(panel2);
// add Jlabel text to panel2
JLabel puzgame = new JLabel("~~Puzzle Game~~");
panel2.add(puzgame);
Try this:
panel1.setBorder(BorderFactory.createTitledBorder("~~Puzzle Game~~"));

Jpanel size after adding jlabel component

I have a central panel. This is my parent panel. I am adding 3 panels to the parent panel.
The panels are going to be stacked vertically. Like a title panel, then a middle panel, then a bottom panel. I just want to focus on my title panel. When I create a jlabel using text. The label shows and the panel borders stretches the entire width of the parent panel, which is what I want.
private JPanel titlePanel() {
String text = "<html><b><big><font color=#5C8C5C>Help Dialog</font></big></b></html>";
JLabel textLabel = new JLabel(text, JLabel.CENTER);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
p.add(textLabel);
p.setBorder(BorderFactory.createLineBorder(Color.black));
return p;
}
I am actually wanting to use a icon as the label and not html text. So make the changes to the code.
private JPanel titlePanel() {
Registry appReg = Registry.getRegistry(this);
ImageIcon ediLabelIcon = appReg.getImageIcon("ToolLabel.ICON");
JLabel textLabel = new JLabel(ediLabelIcon, JLabel.CENTER);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
p.add(textLabel);
p.setBorder(BorderFactory.createLineBorder(Color.black));
return p;
}
Now the label shows, but the border of the panel is only as wide as the label and not stretched out the width of the parent panel.
I am trying to figure out to extend the panel border the width of the parent panel and not just as wide as the label. This is the code for the parent panel.
private void createDialog() {
Component titlePanel = titlePanel();
Component verbiagePanel = verbiagePanel();
Component closeButtonPanel = closeButton();
setTitle("HELP Dialog");
centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
centerPanel.setPreferredSize(new Dimension(600, 300));
centerPanel.add(titlePanel);
centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
centerPanel.add(verbiagePanel);
centerPanel.add(Box.createHorizontalGlue());
centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
centerPanel.add(closeButtonPanel);
getContentPane().add(centerPanel);
this.pack();
}
Using HTML in JLabel text switched the mechanism which calculate preferred size fot JLabel.
Now I can't explain it in detail, but if you change creating title label to
JLabel textLabel = new JLabel("<html></html>", ediLabelIcon, JLabel.CENTER);
your label will be stretched out to parent panel width.
Or you may choose another layout manager such as GridBagLayout. With GridBagLayout you can force stretch any component to its parent width.

Categories

Resources