Search code examples
javaswinguser-interfacejbutton

How to add a button in border?


I'm writing a program and I want to add button and label on top right above JTabbedPane. I use MatteBorder and I want to add label (not title) and 1 button where the blue border is. thanks for your help.

import java.awt.*;
import static java.awt.Font.BOLD;
import java.awt.event.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;
import static javax.swing.SwingConstants.CENTER;
import javax.swing.Timer;
import javax.swing.border.*;
import javax.swing.event.*;



public class hotels extends JFrame{

    JButton hotel;
    JLabel image;
     JTabbedPane tabbed,tabbed1;
    JPanel panel;
    JPanel panel1;

    Container pane;
    JPanel panel2;
    JLabel departure;
    JLabel from;
    JLabel to;


    public hotels(){


         panel=new JPanel();
        panel.setBackground(Color.cyan);
        hotel=new JButton();
        hotel.setText("Hotels");

        Font myFont = new Font("Serif", Font.BOLD, 18);


        hotel.setFont(myFont);
        panel.setLayout(null);
        panel.add(hotel);

        hotel.setBounds(50, 80, 100, 40);


        image=new JLabel();

        image.setBounds(50,1,80,80);
        image.setBorder(BorderFactory.createLineBorder(Color.yellow));
        image.setBackground(Color.white);
        image.setIcon(new ImageIcon("2.gif"));
        panel.add(image);

       panel1=new JPanel();
        panel1.setLayout(null);

      tabbed=new JTabbedPane();




         tabbed.setBorder(BorderFactory.createMatteBorder(24, 6, 12, 6, Color.blue));

          Font myFont1 = new Font("Serif", Font.BOLD, 18);
          tabbed.setFont(myFont1);

            tabbed.setBounds(0, 20, 80, 40);

     tabbed.add( "Round Trip",panel1);

     panel2=new JPanel();
     panel2.setLayout(null);
     panel2.setBackground(Color.white);
     departure=new JLabel();
     departure.setText("Departure");

     Font f=new Font("Serif", Font.PLAIN,12);
     departure.setBounds(50,1,80,80);
     departure.setFont(f);
     panel2.add(departure);
       tabbed.add("One Way",panel2);
       from=new JLabel();
       from.setBackground(Color.blue);

       panel1.setBackground(Color.yellow);



        pane=getContentPane();


          pane.add(panel);

   image.addMouseListener(new MouseAdapter()

   {

      public void mouseClicked(MouseEvent e){
           if (e.getSource()==image){

      pane.removeAll();

      pane.add(tabbed);
      pane.revalidate();
      pane.repaint();

           }


       }


   }


   );

    }



    public static void main(String[] args) {

 hotels mw=new hotels();
  mw.setVisible(true);
  mw.setSize(300, 400);


    }



    }

Solution

  • I want to add button and label on top right above JTabbedPane.

    You can add Components above the JTabbedPane by nesting Containers with the appropriate layout. For instance, to place a JLabel and JButton above and top right of the JTabbedPane, you can use a BoxLayout - in the mouseClicked implementation where the JTabbedPane is added:

    pane.removeAll();
    Box vert = Box.createVerticalBox();
    Box top = Box.createHorizontalBox();
    top.add(Box.createHorizontalGlue());
    top.add(new JLabel("Top Label"));
    top.add(new JButton("Top Button"));
    vert.add(top);
    vert.add(tabbed);
    pane.add(vert);
    ....
    

    Your requirements are not explicit, and so you may have to play with the background colors and border position (for instance, add the Border to vert rather than tabbed) to meet your requirements.