horizontal java swing jtable jpanel jscrollbar

java - jtable horizontal scrollbar netbeans



JPanel en JTabBar no es desplazable (3)

¡Mucho por tu respuesta! Sin embargo, esto solo hace que el JTable se pueda desplazar. ¿También es posible hacer que el JPanel se pueda desplazar donde están las tablas?

Si esto es lo que necesitas

entonces podrías hacer algo como esto:

public JScrollPane createLayout() { JPanel panel = new JPanel(); JScrollPane sp = new JScrollPane(panel); sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); // or ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(table1()); panel.add(Box.createRigidArea(new Dimension(0,10))); panel.add(table2()); panel.add(Box.createRigidArea(new Dimension(0,10))); return sp; }

ingrese el código aquí. Tengo el siguiente problema.

Estoy agregando dos JPanels a un panel y agrego esto finalmente a mi TabbedPane. Sin embargo, quiero que sea desplazable, por lo tanto, solo hago que el Panel se pueda desplazar, lo que simplemente agrega mi desplazamiento en el medio de la barra.

Aquí está mi ejemplo:

public class Test extends JFrame { private static final long serialVersionUID = -4682396888922360841L; private JMenuBar menuBar; private JMenu mAbout; private JMenu mMain; private JTabbedPane tabbedPane; public SettingsTab settings = new SettingsTab(); private void addMenuBar() { menuBar = new JMenuBar(); mMain = new JMenu("Main"); mAbout = new JMenu("About"); menuBar.add(mMain); menuBar.add(mAbout); setJMenuBar(menuBar); } public void createTabBar() { tabbedPane = new JTabbedPane(JTabbedPane.TOP); JComponent panel2 = new JPanel(); tabbedPane.addTab("Test1", null, panel2, "Displays the results"); tabbedPane.addTab("Settings", settings.createLayout()); add(tabbedPane); tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); } private void makeLayout() { setTitle("Test"); setLayout(new BorderLayout()); setPreferredSize(new Dimension(1000, 500)); addMenuBar(); createTabBar(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } public void start() { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { makeLayout(); } }); } public static void main(String[] args) { Test gui = new Test(); gui.start(); } public class SettingsTab extends JPanel { public JPanel createLayout() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(table1()); panel.add(Box.createRigidArea(new Dimension(0,10))); panel.add(table2()); panel.add(Box.createRigidArea(new Dimension(0,10))); panel.add(new JScrollBar()); return panel; } public JPanel table1() { JPanel panel1 = new JPanel(); String[] columnNames = {"First Name", "Last Name"}; Object[][] data = {{"Kathy", "Smith"}, {"John", "Doe"}, {"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"} }; final JTable table = new JTable(data, columnNames); panel1.add(table); panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS)); return panel1; } public JPanel table2() { JPanel panel1 = new JPanel(); String[] columnNames = {"First Name", "Last Name"}; Object[][] data = { {"Kathy", "Smith"}, {"John", "Doe"}, {"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"} }; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); panel1.add(table); panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS)); return panel1; } } }

¿Alguna recomendación de lo que estoy haciendo mal?

¡Agradezco tu respuesta!

ACTUALIZAR

Lo probé con un JScrollPane pero todavía no funciona:

public class Test extends JFrame { private static final long serialVersionUID = -4682396888922360841L; private JMenuBar menuBar; private JMenu mAbout; private JMenu mMain; private JTabbedPane tabbedPane; public SettingsTab settings = new SettingsTab(); private void addMenuBar() { menuBar = new JMenuBar(); mMain = new JMenu("Main"); mAbout = new JMenu("About"); menuBar.add(mMain); menuBar.add(mAbout); setJMenuBar(menuBar); } public void createTabBar() { tabbedPane = new JTabbedPane(JTabbedPane.TOP); JComponent panel2 = new JPanel(); tabbedPane.addTab("Test1", null, panel2, "Displays the results"); tabbedPane.addTab("Settings", settings.createLayout()); add(tabbedPane); tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); } private void makeLayout() { setTitle("Test"); setLayout(new BorderLayout()); setPreferredSize(new Dimension(1000, 500)); addMenuBar(); createTabBar(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } public void start() { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { makeLayout(); } }); } public static void main(String[] args) { Test gui = new Test(); gui.start(); } public class SettingsTab extends JScrollPane { public JPanel createLayout() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(table1()); panel.add(Box.createRigidArea(new Dimension(0,10))); panel.add(table2()); panel.add(Box.createRigidArea(new Dimension(0,10))); panel.add(table3()); panel.add(Box.createRigidArea(new Dimension(0,10))); add(new JScrollPane()); return panel; } public JPanel table1() { JPanel panel1 = new JPanel(); String[] columnNames = {"First Name", "Last Name"}; Object[][] data = { {"Kathy", "Smith"}, {"John", "Doe"}, {"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"}, {"John", "Doe"}, {"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"} }; final JTable table = new JTable(data, columnNames); panel1.add(table); panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS)); return panel1; } public JPanel table2() { JPanel panel1 = new JPanel(); String[] columnNames = {"First Name", "Last Name"}; Object[][] data = { {"Kathy", "Smith"}, {"John", "Doe"}, {"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"}, {"John", "Doe"}, {"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"} }; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); panel1.add(table); panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS)); return panel1; } public JPanel table3() { JPanel panel1 = new JPanel(); String[] columnNames = {"First Name", "Last Name"}; Object[][] data = { {"Kathy", "Smith"}, {"John", "Doe"}, {"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"}, {"John", "Doe"}, {"Sue", "Black"}, {"Jane", "White"}, {"Joe", "Brown"} }; final JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); panel1.add(table); panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS)); return panel1; } } }


¡Debes agregar un JScrollPane en él! :)

Realmente no sé lo que hiciste allí, pero básicamente dice así:

JPanel myPanel = new JPanel(); // the main content is on the myPanel myPanel.add(myContent);// add everything you want new JFrame().add(new JScrollPane(myPanel));


Agregue JTable dentro de JScrollPane y luego agregue JScrollPane en el JPanel .

JScrollPane pane = new JScrollPane(table); panel1.add(pane);

no hay necesidad de línea debajo:

panel.add(new JScrollBar());

Consulte el Tutorial de Swing sobre Cómo usar tablas donde también puede encontrar ejemplos.

Puede establecer el tamaño de JPanel si es necesario ( no siempre se requiere ) y luego agregarlo dentro del JScrollPane

JPanel panel = new JPanel(){ @Override public Dimension getSize(){ return new Dimension(...,....); } };

EDITAR

No está agregado dentro del JScrollPane . Debería ser

tabbedPane.addTab("Settings", new JScrollPane(settings.createLayout()));

y eliminar debajo de la línea:

add(new JScrollPane());